@@ -3,7 +3,6 @@ use std::fs::File;
3
3
use std:: io:: BufReader ;
4
4
use std:: io:: prelude:: * ;
5
5
use std:: path:: Path ;
6
- use std:: str:: FromStr ;
7
6
use std:: sync:: OnceLock ;
8
7
9
8
use regex:: Regex ;
@@ -18,30 +17,39 @@ pub enum ErrorKind {
18
17
Warning ,
19
18
}
20
19
21
- impl FromStr for ErrorKind {
22
- type Err = ( ) ;
23
- fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
24
- let s = s. to_uppercase ( ) ;
25
- let part0: & str = s. split ( ':' ) . next ( ) . unwrap ( ) ;
26
- match part0 {
27
- "HELP" => Ok ( ErrorKind :: Help ) ,
28
- "ERROR" => Ok ( ErrorKind :: Error ) ,
29
- "NOTE" => Ok ( ErrorKind :: Note ) ,
30
- "SUGGESTION" => Ok ( ErrorKind :: Suggestion ) ,
31
- "WARN" | "WARNING" => Ok ( ErrorKind :: Warning ) ,
32
- _ => Err ( ( ) ) ,
20
+ impl ErrorKind {
21
+ pub fn from_compiler_str ( s : & str ) -> ErrorKind {
22
+ match s {
23
+ "help" => ErrorKind :: Help ,
24
+ "error" | "error: internal compiler error" => ErrorKind :: Error ,
25
+ "note" | "failure-note" => ErrorKind :: Note ,
26
+ "warning" => ErrorKind :: Warning ,
27
+ _ => panic ! ( "unexpected compiler diagnostic kind `{s}`" ) ,
33
28
}
34
29
}
30
+
31
+ /// Either the canonical uppercase string, or some additional versions for compatibility.
32
+ /// FIXME: consider keeping only the canonical versions here.
33
+ fn from_user_str ( s : & str ) -> Option < ErrorKind > {
34
+ Some ( match s {
35
+ "HELP" | "help" => ErrorKind :: Help ,
36
+ "ERROR" | "error" => ErrorKind :: Error ,
37
+ "NOTE" | "note" => ErrorKind :: Note ,
38
+ "SUGGESTION" => ErrorKind :: Suggestion ,
39
+ "WARN" | "WARNING" | "warn" | "warning" => ErrorKind :: Warning ,
40
+ _ => return None ,
41
+ } )
42
+ }
35
43
}
36
44
37
45
impl fmt:: Display for ErrorKind {
38
46
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
39
47
match * self {
40
- ErrorKind :: Help => write ! ( f, "help message " ) ,
41
- ErrorKind :: Error => write ! ( f, "error " ) ,
42
- ErrorKind :: Note => write ! ( f, "note " ) ,
43
- ErrorKind :: Suggestion => write ! ( f, "suggestion " ) ,
44
- ErrorKind :: Warning => write ! ( f, "warning " ) ,
48
+ ErrorKind :: Help => write ! ( f, "HELP " ) ,
49
+ ErrorKind :: Error => write ! ( f, "ERROR " ) ,
50
+ ErrorKind :: Note => write ! ( f, "NOTE " ) ,
51
+ ErrorKind :: Suggestion => write ! ( f, "SUGGESTION " ) ,
52
+ ErrorKind :: Warning => write ! ( f, "WARN " ) ,
45
53
}
46
54
}
47
55
}
@@ -64,7 +72,7 @@ impl Error {
64
72
use colored:: Colorize ;
65
73
format ! (
66
74
"{: <10}line {: >3}: {}" ,
67
- self . kind. map( |kind| kind. to_string( ) ) . unwrap_or_default( ) . to_uppercase ( ) ,
75
+ self . kind. map( |kind| kind. to_string( ) ) . unwrap_or_default( ) ,
68
76
self . line_num_str( ) ,
69
77
self . msg. cyan( ) ,
70
78
)
@@ -154,18 +162,12 @@ fn parse_expected(
154
162
}
155
163
156
164
// Get the part of the comment after the sigil (e.g. `~^^` or ~|).
157
- let whole_match = captures. get ( 0 ) . unwrap ( ) ;
158
- let ( _, mut msg) = line. split_at ( whole_match. end ( ) ) ;
159
-
160
- let first_word = msg. split_whitespace ( ) . next ( ) . expect ( "Encountered unexpected empty comment" ) ;
161
-
162
- // If we find `//~ ERROR foo` or something like that, skip the first word.
163
- let kind = first_word. parse :: < ErrorKind > ( ) . ok ( ) ;
164
- if kind. is_some ( ) {
165
- msg = & msg. trim_start ( ) . split_at ( first_word. len ( ) ) . 1 ;
166
- }
167
-
168
- let msg = msg. trim ( ) . to_owned ( ) ;
165
+ let tag = captures. get ( 0 ) . unwrap ( ) ;
166
+ let rest = line[ tag. end ( ) ..] . trim_start ( ) ;
167
+ let ( kind_str, _) = rest. split_once ( |c : char | !c. is_ascii_alphabetic ( ) ) . unwrap_or ( ( rest, "" ) ) ;
168
+ let kind = ErrorKind :: from_user_str ( kind_str) ;
169
+ let untrimmed_msg = if kind. is_some ( ) { & rest[ kind_str. len ( ) ..] } else { rest } ;
170
+ let msg = untrimmed_msg. strip_prefix ( ':' ) . unwrap_or ( untrimmed_msg) . trim ( ) . to_owned ( ) ;
169
171
170
172
let line_num_adjust = & captures[ "adjust" ] ;
171
173
let ( follow_prev, line_num) = if line_num_adjust == "|" {
@@ -181,7 +183,7 @@ fn parse_expected(
181
183
debug ! (
182
184
"line={:?} tag={:?} follow_prev={:?} kind={:?} msg={:?}" ,
183
185
line_num,
184
- whole_match . as_str( ) ,
186
+ tag . as_str( ) ,
185
187
follow_prev,
186
188
kind,
187
189
msg
0 commit comments