about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTakayuki Maeda <takoyaki0316@gmail.com>2023-11-09 11:36:52 +0900
committerGitHub <noreply@github.com>2023-11-09 11:36:52 +0900
commit4cc549811f95dfc055dbe2d19673ae5ccedff151 (patch)
tree6c0deb0766f4cda5f7b17a0795685d7b6f0d5940
parenta1a8d6fe9cac85d4006155e2db69cb08720ef181 (diff)
parentc17d33f1df9492ad993da3d8b0ddfcfe08bd652e (diff)
downloadrust-4cc549811f95dfc055dbe2d19673ae5ccedff151.tar.gz
rust-4cc549811f95dfc055dbe2d19673ae5ccedff151.zip
Rollup merge of #117645 - compiler-errors:auto-trait-subst, r=petrochenkov
Extend builtin/auto trait args with error when they have >1 argument

Reuse `extend_with_error` to add error args to any auto trait (or built-in trait like `Copy` that is defined incorrectly) that has additional non-`Self` args.

Fixes #117628
-rw-r--r--compiler/rustc_trait_selection/src/traits/select/mod.rs21
-rw-r--r--tests/ui/auto-traits/has-arguments.rs10
-rw-r--r--tests/ui/auto-traits/has-arguments.stderr11
3 files changed, 36 insertions, 6 deletions
diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs
index cf52e6726a1..08208cc6047 100644
--- a/compiler/rustc_trait_selection/src/traits/select/mod.rs
+++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs
@@ -2389,12 +2389,21 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
                         )
                     });
 
-                let obligation = Obligation::new(
-                    self.tcx(),
-                    cause.clone(),
-                    param_env,
-                    ty::TraitRef::new(self.tcx(), trait_def_id, [normalized_ty]),
-                );
+                let tcx = self.tcx();
+                let trait_ref = if tcx.generics_of(trait_def_id).params.len() == 1 {
+                    ty::TraitRef::new(tcx, trait_def_id, [normalized_ty])
+                } else {
+                    // If this is an ill-formed auto/built-in trait, then synthesize
+                    // new error args for the missing generics.
+                    let err_args = ty::GenericArgs::extend_with_error(
+                        tcx,
+                        trait_def_id,
+                        &[normalized_ty.into()],
+                    );
+                    ty::TraitRef::new(tcx, trait_def_id, err_args)
+                };
+
+                let obligation = Obligation::new(self.tcx(), cause.clone(), param_env, trait_ref);
                 obligations.push(obligation);
                 obligations
             })
diff --git a/tests/ui/auto-traits/has-arguments.rs b/tests/ui/auto-traits/has-arguments.rs
new file mode 100644
index 00000000000..f579eb6772d
--- /dev/null
+++ b/tests/ui/auto-traits/has-arguments.rs
@@ -0,0 +1,10 @@
+#![feature(auto_traits)]
+
+auto trait Trait1<'outer> {}
+//~^ ERROR auto traits cannot have generic parameters
+
+fn f<'a>(x: impl Trait1<'a>) {}
+
+fn main() {
+    f("");
+}
diff --git a/tests/ui/auto-traits/has-arguments.stderr b/tests/ui/auto-traits/has-arguments.stderr
new file mode 100644
index 00000000000..3bba74badbc
--- /dev/null
+++ b/tests/ui/auto-traits/has-arguments.stderr
@@ -0,0 +1,11 @@
+error[E0567]: auto traits cannot have generic parameters
+  --> $DIR/has-arguments.rs:3:18
+   |
+LL | auto trait Trait1<'outer> {}
+   |            ------^^^^^^^^ help: remove the parameters
+   |            |
+   |            auto trait cannot have generic parameters
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0567`.