Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -949,12 +949,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
} else if let Some(impl_did) = impl_did.as_local()
&& let item = self.tcx.hir_expect_item(impl_did)
&& let hir::ItemKind::Impl(item) = item.kind
&& let Some(of_trait) = item.of_trait
&& let hir::ItemKind::Impl(impl_) = item.kind
&& impl_.of_trait.is_some()
{
// trait is const, impl is local and not const
diag.span_suggestion_verbose(
of_trait.trait_ref.path.span.shrink_to_lo(),
item.span.shrink_to_lo(),
format!("make the `impl` of trait `{trait_name}` `const`"),
"const ".to_string(),
Applicability::MaybeIncorrect,
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/comptime/comptime_impl.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ LL | Bar.foo();
|
help: make the `impl` of trait `Foo` `const`
|
LL | impl const Foo for Bar {
| +++++
LL | const impl Foo for Bar {
| +++++

error[E0277]: the trait bound `Bar: const Foo` is not satisfied
--> $DIR/comptime_impl.rs:31:9
Expand All @@ -25,8 +25,8 @@ LL | Bar.bar();
|
help: make the `impl` of trait `Foo` `const`
|
LL | impl const Foo for Bar {
| +++++
LL | const impl Foo for Bar {
| +++++

error: aborting due to 3 previous errors

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/traits/const-traits/assoc-type.current.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ LL | type Bar: [const] Add;
| ^^^^^^^^^^^ required by this bound in `Foo::Bar`
help: make the `impl` of trait `Add` `const`
|
LL | impl const Add for NonConstAdd {
| +++++
LL | const impl Add for NonConstAdd {
| +++++

error: aborting due to 1 previous error

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/traits/const-traits/assoc-type.next.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ LL | type Bar: [const] Add;
| ^^^^^^^^^^^ required by this bound in `Foo::Bar`
help: make the `impl` of trait `Add` `const`
|
LL | impl const Add for NonConstAdd {
| +++++
LL | const impl Add for NonConstAdd {
| +++++

error: aborting due to 1 previous error

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/traits/const-traits/call-const-closure.next.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ LL | (const || ().foo())();
|
help: make the `impl` of trait `Bar` `const`
|
LL | impl const Bar for () {
| +++++
LL | const impl Bar for () {
| +++++

error: aborting due to 1 previous error

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/traits/const-traits/call-const-closure.old.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ LL | (const || ().foo())();
|
help: make the `impl` of trait `Bar` `const`
|
LL | impl const Bar for () {
| +++++
LL | const impl Bar for () {
| +++++

error: aborting due to 1 previous error

Expand Down
30 changes: 30 additions & 0 deletions tests/ui/traits/const-traits/call-const-trait-method-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ pub const trait Plus {
fn plus(self, rhs: Self) -> Self;
}


pub const unsafe trait Minus {
fn minus(self, rhs: Self) -> Self;
}

const impl Plus for i32 {
fn plus(self, rhs: Self) -> Self {
self + rhs
Expand All @@ -17,6 +22,20 @@ impl Plus for u32 {
}
}


const unsafe impl Minus for i32 {
fn minus(self, rhs: Self) -> Self {
self - rhs
}
}

unsafe impl Minus for u32 {
fn minus(self, rhs: Self) -> Self {
self - rhs
}
}


pub const fn add_i32(a: i32, b: i32) -> i32 {
a.plus(b) // ok
}
Expand All @@ -26,4 +45,15 @@ pub const fn add_u32(a: u32, b: u32) -> u32 {
//~^ ERROR the trait bound `u32: [const] Plus`
}


pub const unsafe fn sub_i32(a: i32, b: i32) -> i32 {
a.minus(b) // ok
}

pub const unsafe fn sub_u32(a: u32, b: u32) -> u32 {
a.minus(b)
//~^ ERROR the trait bound `u32: [const] Minus`
}


fn main() {}
19 changes: 15 additions & 4 deletions tests/ui/traits/const-traits/call-const-trait-method-fail.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
error[E0277]: the trait bound `u32: [const] Plus` is not satisfied
--> $DIR/call-const-trait-method-fail.rs:25:5
--> $DIR/call-const-trait-method-fail.rs:44:5
|
LL | a.plus(b)
| ^
|
help: make the `impl` of trait `Plus` `const`
|
LL | impl const Plus for u32 {
| +++++
LL | const impl Plus for u32 {
| +++++

error: aborting due to 1 previous error
error[E0277]: the trait bound `u32: [const] Minus` is not satisfied
--> $DIR/call-const-trait-method-fail.rs:54:5
|
LL | a.minus(b)
| ^
|
help: make the `impl` of trait `Minus` `const`
|
LL | const unsafe impl Minus for u32 {
| +++++

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ LL | const fn equals_self<T: [const] Foo>(t: &T) -> bool {
| ^^^^^^^^^^^ required by this bound in `equals_self`
help: make the `impl` of trait `Foo` `const`
|
LL | impl const Foo for S {
| +++++
LL | const impl Foo for S {
| +++++

error: aborting due to 1 previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ LL | const _: () = assert!(need_const_closure(Tr::a) == 42);
|
help: make the `impl` of trait `Tr` `const`
|
LL | impl const Tr for () {
| +++++
LL | const impl Tr for () {
| +++++

error: aborting due to 1 previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ LL | NonConstImpl.a();
|
help: make the `impl` of trait `ConstDefaultFn` `const`
|
LL | impl const ConstDefaultFn for NonConstImpl {
| +++++
LL | const impl ConstDefaultFn for NonConstImpl {
| +++++

error: aborting due to 1 previous error

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/traits/const-traits/const-drop-fail-2.precise.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ LL | const fn check<T: [const] Destruct>(_: T) {}
| ^^^^^^^^^^^^^^^^ required by this bound in `check`
help: make the `impl` of trait `A` `const`
|
LL | impl const A for NonTrivialDrop {}
| +++++
LL | const impl A for NonTrivialDrop {}
| +++++

error: aborting due to 1 previous error

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/traits/const-traits/const-drop-fail-2.stock.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ LL | const fn check<T: [const] Destruct>(_: T) {}
| ^^^^^^^^^^^^^^^^ required by this bound in `check`
help: make the `impl` of trait `A` `const`
|
LL | impl const A for NonTrivialDrop {}
| +++++
LL | const impl A for NonTrivialDrop {}
| +++++

error: aborting due to 1 previous error

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/traits/const-traits/const-opaque.no.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ LL | const fn bar<T: [const] Foo>(t: T) -> impl [const] Foo {
| ^^^^^^^^^^^ required by this bound in `bar`
help: make the `impl` of trait `Foo` `const`
|
LL | impl const Foo for () {
| +++++
LL | const impl Foo for () {
| +++++

error[E0277]: the trait bound `(): const Foo` is not satisfied
--> $DIR/const-opaque.rs:32:12
Expand All @@ -24,8 +24,8 @@ LL | opaque.method();
|
help: make the `impl` of trait `Foo` `const`
|
LL | impl const Foo for () {
| +++++
LL | const impl Foo for () {
| +++++

error: aborting due to 2 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ LL | const fn foo<T>() where T: [const] Tr {}
| ^^^^^^^^^^ required by this bound in `foo`
help: make the `impl` of trait `Tr` `const`
|
LL | impl const Tr for () {}
| +++++
LL | const impl Tr for () {}
| +++++

error: aborting due to 1 previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ LL | ().a()
|
help: make the `impl` of trait `Tr` `const`
|
LL | impl const Tr for () {}
| +++++
LL | const impl Tr for () {}
| +++++

error: aborting due to 1 previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ LL | type Assoc<T>: [const] Bar
| ^^^^^^^^^^^ required by this bound in `Foo::Assoc`
help: make the `impl` of trait `Bar` `const`
|
LL | impl<T> const Bar for N<T> where T: Bar {}
| +++++
LL | const impl<T> Bar for N<T> where T: Bar {}
| +++++

error[E0277]: the trait bound `T: [const] Bar` is not satisfied
--> $DIR/item-bound-entailment-fails.rs:24:21
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/traits/const-traits/minicore-deref-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ LL | *Ty;
|
help: make the `impl` of trait `Deref` `const`
|
LL | impl const Deref for Ty {
| +++++
LL | const impl Deref for Ty {
| +++++

error: aborting due to 1 previous error

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/traits/const-traits/minicore-fn-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ LL | const fn call_indirect<T: [const] Fn()>(t: &T) { t() }
| ^^^^^^^^^^^^ required by this bound in `call_indirect`
help: make the `impl` of trait `Foo` `const`
|
LL | impl const Foo for () {}
| +++++
LL | const impl Foo for () {}
| +++++

error: aborting due to 1 previous error

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/traits/const-traits/no-explicit-const-params.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ LL | <() as Bar<false>>::bar();
|
help: make the `impl` of trait `Bar` `const`
|
LL | impl const Bar for () {
| +++++
LL | const impl Bar for () {
| +++++

error: aborting due to 5 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ LL | <T as A>::a();
|
help: make the `impl` of trait `A` `const`
|
LL | impl<T: Default> const A for T {
| +++++
LL | const impl<T: Default> A for T {
| +++++

error: aborting due to 1 previous error

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/traits/const-traits/super-traits-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ LL | const impl Bar for S {}
|
help: make the `impl` of trait `Foo` `const`
|
LL | impl const Foo for S {
| +++++
LL | const impl Foo for S {
| +++++

error: aborting due to 1 previous error

Expand Down
Loading