From 46bd5d3fa01a86925f7e7776a5567c536e6f2408 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Sun, 31 Jul 2016 20:11:38 +0000 Subject: [PATCH 1/4] Avoid emitting a unhelpful cascading resolution error. --- src/librustc_resolve/lib.rs | 2 ++ src/test/compile-fail/issue-5035.rs | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 8c8cf1da46738..23281d25c8d52 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -1810,6 +1810,8 @@ impl<'a> Resolver<'a> { if let Def::Trait(_) = path_res.base_def { debug!("(resolving trait) found trait def: {:?}", path_res); Ok(path_res) + } else if path_res.base_def == Def::Err { + Err(true) } else { let mut err = resolve_struct_error(self, diff --git a/src/test/compile-fail/issue-5035.rs b/src/test/compile-fail/issue-5035.rs index a186a399a112c..61080ef609645 100644 --- a/src/test/compile-fail/issue-5035.rs +++ b/src/test/compile-fail/issue-5035.rs @@ -13,4 +13,8 @@ type K = I; //~^ NOTE: aliases cannot be used for traits impl K for isize {} //~ ERROR: `K` is not a trait //~| is not a trait + +use ImportError; //~ ERROR unresolved +impl ImportError for () {} // check that this is not an additional error (c.f. #35142) + fn main() {} From 93fd214d61749ecae05a9ff240d51fcf77ca086c Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Sun, 31 Jul 2016 20:40:03 +0000 Subject: [PATCH 2/4] Clean up `resolve_trait_reference`. --- src/librustc_resolve/lib.rs | 63 ++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 23281d25c8d52..33e5282188c28 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -1807,41 +1807,40 @@ impl<'a> Resolver<'a> { path_depth: usize) -> Result { self.resolve_path(id, trait_path, path_depth, TypeNS).and_then(|path_res| { - if let Def::Trait(_) = path_res.base_def { - debug!("(resolving trait) found trait def: {:?}", path_res); - Ok(path_res) - } else if path_res.base_def == Def::Err { - Err(true) - } else { - let mut err = - resolve_struct_error(self, - trait_path.span, - ResolutionError::IsNotATrait(&path_names_to_string(trait_path, - path_depth))); - - // If it's a typedef, give a note - if let Def::TyAlias(..) = path_res.base_def { - let trait_name = trait_path.segments.last().unwrap().identifier.name; - err.span_label(trait_path.span, - &format!("`{}` is not a trait", trait_name)); - - let definition_site = { - let segments = &trait_path.segments; - if trait_path.global { - self.resolve_crate_relative_path(trait_path.span, segments, TypeNS) - } else { - self.resolve_module_relative_path(trait_path.span, segments, TypeNS) - }.map(|binding| binding.span).unwrap_or(syntax_pos::DUMMY_SP) - }; + match path_res.base_def { + Def::Trait(_) => { + debug!("(resolving trait) found trait def: {:?}", path_res); + return Ok(path_res); + } + Def::Err => return Err(true), + _ => {} + } - if definition_site != syntax_pos::DUMMY_SP { - err.span_label(definition_site, - &format!("type aliases cannot be used for traits")); - } + let mut err = resolve_struct_error(self, trait_path.span, { + ResolutionError::IsNotATrait(&path_names_to_string(trait_path, path_depth)) + }); + + // If it's a typedef, give a note + if let Def::TyAlias(..) = path_res.base_def { + let trait_name = trait_path.segments.last().unwrap().identifier.name; + err.span_label(trait_path.span, &format!("`{}` is not a trait", trait_name)); + + let definition_site = { + let segments = &trait_path.segments; + if trait_path.global { + self.resolve_crate_relative_path(trait_path.span, segments, TypeNS) + } else { + self.resolve_module_relative_path(trait_path.span, segments, TypeNS) + }.map(|binding| binding.span).unwrap_or(syntax_pos::DUMMY_SP) + }; + + if definition_site != syntax_pos::DUMMY_SP { + err.span_label(definition_site, + &format!("type aliases cannot be used for traits")); } - err.emit(); - Err(true) } + err.emit(); + Err(true) }).map_err(|error_reported| { if error_reported { return } From d6b10beb884bf3a2379ba694bc44b933ad5164c6 Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Sun, 31 Jul 2016 21:29:01 +0000 Subject: [PATCH 3/4] Make "type aliases cannot be used for traits" a note instead of a span_label. --- src/librustc_resolve/lib.rs | 17 +---------------- src/test/compile-fail/issue-3907.rs | 4 ++-- src/test/compile-fail/issue-5035.rs | 3 +-- 3 files changed, 4 insertions(+), 20 deletions(-) diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 33e5282188c28..c1511b29c9e01 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -1822,22 +1822,7 @@ impl<'a> Resolver<'a> { // If it's a typedef, give a note if let Def::TyAlias(..) = path_res.base_def { - let trait_name = trait_path.segments.last().unwrap().identifier.name; - err.span_label(trait_path.span, &format!("`{}` is not a trait", trait_name)); - - let definition_site = { - let segments = &trait_path.segments; - if trait_path.global { - self.resolve_crate_relative_path(trait_path.span, segments, TypeNS) - } else { - self.resolve_module_relative_path(trait_path.span, segments, TypeNS) - }.map(|binding| binding.span).unwrap_or(syntax_pos::DUMMY_SP) - }; - - if definition_site != syntax_pos::DUMMY_SP { - err.span_label(definition_site, - &format!("type aliases cannot be used for traits")); - } + err.note(&format!("type aliases cannot be used for traits")); } err.emit(); Err(true) diff --git a/src/test/compile-fail/issue-3907.rs b/src/test/compile-fail/issue-3907.rs index c99ff1813e0d1..cbc09a028c2c6 100644 --- a/src/test/compile-fail/issue-3907.rs +++ b/src/test/compile-fail/issue-3907.rs @@ -11,14 +11,14 @@ // aux-build:issue_3907.rs extern crate issue_3907; -type Foo = issue_3907::Foo; //~ NOTE: type aliases cannot be used for traits +type Foo = issue_3907::Foo; struct S { name: isize } impl Foo for S { //~ ERROR: `Foo` is not a trait - //~| `Foo` is not a trait + //~| NOTE: type aliases cannot be used for traits fn bar() { } } diff --git a/src/test/compile-fail/issue-5035.rs b/src/test/compile-fail/issue-5035.rs index 61080ef609645..9648d64d1fb6e 100644 --- a/src/test/compile-fail/issue-5035.rs +++ b/src/test/compile-fail/issue-5035.rs @@ -10,9 +10,8 @@ trait I {} type K = I; -//~^ NOTE: aliases cannot be used for traits impl K for isize {} //~ ERROR: `K` is not a trait -//~| is not a trait + //~| NOTE: aliases cannot be used for traits use ImportError; //~ ERROR unresolved impl ImportError for () {} // check that this is not an additional error (c.f. #35142) From 6656a30ca161d3ea46add7e6538f74ed1e671e4e Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Mon, 1 Aug 2016 18:42:16 +0000 Subject: [PATCH 4/4] Fix fallout in `ui/codemap_tests`. --- src/test/ui/codemap_tests/two_files.stderr | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/test/ui/codemap_tests/two_files.stderr b/src/test/ui/codemap_tests/two_files.stderr index 6c388cd69395b..cf3f187af9334 100644 --- a/src/test/ui/codemap_tests/two_files.stderr +++ b/src/test/ui/codemap_tests/two_files.stderr @@ -2,12 +2,9 @@ error[E0404]: `Bar` is not a trait --> $DIR/two_files.rs:16:6 | 16 | impl Bar for Baz { } - | ^^^ `Bar` is not a trait - | - ::: $DIR/two_files_data.rs + | ^^^ | -15 | type Bar = Foo; - | --------------- type aliases cannot be used for traits + = note: type aliases cannot be used for traits error: cannot continue compilation due to previous error