Skip to content

Commit

Permalink
Unconditionally error at definition if default field value has const …
Browse files Browse the repository at this point in the history
…errors

Emit E0080 always on struct definition with default fields that have unconditional const errors and remove `default_field_always_invalid_const` lint.
  • Loading branch information
estebank committed Dec 6, 2024
1 parent 7e45ae6 commit 8da8084
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 148 deletions.
17 changes: 17 additions & 0 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,23 @@ fn check_type_defn<'tcx>(
for variant in variants.iter() {
// All field types must be well-formed.
for field in &variant.fields {
if let Some(def_id) = field.value
&& let Some(_ty) = tcx.type_of(def_id).no_bound_vars()
{
if let Some(def_id) = def_id.as_local()
&& let hir::Node::AnonConst(anon) = tcx.hir_node_by_def_id(def_id)
&& let expr = &tcx.hir().body(anon.body).value
&& let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
&& let Res::Def(DefKind::ConstParam, _def_id) = path.res
{
// Do not evaluate bare `const` params, as those would ICE and are only
// usable if `#![feature(generic_const_exprs)]` is enabled.
} else {
// Evaluate the constant proactively, to emit an error if the constant has
// an unconditional error. We only do so if the const has no type params.
let _ = tcx.const_eval_poly(def_id.into());
}
}
let field_id = field.did.expect_local();
let hir::FieldDef { ty: hir_ty, .. } =
tcx.hir_node_by_def_id(field_id).expect_field();
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,6 @@ lint_dangling_pointers_from_temporaries = a dangling pointer will be produced be
.note = pointers do not have a lifetime; when calling `{$callee}` the `{$ty}` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
.help = for more information, see <https://doc.rust-lang.org/reference/destructors.html>
lint_default_field_always_invalid_const = default field fails const-evaluation
.label = this field's constant fails const-evaluation, as seen in the previous error
.help = you can skip const-evaluation of default fields by enabling this lint
lint_default_hash_types = prefer `{$preferred}` over `{$used}`, it has better performance
.note = a `use rustc_data_structures::fx::{$preferred}` may be necessary
Expand Down
91 changes: 0 additions & 91 deletions compiler/rustc_lint/src/default_field_always_invalid.rs

This file was deleted.

3 changes: 0 additions & 3 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ mod async_fn_in_trait;
pub mod builtin;
mod context;
mod dangling;
mod default_field_always_invalid;
mod deref_into_dyn_supertrait;
mod drop_forget_useless;
mod early;
Expand Down Expand Up @@ -86,7 +85,6 @@ use async_closures::AsyncClosureUsage;
use async_fn_in_trait::AsyncFnInTrait;
use builtin::*;
use dangling::*;
use default_field_always_invalid::*;
use deref_into_dyn_supertrait::*;
use drop_forget_useless::*;
use enum_intrinsics_non_enums::EnumIntrinsicsNonEnums;
Expand Down Expand Up @@ -195,7 +193,6 @@ late_lint_methods!(
DropForgetUseless: DropForgetUseless,
ImproperCTypesDeclarations: ImproperCTypesDeclarations,
ImproperCTypesDefinitions: ImproperCTypesDefinitions,
DefaultFieldAlwaysInvalid: DefaultFieldAlwaysInvalid,
InvalidFromUtf8: InvalidFromUtf8,
VariantSizeDifferences: VariantSizeDifferences,
PathStatements: PathStatements,
Expand Down
9 changes: 0 additions & 9 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,15 +730,6 @@ pub(crate) struct UndroppedManuallyDropsSuggestion {
pub end_span: Span,
}

#[derive(LintDiagnostic)]
#[diag(lint_default_field_always_invalid_const)]
pub(crate) struct DefaultFieldAlwaysInvalidConst {
#[label]
pub span: Span,
#[help]
pub help: (),
}

// invalid_from_utf8.rs
#[derive(LintDiagnostic)]
pub(crate) enum InvalidFromUtf8Diag {
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/structs/default-field-values-failures.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ error: default fields are not supported in tuple structs
LL | pub struct Rak(i32 = 42);
| ^^ default fields are only supported on structs

error: generic `Self` types are currently not permitted in anonymous constants
--> $DIR/default-field-values-failures.rs:20:14
|
LL | bar: S = Self::S,
| ^^^^

error[E0277]: the trait bound `S: Default` is not satisfied
--> $DIR/default-field-values-failures.rs:14:5
|
Expand Down Expand Up @@ -106,12 +112,6 @@ LL - let _ = Rak(.., 0);
LL + let _ = Rak(0);
|

error: generic `Self` types are currently not permitted in anonymous constants
--> $DIR/default-field-values-failures.rs:20:14
|
LL | bar: S = Self::S,
| ^^^^

error: aborting due to 9 previous errors

Some errors have detailed explanations: E0061, E0277, E0308.
Expand Down
3 changes: 0 additions & 3 deletions tests/ui/structs/default-field-values-invalid-const.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#![feature(default_field_values, generic_const_exprs)]
#![allow(incomplete_features)]

#[warn(default_field_always_invalid_const)] //~ WARN lint `default_field_always_invalid_const` can't be warned on
pub struct Bat {
pub bax: u8 = panic!("asdf"),
//~^ ERROR evaluation of constant value failed
//~| WARN default field fails const-evaluation
}

pub struct Baz<const C: u8> {
pub bax: u8 = 130 + C, // ok
pub bat: u8 = 130 + 130,
//~^ ERROR evaluation of `Baz::<C>::bat::{constant#0}` failed
//~| ERROR default field fails const-evaluation
pub bay: u8 = 1, // ok
}

Expand Down
36 changes: 4 additions & 32 deletions tests/ui/structs/default-field-values-invalid-const.stderr
Original file line number Diff line number Diff line change
@@ -1,45 +1,17 @@
warning: lint `default_field_always_invalid_const` can't be warned on
--> $DIR/default-field-values-invalid-const.rs:4:8
|
LL | #[warn(default_field_always_invalid_const)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ either `deny` or `allow`, no other lint level is supported for this lint

error[E0080]: evaluation of constant value failed
--> $DIR/default-field-values-invalid-const.rs:6:19
--> $DIR/default-field-values-invalid-const.rs:5:19
|
LL | pub bax: u8 = panic!("asdf"),
| ^^^^^^^^^^^^^^ the evaluated program panicked at 'asdf', $DIR/default-field-values-invalid-const.rs:6:19
| ^^^^^^^^^^^^^^ the evaluated program panicked at 'asdf', $DIR/default-field-values-invalid-const.rs:5:19
|
= note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: default field fails const-evaluation
--> $DIR/default-field-values-invalid-const.rs:6:5
|
LL | pub bax: u8 = panic!("asdf"),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this field's constant fails const-evaluation, as seen in the previous error
|
= help: you can skip const-evaluation of default fields by enabling this lint
note: the lint level is defined here
--> $DIR/default-field-values-invalid-const.rs:4:8
|
LL | #[warn(default_field_always_invalid_const)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0080]: evaluation of `Baz::<C>::bat::{constant#0}` failed
--> $DIR/default-field-values-invalid-const.rs:13:19
--> $DIR/default-field-values-invalid-const.rs:11:19
|
LL | pub bat: u8 = 130 + 130,
| ^^^^^^^^^ attempt to compute `130_u8 + 130_u8`, which would overflow

error: default field fails const-evaluation
--> $DIR/default-field-values-invalid-const.rs:13:5
|
LL | pub bat: u8 = 130 + 130,
| ^^^^^^^^^^^^^^^^^^^^^^^ this field's constant fails const-evaluation, as seen in the previous error
|
= help: you can skip const-evaluation of default fields by enabling this lint
= note: `#[deny(default_field_always_invalid_const)]` on by default

error: aborting due to 3 previous errors; 2 warnings emitted
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0080`.

0 comments on commit 8da8084

Please sign in to comment.