1
Fork 0

Centralize error spaning and add an enum to make this treatment easier

This commit is contained in:
Guillaume Gomez 2015-07-13 19:32:45 +02:00
parent cbf0b1b399
commit f52a87c44e
2 changed files with 80 additions and 59 deletions

View file

@ -116,24 +116,37 @@ mod record_exports;
mod build_reduced_graph; mod build_reduced_graph;
mod resolve_imports; mod resolve_imports;
fn resolve_err_417<'a, 'tcx>(this: &Resolver<'a, 'tcx>, span: syntax::codemap::Span, formatted: &str) { pub enum ResolutionError<'b, 'a:'b, 'tcx:'a> {
resolve_err!(this, span, E0417, "{}", formatted); /// error: static variables cannot be referenced in a pattern
StaticVariableReference(&'b Resolver<'a, 'tcx>, syntax::codemap::Span),
/// error: does not name a struct
DoesNotNameAStruct(&'b Resolver<'a, 'tcx>, syntax::codemap::Span),
/// error: is a struct variant name, but this expression uses it like a function name
StructVariantUsedAsFunction(&'a Resolver<'a, 'tcx>, syntax::codemap::Span),
/// error: unresolved import
UnresolvedImport(&'b Resolver<'a, 'tcx>, syntax::codemap::Span),
/// error: failed to resolve
FailedToResolve(&'b Resolver<'a, 'tcx>, syntax::codemap::Span),
} }
fn resolve_err_422<'a, 'tcx>(this: &Resolver<'a, 'tcx>, span: syntax::codemap::Span, formatted: &str) { fn resolve_error<'b, 'a:'b, 'tcx:'a>(resolution_error: &ResolutionError<'b, 'a, 'tcx>, formatted: &str) {
resolve_err!(this, span, E0422, "{}", formatted); match resolution_error {
} &ResolutionError::StaticVariableReference(resolver, span) => {
resolve_err!(resolver, span, E0417, "{}", formatted);
fn resolve_err_423<'a, 'tcx>(this: &Resolver<'a, 'tcx>, span: syntax::codemap::Span, formatted: &str) { },
resolve_err!(this, span, E0423, "{}", formatted); &ResolutionError::DoesNotNameAStruct(resolver, span) => {
} resolve_err!(resolver, span, E0422, "{}", formatted);
},
fn resolve_err_432<'a, 'tcx>(this: &Resolver<'a, 'tcx>, span: syntax::codemap::Span, formatted: &str) { &ResolutionError::StructVariantUsedAsFunction(resolver, span) => {
resolve_err!(this, span, E0432, "{}", formatted); resolve_err!(resolver, span, E0423, "{}", formatted);
} },
&ResolutionError::UnresolvedImport(resolver, span) => {
fn resolve_err_433<'a, 'tcx>(this: &Resolver<'a, 'tcx>, span: syntax::codemap::Span, formatted: &str) { resolve_err!(resolver, span, E0432, "{}", formatted);
resolve_err!(this, span, E0433, "{}", formatted); },
&ResolutionError::FailedToResolve(resolver, span) => {
resolve_err!(resolver, span, E0433, "{}", formatted);
},
}
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
@ -1330,7 +1343,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
PathSearch, PathSearch,
true) { true) {
Failed(Some((span, msg))) => { Failed(Some((span, msg))) => {
resolve_err_433(self, span, &*format!("failed to resolve. {}", msg)); resolve_error(&ResolutionError::FailedToResolve(self, span),
&*format!("failed to resolve. {}",
msg)
);
}, },
Failed(None) => (), // Continue up the search chain. Failed(None) => (), // Continue up the search chain.
Indeterminate => { Indeterminate => {
@ -1588,12 +1604,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
.span_to_snippet((*imports)[index].span) .span_to_snippet((*imports)[index].span)
.unwrap(); .unwrap();
if sn.contains("::") { if sn.contains("::") {
resolve_err_432(self, (*imports)[index].span, "unresolved import"); resolve_error(&ResolutionError::UnresolvedImport(self, (*imports)[index].span),
"unresolved import");
} else { } else {
resolve_err_432(self, (*imports)[index].span, resolve_error(&ResolutionError::UnresolvedImport(self, (*imports)[index].span),
&*format!("unresolved import (maybe you meant `{}::*`?)", &*format!("unresolved import (maybe you meant `{}::*`?)",
sn) sn)
); );
} }
} }
@ -2549,10 +2566,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
self.record_def(pattern.id, path_res); self.record_def(pattern.id, path_res);
} }
DefStatic(..) => { DefStatic(..) => {
resolve_err_417(self, path.span, resolve_error(&ResolutionError::StaticVariableReference(&self, path.span),
"static variables cannot be \ "static variables cannot be \
referenced in a pattern, \ referenced in a pattern, \
use a `const` instead"); use a `const` instead");
} }
_ => { _ => {
// If anything ends up here entirely resolved, // If anything ends up here entirely resolved,
@ -2630,7 +2647,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
result => { result => {
debug!("(resolving pattern) didn't find struct \ debug!("(resolving pattern) didn't find struct \
def: {:?}", result); def: {:?}", result);
resolve_err_422(self, path.span, resolve_error(&ResolutionError::DoesNotNameAStruct(self, path.span),
&*format!("`{}` does not name a structure", &*format!("`{}` does not name a structure",
path_names_to_string(path, 0))); path_names_to_string(path, 0)));
} }
@ -2678,10 +2695,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
return FoundConst(def, LastMod(AllPublic)); return FoundConst(def, LastMod(AllPublic));
} }
DefStatic(..) => { DefStatic(..) => {
resolve_err_417(self, span, resolve_error(&ResolutionError::StaticVariableReference(self, span),
"static variables cannot be \ "static variables cannot be \
referenced in a pattern, \ referenced in a pattern, \
use a `const` instead"); use a `const` instead");
return BareIdentifierPatternUnresolved; return BareIdentifierPatternUnresolved;
} }
_ => { _ => {
@ -2698,9 +2715,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
Failed(err) => { Failed(err) => {
match err { match err {
Some((span, msg)) => { Some((span, msg)) => {
resolve_err_433(self, span, resolve_error(&ResolutionError::FailedToResolve(self, span),
&*format!("failed to resolve: {}", &*format!("failed to resolve. {}",
msg)); msg)
);
} }
None => () None => ()
} }
@ -2929,9 +2947,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
} }
}; };
resolve_err_433(self, span, resolve_error(&ResolutionError::FailedToResolve(self, span),
&*format!("failed to resolve: {}", &*format!("failed to resolve. {}",
msg)); msg)
);
return None; return None;
} }
Indeterminate => panic!("indeterminate unexpected"), Indeterminate => panic!("indeterminate unexpected"),
@ -2990,11 +3009,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
} }
}; };
/*self.resolve_error(span, &format!("failed to resolve. {}", resolve_error(&ResolutionError::FailedToResolve(self, span),
msg));*/ &*format!("failed to resolve. {}",
resolve_err_433(self, span, msg)
&*format!("failed to resolve: {}", );
msg));
return None; return None;
} }
@ -3090,9 +3108,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
failed to resolve {}", name); failed to resolve {}", name);
if let Some((span, msg)) = err { if let Some((span, msg)) = err {
resolve_err_433(self, span, resolve_error(&ResolutionError::FailedToResolve(self, span),
&*format!("failed to resolve: {}", &*format!("failed to resolve. {}",
msg)) msg)
)
} }
return None; return None;
@ -3294,11 +3313,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
// Check if struct variant // Check if struct variant
if let DefVariant(_, _, true) = path_res.base_def { if let DefVariant(_, _, true) = path_res.base_def {
let path_name = path_names_to_string(path, 0); let path_name = path_names_to_string(path, 0);
resolve_err_423(self, expr.span,
&*format!("`{}` is a struct variant name, but \ resolve_error(&ResolutionError::StructVariantUsedAsFunction(self, expr.span),
this expression \ &*format!("`{}` is a struct variant name, but \
uses it like a function name", this expression \
path_name)); uses it like a function name",
path_name));
let msg = format!("did you mean to write: \ let msg = format!("did you mean to write: \
`{} {{ /* fields */ }}`?", `{} {{ /* fields */ }}`?",
@ -3335,11 +3355,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
match type_res.map(|r| r.base_def) { match type_res.map(|r| r.base_def) {
Some(DefTy(struct_id, _)) Some(DefTy(struct_id, _))
if self.structs.contains_key(&struct_id) => { if self.structs.contains_key(&struct_id) => {
resolve_err_423(self, expr.span, resolve_error(&ResolutionError::StructVariantUsedAsFunction(self, expr.span),
&*format!("{}` is a structure name, but \ &*format!("`{}` is a struct variant name, but \
this expression \ this expression \
uses it like a function name", uses it like a function name",
path_name)); path_name));
let msg = format!("did you mean to write: \ let msg = format!("did you mean to write: \
`{} {{ /* fields */ }}`?", `{} {{ /* fields */ }}`?",
@ -3414,7 +3434,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
Some(definition) => self.record_def(expr.id, definition), Some(definition) => self.record_def(expr.id, definition),
None => { None => {
debug!("(resolving expression) didn't find struct def",); debug!("(resolving expression) didn't find struct def",);
resolve_err_422(self, path.span,
resolve_error(&ResolutionError::DoesNotNameAStruct(self, path.span),
&*format!("`{}` does not name a structure", &*format!("`{}` does not name a structure",
path_names_to_string(path, 0))); path_names_to_string(path, 0)));
} }

View file

@ -272,13 +272,13 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
Some((span, msg)) => (span, format!(". {}", msg)), Some((span, msg)) => (span, format!(". {}", msg)),
None => (import_directive.span, String::new()) None => (import_directive.span, String::new())
}; };
::resolve_err_432(self.resolver, span, ::resolve_error(&::ResolutionError::UnresolvedImport(self.resolver, span),
&*format!("unresolved import `{}`{}", &*format!("unresolved import `{}`{}",
import_path_to_string( import_path_to_string(
&import_directive.module_path, &import_directive.module_path,
import_directive.subclass), import_directive.subclass),
help) help)
); );
} }
ResolveResult::Indeterminate => break, // Bail out. We'll come around next time. ResolveResult::Indeterminate => break, // Bail out. We'll come around next time.
ResolveResult::Success(()) => () // Good. Continue. ResolveResult::Success(()) => () // Good. Continue.