Skip to content

Commit

Permalink
reword messages and add more test cases for stable/nightly and featur…
Browse files Browse the repository at this point in the history
…e enabled/disabled
  • Loading branch information
estebank committed Dec 9, 2024
1 parent a5c24ef commit 853ca8c
Show file tree
Hide file tree
Showing 36 changed files with 732 additions and 93 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_hir_analysis/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ hir_analysis_coercion_between_struct_single_note = expected a single field to be
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 = mark `{$trait_name}` as `const`
.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 = not marked with `#[const_trait]`
.suggestion = mark `{$trait_name}` as `const`
.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 @@ -1637,11 +1637,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
2 changes: 2 additions & 0 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ pub(crate) struct ConstImplForNonConstTrait {
style = "verbose"
)]
pub local_trait_span: Option<Span>,
pub suggestion_pre: &'static str,
#[note]
pub marking: (),
#[note(hir_analysis_adding)]
Expand All @@ -554,6 +555,7 @@ pub(crate) struct ConstBoundForNonConstTrait {
pub modifier: &'static str,
#[note]
pub def_span: Option<Span>,
pub suggestion_pre: &'static str,
#[suggestion(
applicability = "machine-applicable",
code = "#[const_trait] ",
Expand Down
19 changes: 14 additions & 5 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,16 +737,25 @@ 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) = if trait_def_id.is_local() {
(None, Some(tcx.def_span(trait_def_id).shrink_to_lo()))
} else {
(Some(tcx.def_span(trait_def_id)), None)
};
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`.
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() {}
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 dev 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`.
51 changes: 51 additions & 0 deletions tests/run-make/const-trait-stable-toolchain/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Test output of const super trait errors in both stable and nightly.
// We don't want to provide suggestions on stable that only make sense in nightly.

use run_make_support::{diff, rustc};

fn main() {
let out = rustc()
.input("const-super-trait.rs")
.env("RUSTC_BOOTSTRAP", "-1")
.cfg("feature_enabled")
.run_fail()
.assert_stderr_not_contains("as `#[const_trait]` to allow it to have `const` implementations")
.stderr_utf8();
diff()
.expected_file("const-super-trait.stable.enabled.stderr")
.actual_text("(rustc)", &out)
.run();
let out = rustc()
.input("const-super-trait.rs")
.cfg("feature_enabled")
.ui_testing()
.run_fail()
.assert_stderr_not_contains("enable `#![feature(const_trait_impl)]` in your crate and mark")
.assert_stderr_contains("as `#[const_trait]` to allow it to have `const` implementations")
.stderr_utf8();
diff()
.expected_file("const-super-trait.nightly.enabled.stderr")
.actual_text("(rustc)", &out)
.run();
let out = rustc()
.input("const-super-trait.rs")
.env("RUSTC_BOOTSTRAP", "-1")
.run_fail()
.assert_stderr_not_contains("enable `#![feature(const_trait_impl)]` in your crate and mark")
.assert_stderr_not_contains("as `#[const_trait]` to allow it to have `const` implementations")
.stderr_utf8();
diff()
.expected_file("const-super-trait.stable.disabled.stderr")
.actual_text("(rustc)", &out)
.run();
let out = rustc()
.input("const-super-trait.rs")
.ui_testing()
.run_fail()
.assert_stderr_contains("enable `#![feature(const_trait_impl)]` in your crate and mark")
.stderr_utf8();
diff()
.expected_file("const-super-trait.nightly.disabled.stderr")
.actual_text("(rustc)", &out)
.run();
}
Loading

0 comments on commit 853ca8c

Please sign in to comment.