Skip to content

Commit f52a87c

Browse files
Centralize error spaning and add an enum to make this treatment easier
1 parent cbf0b1b commit f52a87c

File tree

2 files changed

+80
-59
lines changed

2 files changed

+80
-59
lines changed

src/librustc_resolve/lib.rs

Lines changed: 77 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,37 @@ mod record_exports;
116116
mod build_reduced_graph;
117117
mod resolve_imports;
118118

119-
fn resolve_err_417<'a, 'tcx>(this: &Resolver<'a, 'tcx>, span: syntax::codemap::Span, formatted: &str) {
120-
resolve_err!(this, span, E0417, "{}", formatted);
119+
pub enum ResolutionError<'b, 'a:'b, 'tcx:'a> {
120+
/// error: static variables cannot be referenced in a pattern
121+
StaticVariableReference(&'b Resolver<'a, 'tcx>, syntax::codemap::Span),
122+
/// error: does not name a struct
123+
DoesNotNameAStruct(&'b Resolver<'a, 'tcx>, syntax::codemap::Span),
124+
/// error: is a struct variant name, but this expression uses it like a function name
125+
StructVariantUsedAsFunction(&'a Resolver<'a, 'tcx>, syntax::codemap::Span),
126+
/// error: unresolved import
127+
UnresolvedImport(&'b Resolver<'a, 'tcx>, syntax::codemap::Span),
128+
/// error: failed to resolve
129+
FailedToResolve(&'b Resolver<'a, 'tcx>, syntax::codemap::Span),
121130
}
122131

123-
fn resolve_err_422<'a, 'tcx>(this: &Resolver<'a, 'tcx>, span: syntax::codemap::Span, formatted: &str) {
124-
resolve_err!(this, span, E0422, "{}", formatted);
125-
}
126-
127-
fn resolve_err_423<'a, 'tcx>(this: &Resolver<'a, 'tcx>, span: syntax::codemap::Span, formatted: &str) {
128-
resolve_err!(this, span, E0423, "{}", formatted);
129-
}
130-
131-
fn resolve_err_432<'a, 'tcx>(this: &Resolver<'a, 'tcx>, span: syntax::codemap::Span, formatted: &str) {
132-
resolve_err!(this, span, E0432, "{}", formatted);
133-
}
134-
135-
fn resolve_err_433<'a, 'tcx>(this: &Resolver<'a, 'tcx>, span: syntax::codemap::Span, formatted: &str) {
136-
resolve_err!(this, span, E0433, "{}", formatted);
132+
fn resolve_error<'b, 'a:'b, 'tcx:'a>(resolution_error: &ResolutionError<'b, 'a, 'tcx>, formatted: &str) {
133+
match resolution_error {
134+
&ResolutionError::StaticVariableReference(resolver, span) => {
135+
resolve_err!(resolver, span, E0417, "{}", formatted);
136+
},
137+
&ResolutionError::DoesNotNameAStruct(resolver, span) => {
138+
resolve_err!(resolver, span, E0422, "{}", formatted);
139+
},
140+
&ResolutionError::StructVariantUsedAsFunction(resolver, span) => {
141+
resolve_err!(resolver, span, E0423, "{}", formatted);
142+
},
143+
&ResolutionError::UnresolvedImport(resolver, span) => {
144+
resolve_err!(resolver, span, E0432, "{}", formatted);
145+
},
146+
&ResolutionError::FailedToResolve(resolver, span) => {
147+
resolve_err!(resolver, span, E0433, "{}", formatted);
148+
},
149+
}
137150
}
138151

139152
#[derive(Copy, Clone)]
@@ -1330,7 +1343,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13301343
PathSearch,
13311344
true) {
13321345
Failed(Some((span, msg))) => {
1333-
resolve_err_433(self, span, &*format!("failed to resolve. {}", msg));
1346+
resolve_error(&ResolutionError::FailedToResolve(self, span),
1347+
&*format!("failed to resolve. {}",
1348+
msg)
1349+
);
13341350
},
13351351
Failed(None) => (), // Continue up the search chain.
13361352
Indeterminate => {
@@ -1588,12 +1604,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15881604
.span_to_snippet((*imports)[index].span)
15891605
.unwrap();
15901606
if sn.contains("::") {
1591-
resolve_err_432(self, (*imports)[index].span, "unresolved import");
1607+
resolve_error(&ResolutionError::UnresolvedImport(self, (*imports)[index].span),
1608+
"unresolved import");
15921609
} else {
1593-
resolve_err_432(self, (*imports)[index].span,
1594-
&*format!("unresolved import (maybe you meant `{}::*`?)",
1595-
sn)
1596-
);
1610+
resolve_error(&ResolutionError::UnresolvedImport(self, (*imports)[index].span),
1611+
&*format!("unresolved import (maybe you meant `{}::*`?)",
1612+
sn)
1613+
);
15971614
}
15981615
}
15991616

@@ -2549,10 +2566,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
25492566
self.record_def(pattern.id, path_res);
25502567
}
25512568
DefStatic(..) => {
2552-
resolve_err_417(self, path.span,
2553-
"static variables cannot be \
2554-
referenced in a pattern, \
2555-
use a `const` instead");
2569+
resolve_error(&ResolutionError::StaticVariableReference(&self, path.span),
2570+
"static variables cannot be \
2571+
referenced in a pattern, \
2572+
use a `const` instead");
25562573
}
25572574
_ => {
25582575
// If anything ends up here entirely resolved,
@@ -2630,7 +2647,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
26302647
result => {
26312648
debug!("(resolving pattern) didn't find struct \
26322649
def: {:?}", result);
2633-
resolve_err_422(self, path.span,
2650+
resolve_error(&ResolutionError::DoesNotNameAStruct(self, path.span),
26342651
&*format!("`{}` does not name a structure",
26352652
path_names_to_string(path, 0)));
26362653
}
@@ -2678,10 +2695,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
26782695
return FoundConst(def, LastMod(AllPublic));
26792696
}
26802697
DefStatic(..) => {
2681-
resolve_err_417(self, span,
2682-
"static variables cannot be \
2683-
referenced in a pattern, \
2684-
use a `const` instead");
2698+
resolve_error(&ResolutionError::StaticVariableReference(self, span),
2699+
"static variables cannot be \
2700+
referenced in a pattern, \
2701+
use a `const` instead");
26852702
return BareIdentifierPatternUnresolved;
26862703
}
26872704
_ => {
@@ -2698,9 +2715,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
26982715
Failed(err) => {
26992716
match err {
27002717
Some((span, msg)) => {
2701-
resolve_err_433(self, span,
2702-
&*format!("failed to resolve: {}",
2703-
msg));
2718+
resolve_error(&ResolutionError::FailedToResolve(self, span),
2719+
&*format!("failed to resolve. {}",
2720+
msg)
2721+
);
27042722
}
27052723
None => ()
27062724
}
@@ -2929,9 +2947,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
29292947
}
29302948
};
29312949

2932-
resolve_err_433(self, span,
2933-
&*format!("failed to resolve: {}",
2934-
msg));
2950+
resolve_error(&ResolutionError::FailedToResolve(self, span),
2951+
&*format!("failed to resolve. {}",
2952+
msg)
2953+
);
29352954
return None;
29362955
}
29372956
Indeterminate => panic!("indeterminate unexpected"),
@@ -2990,11 +3009,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
29903009
}
29913010
};
29923011

2993-
/*self.resolve_error(span, &format!("failed to resolve. {}",
2994-
msg));*/
2995-
resolve_err_433(self, span,
2996-
&*format!("failed to resolve: {}",
2997-
msg));
3012+
resolve_error(&ResolutionError::FailedToResolve(self, span),
3013+
&*format!("failed to resolve. {}",
3014+
msg)
3015+
);
29983016
return None;
29993017
}
30003018

@@ -3090,9 +3108,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
30903108
failed to resolve {}", name);
30913109

30923110
if let Some((span, msg)) = err {
3093-
resolve_err_433(self, span,
3094-
&*format!("failed to resolve: {}",
3095-
msg))
3111+
resolve_error(&ResolutionError::FailedToResolve(self, span),
3112+
&*format!("failed to resolve. {}",
3113+
msg)
3114+
)
30963115
}
30973116

30983117
return None;
@@ -3294,11 +3313,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
32943313
// Check if struct variant
32953314
if let DefVariant(_, _, true) = path_res.base_def {
32963315
let path_name = path_names_to_string(path, 0);
3297-
resolve_err_423(self, expr.span,
3298-
&*format!("`{}` is a struct variant name, but \
3299-
this expression \
3300-
uses it like a function name",
3301-
path_name));
3316+
3317+
resolve_error(&ResolutionError::StructVariantUsedAsFunction(self, expr.span),
3318+
&*format!("`{}` is a struct variant name, but \
3319+
this expression \
3320+
uses it like a function name",
3321+
path_name));
33023322

33033323
let msg = format!("did you mean to write: \
33043324
`{} {{ /* fields */ }}`?",
@@ -3335,11 +3355,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
33353355
match type_res.map(|r| r.base_def) {
33363356
Some(DefTy(struct_id, _))
33373357
if self.structs.contains_key(&struct_id) => {
3338-
resolve_err_423(self, expr.span,
3339-
&*format!("{}` is a structure name, but \
3340-
this expression \
3341-
uses it like a function name",
3342-
path_name));
3358+
resolve_error(&ResolutionError::StructVariantUsedAsFunction(self, expr.span),
3359+
&*format!("`{}` is a struct variant name, but \
3360+
this expression \
3361+
uses it like a function name",
3362+
path_name));
33433363

33443364
let msg = format!("did you mean to write: \
33453365
`{} {{ /* fields */ }}`?",
@@ -3414,7 +3434,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
34143434
Some(definition) => self.record_def(expr.id, definition),
34153435
None => {
34163436
debug!("(resolving expression) didn't find struct def",);
3417-
resolve_err_422(self, path.span,
3437+
3438+
resolve_error(&ResolutionError::DoesNotNameAStruct(self, path.span),
34183439
&*format!("`{}` does not name a structure",
34193440
path_names_to_string(path, 0)));
34203441
}

src/librustc_resolve/resolve_imports.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,13 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
272272
Some((span, msg)) => (span, format!(". {}", msg)),
273273
None => (import_directive.span, String::new())
274274
};
275-
::resolve_err_432(self.resolver, span,
276-
&*format!("unresolved import `{}`{}",
275+
::resolve_error(&::ResolutionError::UnresolvedImport(self.resolver, span),
276+
&*format!("unresolved import `{}`{}",
277277
import_path_to_string(
278278
&import_directive.module_path,
279279
import_directive.subclass),
280280
help)
281-
);
281+
);
282282
}
283283
ResolveResult::Indeterminate => break, // Bail out. We'll come around next time.
284284
ResolveResult::Success(()) => () // Good. Continue.

0 commit comments

Comments
 (0)