diff --git a/compiler/rustc_hir_analysis/src/astconv/mod.rs b/compiler/rustc_hir_analysis/src/astconv/mod.rs index c199b9da6feea..6bc007baf4e39 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,13 @@ 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 { + diag.span_label( + self_ty.span, + "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 self.maybe_lint_blanket_trait_impl(&self_ty, &mut diag); diag.emit(); 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..7796efb5e43d9 --- /dev/null +++ b/tests/ui/associated-item/issue-111312.stderr @@ -0,0 +1,28 @@ +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 do not want to use a bare trait object here + | +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`. diff --git a/tests/ui/editions/dyn-trait-sugg-2021.stderr b/tests/ui/editions/dyn-trait-sugg-2021.stderr index 8c68dec1df7e4..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 do not want to use a bare trait object here | help: add `dyn` keyword before this trait |