From adde32b245b81bd539790f85ec65376c8b5cb922 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Fri, 26 Jan 2024 23:21:21 +0800 Subject: [PATCH 1/3] Reconstify `Add` --- library/core/src/ops/arith.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/core/src/ops/arith.rs b/library/core/src/ops/arith.rs index bb3cdde66d173..fd50f80474833 100644 --- a/library/core/src/ops/arith.rs +++ b/library/core/src/ops/arith.rs @@ -73,6 +73,7 @@ append_const_msg )] #[doc(alias = "+")] +#[const_trait] pub trait Add { /// The resulting type after applying the `+` operator. #[stable(feature = "rust1", since = "1.0.0")] @@ -94,7 +95,8 @@ pub trait Add { macro_rules! add_impl { ($($t:ty)*) => ($( #[stable(feature = "rust1", since = "1.0.0")] - impl Add for $t { + #[rustc_const_unstable(feature = "const_ops", issue = "90080")] + impl const Add for $t { type Output = $t; #[inline] From b80d44e9b8c53397649a9bee8aead138310620f1 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Sun, 28 Jan 2024 00:20:52 +0800 Subject: [PATCH 2/3] make effect infer variables suggestable in diagnostics it works when a non-const context that does not enable effects calls into a const effects-enabled trait. We'd simply suggest the non-const trait bound in this case consistent to its fallback. --- compiler/rustc_middle/src/ty/diagnostics.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs index c1e2479defd0a..38cdbedc6fd59 100644 --- a/compiler/rustc_middle/src/ty/diagnostics.rs +++ b/compiler/rustc_middle/src/ty/diagnostics.rs @@ -98,6 +98,7 @@ impl<'tcx, T> IsSuggestable<'tcx> for T where T: TypeVisitable> + TypeFoldable>, { + #[tracing::instrument(level = "debug", skip(tcx))] fn is_suggestable(self, tcx: TyCtxt<'tcx>, infer_suggestable: bool) -> bool { self.visit_with(&mut IsSuggestableVisitor { tcx, infer_suggestable }).is_continue() } @@ -533,6 +534,9 @@ impl<'tcx> TypeVisitor> for IsSuggestableVisitor<'tcx> { match c.kind() { ConstKind::Infer(InferConst::Var(_)) if self.infer_suggestable => {} + // effect variables are always suggestable, because they are not visible + ConstKind::Infer(InferConst::EffectVar(_)) => {} + ConstKind::Infer(..) | ConstKind::Bound(..) | ConstKind::Placeholder(..) From 2e1e574ce6a4ed302330a653fa15d149857f0942 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Sun, 28 Jan 2024 00:22:47 +0800 Subject: [PATCH 3/3] fix ui tests these ui changes are closer to what was there before const_trait_impl changes. --- tests/ui/suggestions/invalid-bin-op.stderr | 4 ++++ tests/ui/ufcs/ufcs-qpath-self-mismatch.rs | 1 + tests/ui/ufcs/ufcs-qpath-self-mismatch.stderr | 23 +++++++++++++++---- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/tests/ui/suggestions/invalid-bin-op.stderr b/tests/ui/suggestions/invalid-bin-op.stderr index 2bd745c645a1c..018250c8c1b60 100644 --- a/tests/ui/suggestions/invalid-bin-op.stderr +++ b/tests/ui/suggestions/invalid-bin-op.stderr @@ -16,6 +16,10 @@ help: consider annotating `S` with `#[derive(PartialEq)]` LL + #[derive(PartialEq)] LL | struct S(T); | +help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement + | +LL | pub fn foo(s: S, t: S) where S: PartialEq { + | +++++++++++++++++++++ error: aborting due to 1 previous error diff --git a/tests/ui/ufcs/ufcs-qpath-self-mismatch.rs b/tests/ui/ufcs/ufcs-qpath-self-mismatch.rs index ec86213f8629d..1a07dc46c5e8a 100644 --- a/tests/ui/ufcs/ufcs-qpath-self-mismatch.rs +++ b/tests/ui/ufcs/ufcs-qpath-self-mismatch.rs @@ -4,6 +4,7 @@ fn main() { >::add(1, 2); //~^ ERROR cannot add `u32` to `i32` //~| ERROR cannot add `u32` to `i32` + //~| ERROR cannot add `u32` to `i32` >::add(1u32, 2); //~^ ERROR mismatched types >::add(1, 2u32); diff --git a/tests/ui/ufcs/ufcs-qpath-self-mismatch.stderr b/tests/ui/ufcs/ufcs-qpath-self-mismatch.stderr index ae0a06e6328df..6ea80e100afbb 100644 --- a/tests/ui/ufcs/ufcs-qpath-self-mismatch.stderr +++ b/tests/ui/ufcs/ufcs-qpath-self-mismatch.stderr @@ -11,8 +11,21 @@ LL | >::add(1, 2); <&'a i32 as Add> <&i32 as Add<&i32>> +error[E0277]: cannot add `u32` to `i32` + --> $DIR/ufcs-qpath-self-mismatch.rs:4:5 + | +LL | >::add(1, 2); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no implementation for `i32 + u32` + | + = help: the trait `Add` is not implemented for `i32` + = help: the following other types implement trait `Add`: + + > + <&'a i32 as Add> + <&i32 as Add<&i32>> + error[E0308]: mismatched types - --> $DIR/ufcs-qpath-self-mismatch.rs:7:28 + --> $DIR/ufcs-qpath-self-mismatch.rs:8:28 | LL | >::add(1u32, 2); | ---------------------- ^^^^ expected `i32`, found `u32` @@ -20,7 +33,7 @@ LL | >::add(1u32, 2); | arguments to this function are incorrect | help: the return type of this call is `u32` due to the type of the argument passed - --> $DIR/ufcs-qpath-self-mismatch.rs:7:5 + --> $DIR/ufcs-qpath-self-mismatch.rs:8:5 | LL | >::add(1u32, 2); | ^^^^^^^^^^^^^^^^^^^^^^^----^^^^ @@ -34,7 +47,7 @@ LL | >::add(1i32, 2); | ~~~ error[E0308]: mismatched types - --> $DIR/ufcs-qpath-self-mismatch.rs:9:31 + --> $DIR/ufcs-qpath-self-mismatch.rs:10:31 | LL | >::add(1, 2u32); | ---------------------- ^^^^ expected `i32`, found `u32` @@ -42,7 +55,7 @@ LL | >::add(1, 2u32); | arguments to this function are incorrect | help: the return type of this call is `u32` due to the type of the argument passed - --> $DIR/ufcs-qpath-self-mismatch.rs:9:5 + --> $DIR/ufcs-qpath-self-mismatch.rs:10:5 | LL | >::add(1, 2u32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^----^ @@ -68,7 +81,7 @@ LL | >::add(1, 2); <&'a i32 as Add> <&i32 as Add<&i32>> -error: aborting due to 4 previous errors +error: aborting due to 5 previous errors Some errors have detailed explanations: E0277, E0308. For more information about an error, try `rustc --explain E0277`.