From eff9a27ed042a94b58efdd9484fd4486bd126917 Mon Sep 17 00:00:00 2001 From: Mu001999 Date: Mon, 8 May 2023 01:01:33 +0800 Subject: [PATCH 1/6] Note if the user wants to use associated items of a trait directly --- compiler/rustc_hir_analysis/src/astconv/mod.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_hir_analysis/src/astconv/mod.rs b/compiler/rustc_hir_analysis/src/astconv/mod.rs index c199b9da6feea..1bf74362f2f37 100644 --- a/compiler/rustc_hir_analysis/src/astconv/mod.rs +++ b/compiler/rustc_hir_analysis/src/astconv/mod.rs @@ -3635,7 +3635,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { fn maybe_lint_bare_trait(&self, self_ty: &hir::Ty<'_>, in_path: bool) { let tcx = self.tcx(); - if let hir::TyKind::TraitObject([poly_trait_ref, ..], _, TraitObjectSyntax::None) = + if let hir::TyKind::TraitObject([poly_trait_ref, res @ ..], _, TraitObjectSyntax::None) = self_ty.kind { let needs_bracket = in_path @@ -3680,6 +3680,19 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { Applicability::MachineApplicable, ); } + // note if the user wants to use associated items of a trait directly + if in_path && res.len() == 0 { + if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(self_ty.span) { + diag.note(format!( + "desired associated item not found in trait `{}` if you want to use such one", + snippet, + )); + } else { + diag.note(format!( + "desired associated item not found in this trait if you want to use such one", + )); + } + } // check if the impl trait that we are considering is a impl of a local trait self.maybe_lint_blanket_trait_impl(&self_ty, &mut diag); diag.emit(); From c6215f28e58ff15a87b054c86af936fff8d32338 Mon Sep 17 00:00:00 2001 From: Mu001999 Date: Mon, 8 May 2023 01:01:57 +0800 Subject: [PATCH 2/6] Add tests for issue 111312 --- tests/ui/associated-item/issue-111312.rs | 12 ++++++++ tests/ui/associated-item/issue-111312.stderr | 29 ++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 tests/ui/associated-item/issue-111312.rs create mode 100644 tests/ui/associated-item/issue-111312.stderr diff --git a/tests/ui/associated-item/issue-111312.rs b/tests/ui/associated-item/issue-111312.rs new file mode 100644 index 0000000000000..6c59dfb5c6da7 --- /dev/null +++ b/tests/ui/associated-item/issue-111312.rs @@ -0,0 +1,12 @@ +// edition:2021 + +trait Has { + fn has() {} +} + +trait HasNot {} + +fn main() { + HasNot::has(); //~ ERROR E0782 + //~^ ERROR E0599 +} diff --git a/tests/ui/associated-item/issue-111312.stderr b/tests/ui/associated-item/issue-111312.stderr new file mode 100644 index 0000000000000..a6592ea5d52e5 --- /dev/null +++ b/tests/ui/associated-item/issue-111312.stderr @@ -0,0 +1,29 @@ +error[E0782]: trait objects must include the `dyn` keyword + --> $DIR/issue-111312.rs:10:5 + | +LL | HasNot::has(); + | ^^^^^^ + | + = note: desired associated item not found in trait `HasNot` if you want to use such one +help: add `dyn` keyword before this trait + | +LL | ::has(); + | ++++ + + +error[E0599]: no function or associated item named `has` found for trait object `dyn HasNot` in the current scope + --> $DIR/issue-111312.rs:10:13 + | +LL | HasNot::has(); + | ^^^ function or associated item not found in `dyn HasNot` + | + = help: items from traits can only be used if the trait is implemented and in scope +note: `Has` defines an item `has`, perhaps you need to implement it + --> $DIR/issue-111312.rs:3:1 + | +LL | trait Has { + | ^^^^^^^^^ + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0599, E0782. +For more information about an error, try `rustc --explain E0599`. From e977046722ab7e3822645516d61ea4b77c8dd979 Mon Sep 17 00:00:00 2001 From: Mu001999 Date: Mon, 8 May 2023 01:02:09 +0800 Subject: [PATCH 3/6] Bless other tests --- tests/ui/editions/dyn-trait-sugg-2021.stderr | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ui/editions/dyn-trait-sugg-2021.stderr b/tests/ui/editions/dyn-trait-sugg-2021.stderr index 8c68dec1df7e4..595b72b995ab2 100644 --- a/tests/ui/editions/dyn-trait-sugg-2021.stderr +++ b/tests/ui/editions/dyn-trait-sugg-2021.stderr @@ -4,6 +4,7 @@ error[E0782]: trait objects must include the `dyn` keyword LL | Foo::hi(123); | ^^^ | + = note: desired associated item not found in trait `Foo` if you want to use such one help: add `dyn` keyword before this trait | LL | ::hi(123); From 4f92085315b61a274c191b14a436c515a7ca9d30 Mon Sep 17 00:00:00 2001 From: Mu001999 Date: Mon, 8 May 2023 10:59:12 +0800 Subject: [PATCH 4/6] Use span_label --- compiler/rustc_hir_analysis/src/astconv/mod.rs | 14 ++++---------- tests/ui/associated-item/issue-111312.stderr | 3 +-- tests/ui/editions/dyn-trait-sugg-2021.stderr | 3 +-- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/astconv/mod.rs b/compiler/rustc_hir_analysis/src/astconv/mod.rs index 1bf74362f2f37..257cff27c1d89 100644 --- a/compiler/rustc_hir_analysis/src/astconv/mod.rs +++ b/compiler/rustc_hir_analysis/src/astconv/mod.rs @@ -3682,16 +3682,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { } // note if the user wants to use associated items of a trait directly if in_path && res.len() == 0 { - if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(self_ty.span) { - diag.note(format!( - "desired associated item not found in trait `{}` if you want to use such one", - snippet, - )); - } else { - diag.note(format!( - "desired associated item not found in this trait if you want to use such one", - )); - } + diag.span_label( + self_ty.span, + "no desired associated item found in this trait if you want to use such one", + ); } // check if the impl trait that we are considering is a impl of a local trait self.maybe_lint_blanket_trait_impl(&self_ty, &mut diag); diff --git a/tests/ui/associated-item/issue-111312.stderr b/tests/ui/associated-item/issue-111312.stderr index a6592ea5d52e5..88526025099c0 100644 --- a/tests/ui/associated-item/issue-111312.stderr +++ b/tests/ui/associated-item/issue-111312.stderr @@ -2,9 +2,8 @@ error[E0782]: trait objects must include the `dyn` keyword --> $DIR/issue-111312.rs:10:5 | LL | HasNot::has(); - | ^^^^^^ + | ^^^^^^ no desired associated item found in this trait if you want to use such one | - = note: desired associated item not found in trait `HasNot` if you want to use such one help: add `dyn` keyword before this trait | LL | ::has(); diff --git a/tests/ui/editions/dyn-trait-sugg-2021.stderr b/tests/ui/editions/dyn-trait-sugg-2021.stderr index 595b72b995ab2..921f59defee8b 100644 --- a/tests/ui/editions/dyn-trait-sugg-2021.stderr +++ b/tests/ui/editions/dyn-trait-sugg-2021.stderr @@ -2,9 +2,8 @@ error[E0782]: trait objects must include the `dyn` keyword --> $DIR/dyn-trait-sugg-2021.rs:10:5 | LL | Foo::hi(123); - | ^^^ + | ^^^ no desired associated item found in this trait if you want to use such one | - = note: desired associated item not found in trait `Foo` if you want to use such one help: add `dyn` keyword before this trait | LL | ::hi(123); From fe76d62aad41a3903f4ec00ded97e98b6fe44391 Mon Sep 17 00:00:00 2001 From: Mu001999 Date: Mon, 8 May 2023 11:05:13 +0800 Subject: [PATCH 5/6] Fix wrong spaces --- tests/ui/associated-item/issue-111312.stderr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ui/associated-item/issue-111312.stderr b/tests/ui/associated-item/issue-111312.stderr index 88526025099c0..ad7f90194f2b8 100644 --- a/tests/ui/associated-item/issue-111312.stderr +++ b/tests/ui/associated-item/issue-111312.stderr @@ -2,7 +2,7 @@ error[E0782]: trait objects must include the `dyn` keyword --> $DIR/issue-111312.rs:10:5 | LL | HasNot::has(); - | ^^^^^^ no desired associated item found in this trait if you want to use such one + | ^^^^^^ no desired associated item found in this trait if you want to use such one | help: add `dyn` keyword before this trait | From 2b97370c621767a1c729ffe3930c823427a0bc51 Mon Sep 17 00:00:00 2001 From: Mu001999 Date: Mon, 8 May 2023 11:30:56 +0800 Subject: [PATCH 6/6] Update the note --- compiler/rustc_hir_analysis/src/astconv/mod.rs | 2 +- tests/ui/associated-item/issue-111312.stderr | 2 +- tests/ui/editions/dyn-trait-sugg-2021.stderr | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/astconv/mod.rs b/compiler/rustc_hir_analysis/src/astconv/mod.rs index 257cff27c1d89..6bc007baf4e39 100644 --- a/compiler/rustc_hir_analysis/src/astconv/mod.rs +++ b/compiler/rustc_hir_analysis/src/astconv/mod.rs @@ -3684,7 +3684,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { if in_path && res.len() == 0 { diag.span_label( self_ty.span, - "no desired associated item found in this trait if you want to use such one", + "no desired associated item found in this trait, if you do not want to use a bare trait object here", ); } // check if the impl trait that we are considering is a impl of a local trait diff --git a/tests/ui/associated-item/issue-111312.stderr b/tests/ui/associated-item/issue-111312.stderr index ad7f90194f2b8..7796efb5e43d9 100644 --- a/tests/ui/associated-item/issue-111312.stderr +++ b/tests/ui/associated-item/issue-111312.stderr @@ -2,7 +2,7 @@ error[E0782]: trait objects must include the `dyn` keyword --> $DIR/issue-111312.rs:10:5 | LL | HasNot::has(); - | ^^^^^^ no desired associated item found in this trait if you want to use such one + | ^^^^^^ no desired associated item found in this trait, if you do not want to use a bare trait object here | help: add `dyn` keyword before this trait | diff --git a/tests/ui/editions/dyn-trait-sugg-2021.stderr b/tests/ui/editions/dyn-trait-sugg-2021.stderr index 921f59defee8b..6f0e5dfe1e15b 100644 --- a/tests/ui/editions/dyn-trait-sugg-2021.stderr +++ b/tests/ui/editions/dyn-trait-sugg-2021.stderr @@ -2,7 +2,7 @@ error[E0782]: trait objects must include the `dyn` keyword --> $DIR/dyn-trait-sugg-2021.rs:10:5 | LL | Foo::hi(123); - | ^^^ no desired associated item found in this trait if you want to use such one + | ^^^ no desired associated item found in this trait, if you do not want to use a bare trait object here | help: add `dyn` keyword before this trait |