@@ -3,42 +3,40 @@ use std::borrow::Cow;
3
3
use rustc_ast:: token:: { self , Token , TokenKind } ;
4
4
use rustc_ast:: tokenstream:: TokenStream ;
5
5
use rustc_ast_pretty:: pprust;
6
- use rustc_errors:: { Applicability , Diag , DiagMessage } ;
6
+ use rustc_errors:: { Applicability , Diag , DiagCtxtHandle , DiagMessage } ;
7
7
use rustc_macros:: Subdiagnostic ;
8
8
use rustc_parse:: parser:: { Parser , Recovery } ;
9
+ use rustc_session:: parse:: ParseSess ;
9
10
use rustc_span:: source_map:: SourceMap ;
10
11
use rustc_span:: symbol:: Ident ;
11
12
use rustc_span:: { ErrorGuaranteed , Span } ;
12
13
use tracing:: debug;
13
14
14
15
use super :: macro_rules:: { parser_from_cx, NoopTracker } ;
15
- use crate :: base:: { DummyResult , ExtCtxt , MacResult } ;
16
16
use crate :: expand:: { parse_ast_fragment, AstFragmentKind } ;
17
17
use crate :: mbe:: macro_parser:: ParseResult :: * ;
18
18
use crate :: mbe:: macro_parser:: { MatcherLoc , NamedParseResult , TtParser } ;
19
19
use crate :: mbe:: macro_rules:: { try_match_macro, Tracker } ;
20
20
21
- pub ( super ) fn failed_to_match_macro < ' cx > (
22
- cx : & ' cx mut ExtCtxt < ' _ > ,
21
+ pub ( super ) fn failed_to_match_macro (
22
+ psess : & ParseSess ,
23
23
sp : Span ,
24
24
def_span : Span ,
25
25
name : Ident ,
26
26
arg : TokenStream ,
27
27
lhses : & [ Vec < MatcherLoc > ] ,
28
- ) -> Box < dyn MacResult + ' cx > {
29
- let psess = & cx. sess . psess ;
30
-
28
+ ) -> ( Span , ErrorGuaranteed ) {
31
29
// An error occurred, try the expansion again, tracking the expansion closely for better
32
30
// diagnostics.
33
- let mut tracker = CollectTrackerAndEmitter :: new ( cx , sp) ;
31
+ let mut tracker = CollectTrackerAndEmitter :: new ( psess . dcx ( ) , sp) ;
34
32
35
33
let try_success_result = try_match_macro ( psess, name, & arg, lhses, & mut tracker) ;
36
34
37
35
if try_success_result. is_ok ( ) {
38
36
// Nonterminal parser recovery might turn failed matches into successful ones,
39
37
// but for that it must have emitted an error already
40
38
assert ! (
41
- tracker. cx . dcx( ) . has_errors( ) . is_some( ) ,
39
+ tracker. dcx. has_errors( ) . is_some( ) ,
42
40
"Macro matching returned a success on the second try"
43
41
) ;
44
42
}
@@ -50,15 +48,15 @@ pub(super) fn failed_to_match_macro<'cx>(
50
48
51
49
let Some ( BestFailure { token, msg : label, remaining_matcher, .. } ) = tracker. best_failure
52
50
else {
53
- return DummyResult :: any ( sp, cx . dcx ( ) . span_delayed_bug ( sp, "failed to match a macro" ) ) ;
51
+ return ( sp, psess . dcx ( ) . span_delayed_bug ( sp, "failed to match a macro" ) ) ;
54
52
} ;
55
53
56
54
let span = token. span . substitute_dummy ( sp) ;
57
55
58
- let mut err = cx . dcx ( ) . struct_span_err ( span, parse_failure_msg ( & token, None ) ) ;
56
+ let mut err = psess . dcx ( ) . struct_span_err ( span, parse_failure_msg ( & token, None ) ) ;
59
57
err. span_label ( span, label) ;
60
- if !def_span. is_dummy ( ) && !cx . source_map ( ) . is_imported ( def_span) {
61
- err. span_label ( cx . source_map ( ) . guess_head_span ( def_span) , "when calling this macro" ) ;
58
+ if !def_span. is_dummy ( ) && !psess . source_map ( ) . is_imported ( def_span) {
59
+ err. span_label ( psess . source_map ( ) . guess_head_span ( def_span) , "when calling this macro" ) ;
62
60
}
63
61
64
62
annotate_doc_comment ( & mut err, psess. source_map ( ) , span) ;
@@ -76,7 +74,7 @@ pub(super) fn failed_to_match_macro<'cx>(
76
74
err. note ( "captured metavariables except for `:tt`, `:ident` and `:lifetime` cannot be compared to other tokens" ) ;
77
75
err. note ( "see <https://doc.rust-lang.org/nightly/reference/macros-by-example.html#forwarding-a-matched-fragment> for more information" ) ;
78
76
79
- if !def_span. is_dummy ( ) && !cx . source_map ( ) . is_imported ( def_span) {
77
+ if !def_span. is_dummy ( ) && !psess . source_map ( ) . is_imported ( def_span) {
80
78
err. help ( "try using `:tt` instead in the macro definition" ) ;
81
79
}
82
80
}
@@ -104,18 +102,17 @@ pub(super) fn failed_to_match_macro<'cx>(
104
102
}
105
103
}
106
104
let guar = err. emit ( ) ;
107
- cx. trace_macros_diag ( ) ;
108
- DummyResult :: any ( sp, guar)
105
+ ( sp, guar)
109
106
}
110
107
111
108
/// The tracker used for the slow error path that collects useful info for diagnostics.
112
- struct CollectTrackerAndEmitter < ' a , ' cx , ' matcher > {
113
- cx : & ' a mut ExtCtxt < ' cx > ,
109
+ struct CollectTrackerAndEmitter < ' dcx , ' matcher > {
110
+ dcx : DiagCtxtHandle < ' dcx > ,
114
111
remaining_matcher : Option < & ' matcher MatcherLoc > ,
115
112
/// Which arm's failure should we report? (the one furthest along)
116
113
best_failure : Option < BestFailure > ,
117
114
root_span : Span ,
118
- result : Option < Box < dyn MacResult + ' cx > > ,
115
+ result : Option < ( Span , ErrorGuaranteed ) > ,
119
116
}
120
117
121
118
struct BestFailure {
@@ -131,7 +128,7 @@ impl BestFailure {
131
128
}
132
129
}
133
130
134
- impl < ' a , ' cx , ' matcher > Tracker < ' matcher > for CollectTrackerAndEmitter < ' a , ' cx , ' matcher > {
131
+ impl < ' dcx , ' matcher > Tracker < ' matcher > for CollectTrackerAndEmitter < ' dcx , ' matcher > {
135
132
type Failure = ( Token , u32 , & ' static str ) ;
136
133
137
134
fn build_failure ( tok : Token , position : u32 , msg : & ' static str ) -> Self :: Failure {
@@ -151,7 +148,7 @@ impl<'a, 'cx, 'matcher> Tracker<'matcher> for CollectTrackerAndEmitter<'a, 'cx,
151
148
Success ( _) => {
152
149
// Nonterminal parser recovery might turn failed matches into successful ones,
153
150
// but for that it must have emitted an error already
154
- self . cx . dcx ( ) . span_delayed_bug (
151
+ self . dcx . span_delayed_bug (
155
152
self . root_span ,
156
153
"should not collect detailed info for successful macro match" ,
157
154
) ;
@@ -177,10 +174,10 @@ impl<'a, 'cx, 'matcher> Tracker<'matcher> for CollectTrackerAndEmitter<'a, 'cx,
177
174
}
178
175
Error ( err_sp, msg) => {
179
176
let span = err_sp. substitute_dummy ( self . root_span ) ;
180
- let guar = self . cx . dcx ( ) . span_err ( span, msg. clone ( ) ) ;
181
- self . result = Some ( DummyResult :: any ( span, guar) ) ;
177
+ let guar = self . dcx . span_err ( span, msg. clone ( ) ) ;
178
+ self . result = Some ( ( span, guar) ) ;
182
179
}
183
- ErrorReported ( guar) => self . result = Some ( DummyResult :: any ( self . root_span , * guar) ) ,
180
+ ErrorReported ( guar) => self . result = Some ( ( self . root_span , * guar) ) ,
184
181
}
185
182
}
186
183
@@ -193,9 +190,9 @@ impl<'a, 'cx, 'matcher> Tracker<'matcher> for CollectTrackerAndEmitter<'a, 'cx,
193
190
}
194
191
}
195
192
196
- impl < ' a , ' cx > CollectTrackerAndEmitter < ' a , ' cx , ' _ > {
197
- fn new ( cx : & ' a mut ExtCtxt < ' cx > , root_span : Span ) -> Self {
198
- Self { cx , remaining_matcher : None , best_failure : None , root_span, result : None }
193
+ impl < ' dcx > CollectTrackerAndEmitter < ' dcx , ' _ > {
194
+ fn new ( dcx : DiagCtxtHandle < ' dcx > , root_span : Span ) -> Self {
195
+ Self { dcx , remaining_matcher : None , best_failure : None , root_span, result : None }
199
196
}
200
197
}
201
198
0 commit comments