@@ -3,7 +3,6 @@ use std::fs::File;
33use std:: io:: BufReader ;
44use std:: io:: prelude:: * ;
55use std:: path:: Path ;
6- use std:: str:: FromStr ;
76use std:: sync:: OnceLock ;
87
98use regex:: Regex ;
@@ -18,30 +17,39 @@ pub enum ErrorKind {
1817 Warning ,
1918}
2019
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}`" ) ,
3328 }
3429 }
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+ }
3543}
3644
3745impl fmt:: Display for ErrorKind {
3846 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
3947 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 " ) ,
4553 }
4654 }
4755}
@@ -64,7 +72,7 @@ impl Error {
6472 use colored:: Colorize ;
6573 format ! (
6674 "{: <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( ) ,
6876 self . line_num_str( ) ,
6977 self . msg. cyan( ) ,
7078 )
@@ -154,18 +162,12 @@ fn parse_expected(
154162 }
155163
156164 // 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 ( ) ;
169171
170172 let line_num_adjust = & captures[ "adjust" ] ;
171173 let ( follow_prev, line_num) = if line_num_adjust == "|" {
@@ -181,7 +183,7 @@ fn parse_expected(
181183 debug ! (
182184 "line={:?} tag={:?} follow_prev={:?} kind={:?} msg={:?}" ,
183185 line_num,
184- whole_match . as_str( ) ,
186+ tag . as_str( ) ,
185187 follow_prev,
186188 kind,
187189 msg
0 commit comments