Skip to content

Commit

Permalink
Unrolled build for rust-lang#134094
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#134094 - estebank:const-trait-errors, r=compiler-errors

Tweak wording of non-const traits used as const bounds

Use verbose suggestions and add additional labels/notes.

r? ``@compiler-errors``
  • Loading branch information
rust-timer authored Dec 11, 2024
2 parents 33c245b + 4007fc9 commit 3aba02d
Show file tree
Hide file tree
Showing 49 changed files with 1,122 additions and 195 deletions.
14 changes: 8 additions & 6 deletions compiler/rustc_hir_analysis/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ hir_analysis_coercion_between_struct_same_note = expected coercion between the s
hir_analysis_coercion_between_struct_single_note = expected a single field to be coerced, none found
hir_analysis_const_bound_for_non_const_trait =
`{$modifier}` can only be applied to `#[const_trait]` traits
hir_analysis_const_impl_for_non_const_trait =
const `impl` for trait `{$trait_name}` which is not marked with `#[const_trait]`
.suggestion = mark `{$trait_name}` as const
hir_analysis_const_bound_for_non_const_trait = `{$modifier}` can only be applied to `#[const_trait]` traits
.label = can't be applied to `{$trait_name}`
.note = `{$trait_name}` can't be used with `{$modifier}` because it isn't annotated with `#[const_trait]`
.suggestion = {$suggestion_pre}mark `{$trait_name}` as `#[const_trait]` to allow it to have `const` implementations
hir_analysis_const_impl_for_non_const_trait = const `impl` for trait `{$trait_name}` which is not marked with `#[const_trait]`
.label = this trait is not `const`
.suggestion = {$suggestion_pre}mark `{$trait_name}` as `#[const_trait]` to allow it to have `const` implementations
.note = marking a trait with `#[const_trait]` ensures all default method bodies are `const`
.adding = adding a non-const method body in the future would be a breaking change
Expand Down
16 changes: 14 additions & 2 deletions compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1638,11 +1638,23 @@ fn check_impl_constness(
}

let trait_name = tcx.item_name(trait_def_id).to_string();
let (local_trait_span, suggestion_pre) =
match (trait_def_id.is_local(), tcx.sess.is_nightly_build()) {
(true, true) => (
Some(tcx.def_span(trait_def_id).shrink_to_lo()),
if tcx.features().const_trait_impl() {
""
} else {
"enable `#![feature(const_trait_impl)]` in your crate and "
},
),
(false, _) | (_, false) => (None, ""),
};
Some(tcx.dcx().emit_err(errors::ConstImplForNonConstTrait {
trait_ref_span: hir_trait_ref.path.span,
trait_name,
local_trait_span:
trait_def_id.as_local().map(|_| tcx.def_span(trait_def_id).shrink_to_lo()),
local_trait_span,
suggestion_pre,
marking: (),
adding: (),
}))
Expand Down
19 changes: 18 additions & 1 deletion compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,16 @@ pub(crate) struct GenericArgsOnOverriddenImpl {
#[diag(hir_analysis_const_impl_for_non_const_trait)]
pub(crate) struct ConstImplForNonConstTrait {
#[primary_span]
#[label]
pub trait_ref_span: Span,
pub trait_name: String,
#[suggestion(applicability = "machine-applicable", code = "#[const_trait]")]
#[suggestion(
applicability = "machine-applicable",
code = "#[const_trait] ",
style = "verbose"
)]
pub local_trait_span: Option<Span>,
pub suggestion_pre: &'static str,
#[note]
pub marking: (),
#[note(hir_analysis_adding)]
Expand All @@ -544,8 +550,19 @@ pub(crate) struct ConstImplForNonConstTrait {
#[diag(hir_analysis_const_bound_for_non_const_trait)]
pub(crate) struct ConstBoundForNonConstTrait {
#[primary_span]
#[label]
pub span: Span,
pub modifier: &'static str,
#[note]
pub def_span: Option<Span>,
pub suggestion_pre: &'static str,
#[suggestion(
applicability = "machine-applicable",
code = "#[const_trait] ",
style = "verbose"
)]
pub suggestion: Option<Span>,
pub trait_name: String,
}

#[derive(Diagnostic)]
Expand Down
17 changes: 17 additions & 0 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,9 +737,26 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
if let hir::BoundConstness::Always(span) | hir::BoundConstness::Maybe(span) = constness
&& !self.tcx().is_const_trait(trait_def_id)
{
let (def_span, suggestion, suggestion_pre) =
match (trait_def_id.is_local(), self.tcx().sess.is_nightly_build()) {
(true, true) => (
None,
Some(tcx.def_span(trait_def_id).shrink_to_lo()),
if self.tcx().features().const_trait_impl() {
""
} else {
"enable `#![feature(const_trait_impl)]` in your crate and "
},
),
(false, _) | (_, false) => (Some(tcx.def_span(trait_def_id)), None, ""),
};
self.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
span,
modifier: constness.as_str(),
def_span,
trait_name: self.tcx().def_path_str(trait_def_id),
suggestion_pre,
suggestion,
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
error: `~const` is not allowed here
--> const-super-trait.rs:7:12
|
LL | trait Bar: ~const Foo {}
| ^^^^^^
|
note: this trait is not a `#[const_trait]`, so it cannot have `~const` trait bounds
--> const-super-trait.rs:7:1
|
LL | trait Bar: ~const Foo {}
| ^^^^^^^^^^^^^^^^^^^^^^^^

error[E0658]: const trait impls are experimental
--> const-super-trait.rs:7:12
|
LL | trait Bar: ~const Foo {}
| ^^^^^^
|
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: const trait impls are experimental
--> const-super-trait.rs:9:17
|
LL | const fn foo<T: ~const Bar>(x: &T) {
| ^^^^^^
|
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: `~const` can only be applied to `#[const_trait]` traits
--> const-super-trait.rs:7:12
|
LL | trait Bar: ~const Foo {}
| ^^^^^^ can't be applied to `Foo`
|
help: enable `#![feature(const_trait_impl)]` in your crate and mark `Foo` as `#[const_trait]` to allow it to have `const` implementations
|
LL | #[const_trait] trait Foo {
| ++++++++++++++

error: `~const` can only be applied to `#[const_trait]` traits
--> const-super-trait.rs:9:17
|
LL | const fn foo<T: ~const Bar>(x: &T) {
| ^^^^^^ can't be applied to `Bar`
|
help: enable `#![feature(const_trait_impl)]` in your crate and mark `Bar` as `#[const_trait]` to allow it to have `const` implementations
|
LL | #[const_trait] trait Bar: ~const Foo {}
| ++++++++++++++

error[E0015]: cannot call non-const fn `<T as Foo>::a` in constant functions
--> const-super-trait.rs:10:7
|
LL | x.a();
| ^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error: aborting due to 6 previous errors

Some errors have detailed explanations: E0015, E0658.
For more information about an error, try `rustc --explain E0015`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
error: `~const` is not allowed here
--> const-super-trait.rs:7:12
|
LL | trait Bar: ~const Foo {}
| ^^^^^^
|
note: this trait is not a `#[const_trait]`, so it cannot have `~const` trait bounds
--> const-super-trait.rs:7:1
|
LL | trait Bar: ~const Foo {}
| ^^^^^^^^^^^^^^^^^^^^^^^^

error: `~const` can only be applied to `#[const_trait]` traits
--> const-super-trait.rs:7:12
|
LL | trait Bar: ~const Foo {}
| ^^^^^^ can't be applied to `Foo`
|
help: mark `Foo` as `#[const_trait]` to allow it to have `const` implementations
|
LL | #[const_trait] trait Foo {
| ++++++++++++++

error: `~const` can only be applied to `#[const_trait]` traits
--> const-super-trait.rs:9:17
|
LL | const fn foo<T: ~const Bar>(x: &T) {
| ^^^^^^ can't be applied to `Bar`
|
help: mark `Bar` as `#[const_trait]` to allow it to have `const` implementations
|
LL | #[const_trait] trait Bar: ~const Foo {}
| ++++++++++++++

error[E0015]: cannot call non-const fn `<T as Foo>::a` in constant functions
--> const-super-trait.rs:10:7
|
LL | x.a();
| ^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0015`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
error: `~const` is not allowed here
--> const-super-trait.rs:7:12
|
7 | trait Bar: ~const Foo {}
| ^^^^^^
|
note: this trait is not a `#[const_trait]`, so it cannot have `~const` trait bounds
--> const-super-trait.rs:7:1
|
7 | trait Bar: ~const Foo {}
| ^^^^^^^^^^^^^^^^^^^^^^^^

error[E0658]: const trait impls are experimental
--> const-super-trait.rs:7:12
|
7 | trait Bar: ~const Foo {}
| ^^^^^^
|
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information

error[E0658]: const trait impls are experimental
--> const-super-trait.rs:9:17
|
9 | const fn foo<T: ~const Bar>(x: &T) {
| ^^^^^^
|
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information

error: `~const` can only be applied to `#[const_trait]` traits
--> const-super-trait.rs:7:12
|
7 | trait Bar: ~const Foo {}
| ^^^^^^ can't be applied to `Foo`
|
note: `Foo` can't be used with `~const` because it isn't annotated with `#[const_trait]`
--> const-super-trait.rs:3:1
|
3 | trait Foo {
| ^^^^^^^^^

error: `~const` can only be applied to `#[const_trait]` traits
--> const-super-trait.rs:9:17
|
9 | const fn foo<T: ~const Bar>(x: &T) {
| ^^^^^^ can't be applied to `Bar`
|
note: `Bar` can't be used with `~const` because it isn't annotated with `#[const_trait]`
--> const-super-trait.rs:7:1
|
7 | trait Bar: ~const Foo {}
| ^^^^^^^^^^^^^^^^^^^^^

error[E0015]: cannot call non-const fn `<T as Foo>::a` in constant functions
--> const-super-trait.rs:10:7
|
10 | x.a();
| ^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error: aborting due to 6 previous errors

Some errors have detailed explanations: E0015, E0658.
For more information about an error, try `rustc --explain E0015`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
error: `~const` is not allowed here
--> const-super-trait.rs:7:12
|
7 | trait Bar: ~const Foo {}
| ^^^^^^
|
note: this trait is not a `#[const_trait]`, so it cannot have `~const` trait bounds
--> const-super-trait.rs:7:1
|
7 | trait Bar: ~const Foo {}
| ^^^^^^^^^^^^^^^^^^^^^^^^

error[E0554]: `#![feature]` may not be used on the NIGHTLY release channel
--> const-super-trait.rs:1:30
|
1 | #![cfg_attr(feature_enabled, feature(const_trait_impl))]
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: `~const` can only be applied to `#[const_trait]` traits
--> const-super-trait.rs:7:12
|
7 | trait Bar: ~const Foo {}
| ^^^^^^ can't be applied to `Foo`
|
note: `Foo` can't be used with `~const` because it isn't annotated with `#[const_trait]`
--> const-super-trait.rs:3:1
|
3 | trait Foo {
| ^^^^^^^^^

error: `~const` can only be applied to `#[const_trait]` traits
--> const-super-trait.rs:9:17
|
9 | const fn foo<T: ~const Bar>(x: &T) {
| ^^^^^^ can't be applied to `Bar`
|
note: `Bar` can't be used with `~const` because it isn't annotated with `#[const_trait]`
--> const-super-trait.rs:7:1
|
7 | trait Bar: ~const Foo {}
| ^^^^^^^^^^^^^^^^^^^^^

error[E0015]: cannot call non-const fn `<T as Foo>::a` in constant functions
--> const-super-trait.rs:10:7
|
10 | x.a();
| ^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error: aborting due to 5 previous errors

Some errors have detailed explanations: E0015, E0554.
For more information about an error, try `rustc --explain E0015`.
13 changes: 13 additions & 0 deletions tests/run-make/const-trait-stable-toolchain/const-super-trait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![cfg_attr(feature_enabled, feature(const_trait_impl))]

trait Foo {
fn a(&self);
}

trait Bar: ~const Foo {}

const fn foo<T: ~const Bar>(x: &T) {
x.a();
}

fn main() {}
Loading

0 comments on commit 3aba02d

Please sign in to comment.