Skip to content

Commit 65ec4df

Browse files
committed
Improve diagnostics for duplicate names
1 parent d5880ff commit 65ec4df

File tree

3 files changed

+63
-104
lines changed

3 files changed

+63
-104
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -105,36 +105,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
105105
/// otherwise, reports an error.
106106
fn define<T: ToNameBinding<'b>>(&self, parent: Module<'b>, name: Name, ns: Namespace, def: T) {
107107
let binding = def.to_name_binding();
108-
let old_binding = match parent.try_define_child(name, ns, binding.clone()) {
109-
Ok(()) => return,
110-
Err(old_binding) => old_binding,
111-
};
112-
113-
let span = binding.span.unwrap_or(DUMMY_SP);
114-
if !old_binding.is_extern_crate() && !binding.is_extern_crate() {
115-
// Record an error here by looking up the namespace that had the duplicate
116-
let ns_str = match ns { TypeNS => "type or module", ValueNS => "value" };
117-
let resolution_error = ResolutionError::DuplicateDefinition(ns_str, name);
118-
let mut err = resolve_struct_error(self, span, resolution_error);
119-
120-
if let Some(sp) = old_binding.span {
121-
let note = format!("first definition of {} `{}` here", ns_str, name);
122-
err.span_note(sp, &note);
123-
}
124-
err.emit();
125-
} else if old_binding.is_extern_crate() && binding.is_extern_crate() {
126-
span_err!(self.session,
127-
span,
128-
E0259,
129-
"an external crate named `{}` has already been imported into this module",
130-
name);
131-
} else {
132-
span_err!(self.session,
133-
span,
134-
E0260,
135-
"the name `{}` conflicts with an external crate \
136-
that has been imported into this module",
137-
name);
108+
if let Err(old_binding) = parent.try_define_child(name, ns, binding.clone()) {
109+
self.report_conflict(parent, name, ns, old_binding, &binding);
138110
}
139111
}
140112

src/librustc_resolve/lib.rs

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,6 @@ pub enum ResolutionError<'a> {
183183
UndeclaredLabel(&'a str),
184184
/// error E0427: cannot use `ref` binding mode with ...
185185
CannotUseRefBindingModeWith(&'a str),
186-
/// error E0428: duplicate definition
187-
DuplicateDefinition(&'a str, Name),
188186
/// error E0429: `self` imports are only allowed within a { } list
189187
SelfImportsOnlyAllowedWithin,
190188
/// error E0430: `self` import can only appear once in the list
@@ -490,14 +488,6 @@ fn resolve_struct_error<'b, 'a: 'b, 'tcx: 'a>(resolver: &'b Resolver<'a, 'tcx>,
490488
"cannot use `ref` binding mode with {}",
491489
descr)
492490
}
493-
ResolutionError::DuplicateDefinition(namespace, name) => {
494-
struct_span_err!(resolver.session,
495-
span,
496-
E0428,
497-
"duplicate definition of {} `{}`",
498-
namespace,
499-
name)
500-
}
501491
ResolutionError::SelfImportsOnlyAllowedWithin => {
502492
struct_span_err!(resolver.session,
503493
span,
@@ -3530,8 +3520,62 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
35303520
}
35313521
}
35323522
}
3533-
}
35343523

3524+
fn report_conflict(&self,
3525+
parent: Module,
3526+
name: Name,
3527+
ns: Namespace,
3528+
binding: &NameBinding,
3529+
old_binding: &NameBinding) {
3530+
// Error on the second of two conflicting names
3531+
if old_binding.span.unwrap().lo > binding.span.unwrap().lo {
3532+
return self.report_conflict(parent, name, ns, old_binding, binding);
3533+
}
3534+
3535+
let container = match parent.def {
3536+
Some(Def::Mod(_)) => "module",
3537+
Some(Def::Trait(_)) => "trait",
3538+
None => "block",
3539+
_ => "enum",
3540+
};
3541+
3542+
let (participle, noun) = match old_binding.is_import() || old_binding.is_extern_crate() {
3543+
true => ("imported", "import"),
3544+
false => ("defined", "definition"),
3545+
};
3546+
3547+
let span = binding.span.unwrap();
3548+
let msg = {
3549+
let kind = match (ns, old_binding.module()) {
3550+
(ValueNS, _) => "a value",
3551+
(TypeNS, Some(module)) if module.extern_crate_id.is_some() => "an extern crate",
3552+
(TypeNS, Some(module)) if module.is_normal() => "a module",
3553+
(TypeNS, Some(module)) if module.is_trait() => "a trait",
3554+
(TypeNS, _) => "a type",
3555+
};
3556+
format!("{} named `{}` has already been {} in this {}",
3557+
kind, name, participle, container)
3558+
};
3559+
3560+
let mut err = match (old_binding.is_extern_crate(), binding.is_extern_crate()) {
3561+
(true, true) => struct_span_err!(self.session, span, E0259, "{}", msg),
3562+
(true, _) | (_, true) if binding.is_import() || old_binding.is_import() =>
3563+
struct_span_err!(self.session, span, E0254, "{}", msg),
3564+
(true, _) | (_, true) => struct_span_err!(self.session, span, E0260, "{}", msg),
3565+
_ => match (old_binding.is_import(), binding.is_import()) {
3566+
(false, false) => struct_span_err!(self.session, span, E0428, "{}", msg),
3567+
(true, true) => struct_span_err!(self.session, span, E0252, "{}", msg),
3568+
_ => struct_span_err!(self.session, span, E0255, "{}", msg),
3569+
},
3570+
};
3571+
3572+
let span = old_binding.span.unwrap();
3573+
if span != codemap::DUMMY_SP {
3574+
err.span_note(span, &format!("previous {} of `{}` here", noun, name));
3575+
}
3576+
err.emit();
3577+
}
3578+
}
35353579

35363580
fn names_to_string(names: &[Name]) -> String {
35373581
let mut first = true;

src/librustc_resolve/resolve_imports.rs

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,8 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
513513
let imported_binding = directive.import(binding, privacy_error);
514514
let conflict = module_.try_define_child(target, ns, imported_binding);
515515
if let Err(old_binding) = conflict {
516-
self.report_conflict(target, ns, &directive.import(binding, None), old_binding);
516+
let binding = &directive.import(binding, None);
517+
self.resolver.report_conflict(module_, target, ns, binding, old_binding);
517518
}
518519
}
519520

@@ -650,67 +651,6 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
650651
return Success(());
651652
}
652653

653-
fn report_conflict(&mut self,
654-
name: Name,
655-
ns: Namespace,
656-
binding: &NameBinding,
657-
old_binding: &NameBinding) {
658-
// Error on the second of two conflicting imports
659-
if old_binding.is_import() && binding.is_import() &&
660-
old_binding.span.unwrap().lo > binding.span.unwrap().lo {
661-
self.report_conflict(name, ns, old_binding, binding);
662-
return;
663-
}
664-
665-
if old_binding.is_extern_crate() {
666-
let msg = format!("import `{0}` conflicts with imported crate \
667-
in this module (maybe you meant `use {0}::*`?)",
668-
name);
669-
span_err!(self.resolver.session, binding.span.unwrap(), E0254, "{}", &msg);
670-
} else if old_binding.is_import() {
671-
let ns_word = match (ns, old_binding.module()) {
672-
(ValueNS, _) => "value",
673-
(TypeNS, Some(module)) if module.is_normal() => "module",
674-
(TypeNS, Some(module)) if module.is_trait() => "trait",
675-
(TypeNS, _) => "type",
676-
};
677-
let mut err = struct_span_err!(self.resolver.session,
678-
binding.span.unwrap(),
679-
E0252,
680-
"a {} named `{}` has already been imported \
681-
in this module",
682-
ns_word,
683-
name);
684-
err.span_note(old_binding.span.unwrap(),
685-
&format!("previous import of `{}` here", name));
686-
err.emit();
687-
} else if ns == ValueNS { // Check for item conflicts in the value namespace
688-
let mut err = struct_span_err!(self.resolver.session,
689-
binding.span.unwrap(),
690-
E0255,
691-
"import `{}` conflicts with value in this module",
692-
name);
693-
err.span_note(old_binding.span.unwrap(), "conflicting value here");
694-
err.emit();
695-
} else { // Check for item conflicts in the type namespace
696-
let (what, note) = match old_binding.module() {
697-
Some(ref module) if module.is_normal() =>
698-
("existing submodule", "note conflicting module here"),
699-
Some(ref module) if module.is_trait() =>
700-
("trait in this module", "note conflicting trait here"),
701-
_ => ("type in this module", "note conflicting type here"),
702-
};
703-
let mut err = struct_span_err!(self.resolver.session,
704-
binding.span.unwrap(),
705-
E0256,
706-
"import `{}` conflicts with {}",
707-
name,
708-
what);
709-
err.span_note(old_binding.span.unwrap(), note);
710-
err.emit();
711-
}
712-
}
713-
714654
// Miscellaneous post-processing, including recording reexports, recording shadowed traits,
715655
// reporting conflicts, reporting the PRIVATE_IN_PUBLIC lint, and reporting unresolved imports.
716656
fn finalize_resolutions(&mut self, module: Module<'b>, report_unresolved_imports: bool) {
@@ -720,7 +660,10 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
720660

721661
let mut reexports = Vec::new();
722662
for (&(name, ns), resolution) in module.resolutions.borrow().iter() {
723-
resolution.report_conflicts(|b1, b2| self.report_conflict(name, ns, b1, b2));
663+
resolution.report_conflicts(|b1, b2| {
664+
self.resolver.report_conflict(module, name, ns, b1, b2)
665+
});
666+
724667
let binding = match resolution.binding {
725668
Some(binding) => binding,
726669
None => continue,

0 commit comments

Comments
 (0)