Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 45 additions & 24 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
span: Span,
source: PathSource<'_, 'ast, 'ra>,
res: Option<Res>,
could_be_expr: bool,
) -> BaseError {
// Make the base error.
let mut expected = source.descr_expected();
Expand All @@ -400,28 +401,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
}
_ => None,
},
could_be_expr: match res {
Res::Def(DefKind::Fn, _) => {
// Verify whether this is a fn call or an Fn used as a type.
self.r
.tcx
.sess
.source_map()
.span_to_snippet(span)
.is_ok_and(|snippet| snippet.ends_with(')'))
}
Res::Def(
DefKind::Ctor(..)
| DefKind::AssocFn
| DefKind::Const { .. }
| DefKind::AssocConst { .. },
_,
)
| Res::SelfCtor(_)
| Res::PrimTy(_)
| Res::Local(_) => true,
_ => false,
},
could_be_expr,
suggestion: None,
module: None,
}
Expand Down Expand Up @@ -571,13 +551,37 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
fallback_label,
span: item_span,
span_label,
could_be_expr: false,
could_be_expr,
suggestion,
module,
}
}
}

fn could_be_expr(&self, res: Res, span: Span) -> bool {
match res {
// Verify whether this is a fn call or an Fn used as a type.
Res::Def(DefKind::Fn, _) => self
.r
.tcx
.sess
.source_map()
.span_to_snippet(span)
.is_ok_and(|snippet| snippet.ends_with(')')),
Res::Def(
DefKind::Ctor(..)
| DefKind::AssocFn
| DefKind::Const { .. }
| DefKind::AssocConst { .. },
_,
)
| Res::SelfCtor(_)
| Res::PrimTy(_)
| Res::Local(_) => true,
_ => false,
}
}

/// Try to suggest for a module path that cannot be resolved.
/// Such as `fmt::Debug` where `fmt` is not resolved without importing,
/// here we search with `lookup_import_candidates` for a module named `fmt`
Expand Down Expand Up @@ -636,12 +640,29 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
qself: Option<&QSelf>,
) -> (Diag<'tcx>, Vec<ImportSuggestion>) {
debug!(?res, ?source);
let base_error = self.make_base_error(path, span, source, res);
let cross_namespace_res = res.filter(|res| !res.matches_ns(source.namespace()));
let could_be_expr = res.is_some_and(|res| self.could_be_expr(res, span));
let base_error = self.make_base_error(
path,
span,
source,
if cross_namespace_res.is_some() { None } else { res },
could_be_expr,
);

let code = source.error_code(res.is_some());
let mut err = self.r.dcx().struct_span_err(base_error.span, base_error.msg.clone());
err.code(code);

if let Some(res) = cross_namespace_res {
err.note(format!(
"{} {} named `{}` exists in another namespace",
res.article(),
res.descr(),
Segment::names_to_string(path),
));
}

// Try to get the span of the identifier within the path's syntax context
// (if that's different).
if let Some(within_macro_span) =
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/fail/rustc-error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Make sure we exit with non-0 status code when the program fails to build.
fn main() {
println("Hello, world!"); //~ ERROR: expected function, found macro
println("Hello, world!"); //~ ERROR: cannot find function `println` in this scope
}
5 changes: 3 additions & 2 deletions src/tools/miri/tests/fail/rustc-error.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
error[E0423]: expected function, found macro `println`
error[E0423]: cannot find function `println` in this scope
--> tests/fail/rustc-error.rs:LL:CC
|
LL | println("Hello, world!");
| ^^^^^^^ not a function
| ^^^^^^^ not found in this scope
|
= note: a macro named `println` exists in another namespace
help: use `!` to invoke the macro
|
LL | println!("Hello, world!");
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/associated-consts/issue-58022.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl Bar<[u8]> {
fn new(slice: &[u8; Self::SIZE]) -> Self {
//~^ ERROR: the size for values of type `[u8]` cannot be known at compilation time
Foo(Box::new(*slice))
//~^ ERROR: expected function, tuple struct or tuple variant, found trait `Foo`
//~^ ERROR: cannot find function, tuple struct or tuple variant `Foo` in this scope
}
}

Expand Down
6 changes: 4 additions & 2 deletions tests/ui/associated-consts/issue-58022.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ LL |
LL | fn new(slice: &[u8; Foo::SIZE]) -> Self;
| ^^^^^^^^^ cannot refer to the associated constant of trait

error[E0423]: expected function, tuple struct or tuple variant, found trait `Foo`
error[E0423]: cannot find function, tuple struct or tuple variant `Foo` in this scope
--> $DIR/issue-58022.rs:15:9
|
LL | Foo(Box::new(*slice))
| ^^^ not a function, tuple struct or tuple variant
| ^^^ not found in this scope
|
= note: a trait named `Foo` exists in another namespace

error: aborting due to 3 previous errors

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Regression test for <https://github.com/rust-lang/rust/issues/137554>.

fn main() -> dyn Iterator + ?Iterator::advance_by(usize) {
//~^ ERROR expected trait, found associated function `Iterator::advance_by`
//~^ ERROR cannot find trait `advance_by` in trait `Iterator`
//~| ERROR relaxed bounds are not permitted in trait object types
todo!()
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0404]: expected trait, found associated function `Iterator::advance_by`
--> $DIR/missing-associated_item_or_field_def_ids.rs:3:30
error[E0404]: cannot find trait `advance_by` in trait `Iterator`
--> $DIR/missing-associated_item_or_field_def_ids.rs:3:40
|
LL | fn main() -> dyn Iterator + ?Iterator::advance_by(usize) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a trait
| ^^^^^^^^^^ not found in `Iterator`
|
= note: an associated function named `Iterator::advance_by` exists in another namespace

error: relaxed bounds are not permitted in trait object types
--> $DIR/missing-associated_item_or_field_def_ids.rs:3:29
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ fn baz_path<T: Trait>() where T::method(..): Send {}
//~^ ERROR return type notation arguments must be elided with `..`

fn foo_qualified<T: Trait>() where T::method(..): Send {}
//~^ ERROR expected associated type
//~^ ERROR cannot find associated type `method` in trait `Trait`

fn bar_qualified<T: Trait>() where T::method(..): Send {}
//~^ ERROR expected associated type
//~^ ERROR cannot find associated type `method` in trait `Trait`

fn baz_qualified<T: Trait>() where T::method(..): Send {}
//~^ ERROR expected associated type
//~^ ERROR cannot find associated type `method` in trait `Trait`

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ fn baz_path<T: Trait>() where T::method(): Send {}
//~^ ERROR return type notation arguments must be elided with `..`

fn foo_qualified<T: Trait>() where <T as Trait>::method(i32): Send {}
//~^ ERROR expected associated type
//~^ ERROR cannot find associated type `method` in trait `Trait`

fn bar_qualified<T: Trait>() where <T as Trait>::method() -> (): Send {}
//~^ ERROR expected associated type
//~^ ERROR cannot find associated type `method` in trait `Trait`

fn baz_qualified<T: Trait>() where <T as Trait>::method(): Send {}
//~^ ERROR expected associated type
//~^ ERROR cannot find associated type `method` in trait `Trait`

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,39 @@ LL - fn bay_path<T: Trait>() where T::method(..) -> (): Send {}
LL + fn bay_path<T: Trait>() where T::method(..): Send {}
|

error[E0575]: expected associated type, found associated function `Trait::method`
--> $DIR/bad-inputs-and-output.rs:31:36
error[E0575]: cannot find associated type `method` in trait `Trait`
--> $DIR/bad-inputs-and-output.rs:31:50
|
LL | fn foo_qualified<T: Trait>() where <T as Trait>::method(i32): Send {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not a associated type
| ^^^^^^ not found in `Trait`
|
= note: an associated function named `Trait::method` exists in another namespace
help: you might have meant to use the return type notation syntax
|
LL - fn foo_qualified<T: Trait>() where <T as Trait>::method(i32): Send {}
LL + fn foo_qualified<T: Trait>() where T::method(..): Send {}
|

error[E0575]: expected associated type, found associated function `Trait::method`
--> $DIR/bad-inputs-and-output.rs:34:36
error[E0575]: cannot find associated type `method` in trait `Trait`
--> $DIR/bad-inputs-and-output.rs:34:50
|
LL | fn bar_qualified<T: Trait>() where <T as Trait>::method() -> (): Send {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a associated type
| ^^^^^^ not found in `Trait`
|
= note: an associated function named `Trait::method` exists in another namespace
help: you might have meant to use the return type notation syntax
|
LL - fn bar_qualified<T: Trait>() where <T as Trait>::method() -> (): Send {}
LL + fn bar_qualified<T: Trait>() where T::method(..): Send {}
|

error[E0575]: expected associated type, found associated function `Trait::method`
--> $DIR/bad-inputs-and-output.rs:37:36
error[E0575]: cannot find associated type `method` in trait `Trait`
--> $DIR/bad-inputs-and-output.rs:37:50
|
LL | fn baz_qualified<T: Trait>() where <T as Trait>::method(): Send {}
| ^^^^^^^^^^^^^^^^^^^^^^ not a associated type
| ^^^^^^ not found in `Trait`
|
= note: an associated function named `Trait::method` exists in another namespace
help: you might have meant to use the return type notation syntax
|
LL - fn baz_qualified<T: Trait>() where <T as Trait>::method(): Send {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ where
fn not_a_method_and_typoed()
where
function(): Send,
//~^ ERROR expected type, found function `function`
//~^ ERROR cannot find type `function` in this scope
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ error[E0575]: expected function, found function `function`
LL | function(..): Send,
| ^^^^^^^^^^^^ not a function

error[E0573]: expected type, found function `function`
error[E0573]: cannot find type `function` in this scope
--> $DIR/not-a-method.rs:15:5
|
LL | function(): Send,
| ^^^^^^^^^^ not a type
| ^^^^^^^^ not found in this scope
|
= note: a function named `function` exists in another namespace

error[E0576]: cannot find function `method` in this scope
--> $DIR/not-a-method.rs:27:5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ where
<T as A>::method(..): Send,
//~^ ERROR cannot find method or associated constant `method` in trait `A`
<T as A>::bad(..): Send,
//~^ ERROR expected method or associated constant, found associated type `A::bad`
//~^ ERROR cannot find method or associated constant `bad` in trait `A`
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ error[E0576]: cannot find method or associated constant `method` in trait `A`
LL | <T as A>::method(..): Send,
| ^^^^^^ not found in `A`

error[E0575]: expected method or associated constant, found associated type `A::bad`
--> $DIR/path-missing.rs:12:5
error[E0575]: cannot find method or associated constant `bad` in trait `A`
--> $DIR/path-missing.rs:12:15
|
LL | <T as A>::bad(..): Send,
| ^^^^^^^^^^^^^^^^^ not a method or associated constant
| ^^^ not found in `A`
|
= note: an associated type named `A::bad` exists in another namespace

error[E0220]: associated function `method` not found for `T`
--> $DIR/path-missing.rs:19:8
Expand Down
16 changes: 8 additions & 8 deletions tests/ui/associated-types/tuple-struct-expr-pat.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@

fn main() {
let <T<0> as Trait>::Assoc {} = <T<0> as Trait>::Assoc {};
//~^ error: expected method or associated constant, found associated type
//~| error: expected tuple struct or tuple variant, found associated type
//~^ error: cannot find method or associated constant `Assoc` in trait `Trait`
//~| error: cannot find tuple struct or tuple variant `Assoc` in trait `Trait`
let <T<1> as Trait>::Assoc { 0: _a } = <T<1> as Trait>::Assoc { 0: 0 };
//~^ error: expected method or associated constant, found associated type
//~| error: expected tuple struct or tuple variant, found associated type
//~^ error: cannot find method or associated constant `Assoc` in trait `Trait`
//~| error: cannot find tuple struct or tuple variant `Assoc` in trait `Trait`
let <T<2> as Trait>::Assoc { 0: _a, 1: _b } = <T<2> as Trait>::Assoc { 0: 0, 1: 1 };
//~^ error: expected method or associated constant, found associated type
//~| error: expected tuple struct or tuple variant, found associated type
//~^ error: cannot find method or associated constant `Assoc` in trait `Trait`
//~| error: cannot find tuple struct or tuple variant `Assoc` in trait `Trait`
let <T<3> as Trait>::Assoc { 0: ref _a, 1: ref mut _b, 2: mut _c } = <T<3> as Trait>::Assoc { 0: 0, 1: 1, 2: 2 };
//~^ error: expected method or associated constant, found associated type
//~| error: expected tuple struct or tuple variant, found associated type
//~^ error: cannot find method or associated constant `Assoc` in trait `Trait`
//~| error: cannot find tuple struct or tuple variant `Assoc` in trait `Trait`
}


Expand Down
16 changes: 8 additions & 8 deletions tests/ui/associated-types/tuple-struct-expr-pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@

fn main() {
let <T<0> as Trait>::Assoc() = <T<0> as Trait>::Assoc();
//~^ error: expected method or associated constant, found associated type
//~| error: expected tuple struct or tuple variant, found associated type
//~^ error: cannot find method or associated constant `Assoc` in trait `Trait`
//~| error: cannot find tuple struct or tuple variant `Assoc` in trait `Trait`
let <T<1> as Trait>::Assoc(_a) = <T<1> as Trait>::Assoc(0);
//~^ error: expected method or associated constant, found associated type
//~| error: expected tuple struct or tuple variant, found associated type
//~^ error: cannot find method or associated constant `Assoc` in trait `Trait`
//~| error: cannot find tuple struct or tuple variant `Assoc` in trait `Trait`
let <T<2> as Trait>::Assoc(_a, _b) = <T<2> as Trait>::Assoc(0, 1);
//~^ error: expected method or associated constant, found associated type
//~| error: expected tuple struct or tuple variant, found associated type
//~^ error: cannot find method or associated constant `Assoc` in trait `Trait`
//~| error: cannot find tuple struct or tuple variant `Assoc` in trait `Trait`
let <T<3> as Trait>::Assoc(ref _a, ref mut _b, mut _c) = <T<3> as Trait>::Assoc(0, 1, 2);
//~^ error: expected method or associated constant, found associated type
//~| error: expected tuple struct or tuple variant, found associated type
//~^ error: cannot find method or associated constant `Assoc` in trait `Trait`
//~| error: cannot find tuple struct or tuple variant `Assoc` in trait `Trait`
}


Expand Down
Loading
Loading