about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2024-10-04 22:59:03 +0000
committerEsteban Küber <esteban@kuber.com.ar>2024-10-04 22:59:03 +0000
commite057c43382a1eacc5941bc4e78b36dc122b32419 (patch)
treeabda526cbeee6688402fd015df83ebcb3427ecea
parent14f303bc1430a78ddaa91b3e104bbe4c0413184e (diff)
downloadrust-e057c43382a1eacc5941bc4e78b36dc122b32419.tar.gz
rust-e057c43382a1eacc5941bc4e78b36dc122b32419.zip
Account for `impl Trait {` when `impl Trait for Type {` was intended
On editions where bare traits are never allowed, detect if the user has
written `impl Trait` with no type, silence any dyn-compatibility errors,
and provide a structured suggestion for the potentially missing type:

```
error[E0782]: trait objects must include the `dyn` keyword
  --> $DIR/missing-for-type-in-impl.rs:8:6
   |
LL | impl Foo<i64> {
   |      ^^^^^^^^
   |
help: add `dyn` keyword before this trait
   |
LL | impl dyn Foo<i64> {
   |      +++
help: you might have intended to implement this trait for a given type
   |
LL | impl Foo<i64> for /* Type */ {
   |               ++++++++++++++
```
-rw-r--r--compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs17
-rw-r--r--compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs22
-rw-r--r--tests/ui/traits/missing-for-type-in-impl.e2015.stderr74
-rw-r--r--tests/ui/traits/missing-for-type-in-impl.e2021.stderr31
-rw-r--r--tests/ui/traits/missing-for-type-in-impl.rs22
5 files changed, 158 insertions, 8 deletions
diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs
index a70f881f5fe..5607fe873f6 100644
--- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs
+++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs
@@ -108,17 +108,20 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
         let tcx = self.tcx();
         let parent_id = tcx.hir().get_parent_item(self_ty.hir_id).def_id;
         if let hir::Node::Item(hir::Item {
-            kind:
-                hir::ItemKind::Impl(hir::Impl {
-                    self_ty: impl_self_ty,
-                    of_trait: Some(of_trait_ref),
-                    generics,
-                    ..
-                }),
+            kind: hir::ItemKind::Impl(hir::Impl { self_ty: impl_self_ty, of_trait, generics, .. }),
             ..
         }) = tcx.hir_node_by_def_id(parent_id)
             && self_ty.hir_id == impl_self_ty.hir_id
         {
+            let Some(of_trait_ref) = of_trait else {
+                diag.span_suggestion_verbose(
+                    impl_self_ty.span.shrink_to_hi(),
+                    "you might have intended to implement this trait for a given type",
+                    format!(" for /* Type */"),
+                    Applicability::HasPlaceholders,
+                );
+                return;
+            };
             if !of_trait_ref.trait_def_id().is_some_and(|def_id| def_id.is_local()) {
                 return;
             }
diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs
index 1889ecc7670..824c25db07d 100644
--- a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs
+++ b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs
@@ -1,6 +1,7 @@
 use core::ops::ControlFlow;
 use std::borrow::Cow;
 
+use rustc_ast::TraitObjectSyntax;
 use rustc_data_structures::fx::FxHashMap;
 use rustc_data_structures::unord::UnordSet;
 use rustc_errors::codes::*;
@@ -573,7 +574,26 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
 
                     ty::PredicateKind::DynCompatible(trait_def_id) => {
                         let violations = self.tcx.dyn_compatibility_violations(trait_def_id);
-                        report_dyn_incompatibility(self.tcx, span, None, trait_def_id, violations)
+                        let mut err = report_dyn_incompatibility(
+                            self.tcx,
+                            span,
+                            None,
+                            trait_def_id,
+                            violations,
+                        );
+                        if let hir::Node::Item(item) =
+                            self.tcx.hir_node_by_def_id(obligation.cause.body_id)
+                            && let hir::ItemKind::Impl(impl_) = item.kind
+                            && let None = impl_.of_trait
+                            && let hir::TyKind::TraitObject(_, _, syntax) = impl_.self_ty.kind
+                            && let TraitObjectSyntax::None = syntax
+                            && impl_.self_ty.span.edition().at_least_rust_2021()
+                        {
+                            // Silence the dyn-compatibility error in favor of the missing dyn on
+                            // self type error. #131051.
+                            err.downgrade_to_delayed_bug();
+                        }
+                        err
                     }
 
                     ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(ty)) => {
diff --git a/tests/ui/traits/missing-for-type-in-impl.e2015.stderr b/tests/ui/traits/missing-for-type-in-impl.e2015.stderr
new file mode 100644
index 00000000000..541b49b024f
--- /dev/null
+++ b/tests/ui/traits/missing-for-type-in-impl.e2015.stderr
@@ -0,0 +1,74 @@
+warning: trait objects without an explicit `dyn` are deprecated
+  --> $DIR/missing-for-type-in-impl.rs:8:6
+   |
+LL | impl Foo<i64> {
+   |      ^^^^^^^^
+   |
+   = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
+   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
+   = note: `#[warn(bare_trait_objects)]` on by default
+help: if this is a dyn-compatible trait, use `dyn`
+   |
+LL | impl dyn Foo<i64> {
+   |      +++
+help: you might have intended to implement this trait for a given type
+   |
+LL | impl Foo<i64> for /* Type */ {
+   |               ++++++++++++++
+
+warning: trait objects without an explicit `dyn` are deprecated
+  --> $DIR/missing-for-type-in-impl.rs:8:6
+   |
+LL | impl Foo<i64> {
+   |      ^^^^^^^^
+   |
+   = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
+   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
+   = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
+help: if this is a dyn-compatible trait, use `dyn`
+   |
+LL | impl dyn Foo<i64> {
+   |      +++
+help: you might have intended to implement this trait for a given type
+   |
+LL | impl Foo<i64> for /* Type */ {
+   |               ++++++++++++++
+
+error[E0038]: the trait `Foo` cannot be made into an object
+  --> $DIR/missing-for-type-in-impl.rs:8:6
+   |
+LL | impl Foo<i64> {
+   |      ^^^^^^^^ `Foo` cannot be made into an object
+   |
+note: for a trait to be "dyn-compatible" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
+  --> $DIR/missing-for-type-in-impl.rs:4:8
+   |
+LL | trait Foo<T> {
+   |       --- this trait cannot be made into an object...
+LL |     fn id(me: T) -> T;
+   |        ^^ ...because associated function `id` has no `self` parameter
+help: consider turning `id` into a method by giving it a `&self` argument
+   |
+LL |     fn id(&self, me: T) -> T;
+   |           ++++++
+help: alternatively, consider constraining `id` so it does not apply to trait objects
+   |
+LL |     fn id(me: T) -> T where Self: Sized;
+   |                       +++++++++++++++++
+
+error[E0277]: the trait bound `i64: Foo<i64>` is not satisfied
+  --> $DIR/missing-for-type-in-impl.rs:19:19
+   |
+LL |     let x: i64 = <i64 as Foo<i64>>::id(10);
+   |                   ^^^ the trait `Foo<i64>` is not implemented for `i64`
+   |
+help: this trait has no implementations, consider adding one
+  --> $DIR/missing-for-type-in-impl.rs:3:1
+   |
+LL | trait Foo<T> {
+   | ^^^^^^^^^^^^
+
+error: aborting due to 2 previous errors; 2 warnings emitted
+
+Some errors have detailed explanations: E0038, E0277.
+For more information about an error, try `rustc --explain E0038`.
diff --git a/tests/ui/traits/missing-for-type-in-impl.e2021.stderr b/tests/ui/traits/missing-for-type-in-impl.e2021.stderr
new file mode 100644
index 00000000000..b5a607a54cb
--- /dev/null
+++ b/tests/ui/traits/missing-for-type-in-impl.e2021.stderr
@@ -0,0 +1,31 @@
+error[E0277]: the trait bound `i64: Foo<i64>` is not satisfied
+  --> $DIR/missing-for-type-in-impl.rs:19:19
+   |
+LL |     let x: i64 = <i64 as Foo<i64>>::id(10);
+   |                   ^^^ the trait `Foo<i64>` is not implemented for `i64`
+   |
+help: this trait has no implementations, consider adding one
+  --> $DIR/missing-for-type-in-impl.rs:3:1
+   |
+LL | trait Foo<T> {
+   | ^^^^^^^^^^^^
+
+error[E0782]: trait objects must include the `dyn` keyword
+  --> $DIR/missing-for-type-in-impl.rs:8:6
+   |
+LL | impl Foo<i64> {
+   |      ^^^^^^^^
+   |
+help: add `dyn` keyword before this trait
+   |
+LL | impl dyn Foo<i64> {
+   |      +++
+help: you might have intended to implement this trait for a given type
+   |
+LL | impl Foo<i64> for /* Type */ {
+   |               ++++++++++++++
+
+error: aborting due to 2 previous errors
+
+Some errors have detailed explanations: E0277, E0782.
+For more information about an error, try `rustc --explain E0277`.
diff --git a/tests/ui/traits/missing-for-type-in-impl.rs b/tests/ui/traits/missing-for-type-in-impl.rs
new file mode 100644
index 00000000000..7d4ad479e77
--- /dev/null
+++ b/tests/ui/traits/missing-for-type-in-impl.rs
@@ -0,0 +1,22 @@
+//@revisions: e2021 e2015
+//@[e2021]edition: 2021
+trait Foo<T> {
+    fn id(me: T) -> T;
+}
+
+/* note the "missing" for ... (in this case for i64, in order for this to compile) */
+impl Foo<i64> {
+//[e2021]~^ ERROR trait objects must include the `dyn` keyword
+//[e2015]~^^ WARNING trait objects without an explicit `dyn` are deprecated
+//[e2015]~| WARNING trait objects without an explicit `dyn` are deprecated
+//[e2015]~| WARNING this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
+//[e2015]~| WARNING this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
+//[e2015]~| ERROR the trait `Foo` cannot be made into an object
+    fn id(me: i64) -> i64 {me}
+}
+
+fn main() {
+    let x: i64 = <i64 as Foo<i64>>::id(10);
+    //~^ ERROR the trait bound `i64: Foo<i64>` is not satisfied
+    println!("{}", x);
+}