@@ -5,8 +5,22 @@ use crate::state::{Compatibility, ParseState};
55use crate :: wordize:: Word ;
66use crate :: RMSFile ;
77use codespan:: { ByteIndex , FileId , Span } ;
8- pub use codespan_reporting:: diagnostic:: { Diagnostic , Label , Severity } ;
8+ use codespan_reporting:: diagnostic:: LabelStyle ;
9+ pub use codespan_reporting:: diagnostic:: Severity ;
910use lazy_static:: lazy_static;
11+ use std:: ops:: Range ;
12+
13+ /// Represents a diagnostic message that can provide information like errors and warnings to the user.
14+ pub type Diagnostic = codespan_reporting:: diagnostic:: Diagnostic < FileId > ;
15+ /// A label describing an underlined region of code associated with a diagnostic.
16+ pub type Label = codespan_reporting:: diagnostic:: Label < FileId > ;
17+
18+ fn span_to_range ( span : Span ) -> Range < usize > {
19+ Range {
20+ start : span. start ( ) . to_usize ( ) ,
21+ end : span. end ( ) . to_usize ( ) ,
22+ }
23+ }
1024
1125#[ derive( Debug , Clone ) ]
1226pub enum AutoFixReplacement {
@@ -103,7 +117,7 @@ impl Suggestion {
103117}
104118
105119/// A warning.
106- #[ derive( Debug , Clone ) ]
120+ #[ derive( Clone ) ]
107121pub struct Warning {
108122 diagnostic : Diagnostic ,
109123 /// A change suggestion: when present, the problem can be fixed by replacing the
@@ -120,9 +134,12 @@ impl Warning {
120134 pub const fn severity ( & self ) -> Severity {
121135 self . diagnostic . severity
122136 }
137+ pub fn main_label ( & self ) -> & Label {
138+ & self . diagnostic . labels [ 0 ]
139+ }
123140 /// Get additional labels for this warning.
124- pub const fn labels ( & self ) -> & Vec < Label > {
125- & self . diagnostic . secondary_labels
141+ pub fn labels ( & self ) -> & [ Label ] {
142+ & self . diagnostic . labels [ 1 .. ]
126143 }
127144 /// Get the human-readable error message.
128145 pub fn message ( & self ) -> & str {
@@ -143,10 +160,14 @@ impl Warning {
143160 pub ( crate ) fn warning ( file_id : FileId , span : Span , message : impl Into < String > ) -> Self {
144161 let message: String = message. into ( ) ;
145162 Warning {
146- diagnostic : Diagnostic :: new_warning (
147- message. clone ( ) ,
148- Label :: new ( file_id, span, message) ,
149- ) ,
163+ diagnostic : Diagnostic :: warning ( )
164+ . with_message ( message. clone ( ) )
165+ . with_labels ( vec ! [ Label :: new(
166+ LabelStyle :: Primary ,
167+ file_id,
168+ span_to_range( span) ,
169+ )
170+ . with_message( message) ] ) ,
150171 suggestions : vec ! [ ] ,
151172 }
152173 }
@@ -156,7 +177,14 @@ impl Warning {
156177 pub ( crate ) fn error ( file_id : FileId , span : Span , message : impl Into < String > ) -> Self {
157178 let message: String = message. into ( ) ;
158179 Warning {
159- diagnostic : Diagnostic :: new_error ( message. clone ( ) , Label :: new ( file_id, span, message) ) ,
180+ diagnostic : Diagnostic :: error ( )
181+ . with_message ( message. clone ( ) )
182+ . with_labels ( vec ! [ Label :: new(
183+ LabelStyle :: Primary ,
184+ file_id,
185+ span_to_range( span) ,
186+ )
187+ . with_message( message) ] ) ,
160188 suggestions : vec ! [ ] ,
161189 }
162190 }
@@ -169,9 +197,9 @@ impl Warning {
169197
170198 /// Add a note referencing a snippet of code.
171199 pub ( crate ) fn note_at ( mut self , file_id : FileId , span : Span , message : & str ) -> Self {
172- self . diagnostic = self
173- . diagnostic
174- . with_secondary_labels ( vec ! [ Label :: new ( file_id , span , message ) ] ) ;
200+ self . diagnostic . labels . push (
201+ Label :: new ( LabelStyle :: Secondary , file_id , span_to_range ( span ) ) . with_message ( message ) ,
202+ ) ;
175203 self
176204 }
177205
@@ -188,10 +216,14 @@ impl Word<'_> {
188216 pub ( crate ) fn warning ( & self , message : impl Into < String > ) -> Warning {
189217 let message: String = message. into ( ) ;
190218 Warning {
191- diagnostic : Diagnostic :: new_warning (
192- message. clone ( ) ,
193- Label :: new ( self . file , self . span , message) ,
194- ) ,
219+ diagnostic : Diagnostic :: warning ( )
220+ . with_message ( message. clone ( ) )
221+ . with_labels ( vec ! [ Label :: new(
222+ LabelStyle :: Primary ,
223+ self . file,
224+ span_to_range( self . span) ,
225+ )
226+ . with_message( message) ] ) ,
195227 suggestions : vec ! [ ] ,
196228 }
197229 }
@@ -200,10 +232,14 @@ impl Word<'_> {
200232 pub ( crate ) fn error ( & self , message : impl Into < String > ) -> Warning {
201233 let message: String = message. into ( ) ;
202234 Warning {
203- diagnostic : Diagnostic :: new_error (
204- message. clone ( ) ,
205- Label :: new ( self . file , self . span , message) ,
206- ) ,
235+ diagnostic : Diagnostic :: error ( )
236+ . with_message ( message. clone ( ) )
237+ . with_labels ( vec ! [ Label :: new(
238+ LabelStyle :: Primary ,
239+ self . file,
240+ span_to_range( self . span) ,
241+ )
242+ . with_message( message) ] ) ,
207243 suggestions : vec ! [ ] ,
208244 }
209245 }
@@ -215,10 +251,14 @@ impl Atom<'_> {
215251 pub ( crate ) fn warning ( & self , message : impl Into < String > ) -> Warning {
216252 let message: String = message. into ( ) ;
217253 Warning {
218- diagnostic : Diagnostic :: new_warning (
219- message. clone ( ) ,
220- Label :: new ( self . file , self . span , message) ,
221- ) ,
254+ diagnostic : Diagnostic :: warning ( )
255+ . with_message ( message. clone ( ) )
256+ . with_labels ( vec ! [ Label :: new(
257+ LabelStyle :: Primary ,
258+ self . file,
259+ span_to_range( self . span) ,
260+ )
261+ . with_message( message) ] ) ,
222262 suggestions : vec ! [ ] ,
223263 }
224264 }
@@ -227,10 +267,14 @@ impl Atom<'_> {
227267 pub ( crate ) fn error ( & self , message : impl Into < String > ) -> Warning {
228268 let message: String = message. into ( ) ;
229269 Warning {
230- diagnostic : Diagnostic :: new_error (
231- message. clone ( ) ,
232- Label :: new ( self . file , self . span , message) ,
233- ) ,
270+ diagnostic : Diagnostic :: error ( )
271+ . with_message ( message. clone ( ) )
272+ . with_labels ( vec ! [ Label :: new(
273+ LabelStyle :: Primary ,
274+ self . file,
275+ span_to_range( self . span) ,
276+ )
277+ . with_message( message) ] ) ,
234278 suggestions : vec ! [ ] ,
235279 }
236280 }
0 commit comments