Skip to content

Commit 32e462e

Browse files
authored
Auto merge of #35145 - jseyfried:avoid_extra_resolve_error, r=arielb1
resolve: Avoid emitting an unhelpful cascading resolution error Fixes #35142.
2 parents 97d5be9 + 6656a30 commit 32e462e

File tree

4 files changed

+26
-40
lines changed

4 files changed

+26
-40
lines changed

src/librustc_resolve/lib.rs

+17-31
Original file line numberDiff line numberDiff line change
@@ -1807,39 +1807,25 @@ impl<'a> Resolver<'a> {
18071807
path_depth: usize)
18081808
-> Result<PathResolution, ()> {
18091809
self.resolve_path(id, trait_path, path_depth, TypeNS).and_then(|path_res| {
1810-
if let Def::Trait(_) = path_res.base_def {
1811-
debug!("(resolving trait) found trait def: {:?}", path_res);
1812-
Ok(path_res)
1813-
} else {
1814-
let mut err =
1815-
resolve_struct_error(self,
1816-
trait_path.span,
1817-
ResolutionError::IsNotATrait(&path_names_to_string(trait_path,
1818-
path_depth)));
1819-
1820-
// If it's a typedef, give a note
1821-
if let Def::TyAlias(..) = path_res.base_def {
1822-
let trait_name = trait_path.segments.last().unwrap().identifier.name;
1823-
err.span_label(trait_path.span,
1824-
&format!("`{}` is not a trait", trait_name));
1825-
1826-
let definition_site = {
1827-
let segments = &trait_path.segments;
1828-
if trait_path.global {
1829-
self.resolve_crate_relative_path(trait_path.span, segments, TypeNS)
1830-
} else {
1831-
self.resolve_module_relative_path(trait_path.span, segments, TypeNS)
1832-
}.map(|binding| binding.span).unwrap_or(syntax_pos::DUMMY_SP)
1833-
};
1834-
1835-
if definition_site != syntax_pos::DUMMY_SP {
1836-
err.span_label(definition_site,
1837-
&format!("type aliases cannot be used for traits"));
1838-
}
1810+
match path_res.base_def {
1811+
Def::Trait(_) => {
1812+
debug!("(resolving trait) found trait def: {:?}", path_res);
1813+
return Ok(path_res);
18391814
}
1840-
err.emit();
1841-
Err(true)
1815+
Def::Err => return Err(true),
1816+
_ => {}
1817+
}
1818+
1819+
let mut err = resolve_struct_error(self, trait_path.span, {
1820+
ResolutionError::IsNotATrait(&path_names_to_string(trait_path, path_depth))
1821+
});
1822+
1823+
// If it's a typedef, give a note
1824+
if let Def::TyAlias(..) = path_res.base_def {
1825+
err.note(&format!("type aliases cannot be used for traits"));
18421826
}
1827+
err.emit();
1828+
Err(true)
18431829
}).map_err(|error_reported| {
18441830
if error_reported { return }
18451831

src/test/compile-fail/issue-3907.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
// aux-build:issue_3907.rs
1212
extern crate issue_3907;
1313

14-
type Foo = issue_3907::Foo; //~ NOTE: type aliases cannot be used for traits
14+
type Foo = issue_3907::Foo;
1515

1616
struct S {
1717
name: isize
1818
}
1919

2020
impl Foo for S { //~ ERROR: `Foo` is not a trait
21-
//~| `Foo` is not a trait
21+
//~| NOTE: type aliases cannot be used for traits
2222
fn bar() { }
2323
}
2424

src/test/compile-fail/issue-5035.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
trait I {}
1212
type K = I;
13-
//~^ NOTE: aliases cannot be used for traits
1413
impl K for isize {} //~ ERROR: `K` is not a trait
15-
//~| is not a trait
14+
//~| NOTE: aliases cannot be used for traits
15+
16+
use ImportError; //~ ERROR unresolved
17+
impl ImportError for () {} // check that this is not an additional error (c.f. #35142)
18+
1619
fn main() {}

src/test/ui/codemap_tests/two_files.stderr

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ error[E0404]: `Bar` is not a trait
22
--> $DIR/two_files.rs:16:6
33
|
44
16 | impl Bar for Baz { }
5-
| ^^^ `Bar` is not a trait
6-
|
7-
::: $DIR/two_files_data.rs
5+
| ^^^
86
|
9-
15 | type Bar = Foo;
10-
| --------------- type aliases cannot be used for traits
7+
= note: type aliases cannot be used for traits
118

129
error: cannot continue compilation due to previous error
1310

0 commit comments

Comments
 (0)