Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rust-lang/rust
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: compiler-errors/rust
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: default-field-value-implicit-param
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 1 commit
  • 5 files changed
  • 1 contributor

Commits on Mar 3, 2025

  1. Copy the full SHA
    cfca91d View commit details
23 changes: 19 additions & 4 deletions compiler/rustc_hir_analysis/src/collect/type_of.rs
Original file line number Diff line number Diff line change
@@ -62,10 +62,25 @@ fn anon_const_type_of<'tcx>(icx: &ItemCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx
return ty;
}

Node::Field(&hir::FieldDef { default: Some(c), def_id: field_def_id, .. })
if c.hir_id == hir_id =>
{
tcx.type_of(field_def_id).instantiate_identity()
Node::Field(&hir::FieldDef {
default: Some(c), def_id: field_def_id, ty: hir_ty, ..
}) if c.hir_id == hir_id => {
let ty = tcx.type_of(field_def_id).instantiate_identity();
if !ty.has_param() || tcx.features().generic_const_exprs() {
ty
} else {
let mut diag = tcx.dcx().struct_span_err(
c.span,
format!("default value for field cannot depend on generic parameters"),
);
diag.span_label(hir_ty.span, "this field references generic parameters");
let adt_def_id = tcx.parent(field_def_id.to_def_id());
let adt = tcx.def_descr(adt_def_id);
diag.span_label(tcx.def_span(adt_def_id), format!("in this {adt}"));
// FIXME: We could give a feature error for GCE, but it's best not to point users
// to that feature since it's broken.
Ty::new_error(tcx, diag.emit())
}
}

_ => Ty::new_error_with_message(
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![feature(default_field_values)]
struct A<'a> { //~ ERROR lifetime parameter `'a` is never used
x: Vec<A> = Vec::new(), //~ ERROR missing lifetime specifier
struct A<'a> {
//~^ ERROR lifetime parameter `'a` is never used
x: Vec<A> = Vec::new(),
//~^ ERROR missing lifetime specifier
//~| ERROR default value for field cannot depend on generic parameters
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0106]: missing lifetime specifier
--> $DIR/do-not-ice-on-invalid-lifetime.rs:3:12
--> $DIR/do-not-ice-on-invalid-lifetime.rs:4:12
|
LL | x: Vec<A> = Vec::new(),
| ^ expected named lifetime parameter
@@ -9,6 +9,17 @@ help: consider using the `'a` lifetime
LL | x: Vec<A<'a>> = Vec::new(),
| ++++

error: default value for field cannot depend on generic parameters
--> $DIR/do-not-ice-on-invalid-lifetime.rs:4:17
|
LL | struct A<'a> {
| ------------ in this struct
LL |
LL | x: Vec<A> = Vec::new(),
| ------ ^^^^^^^^^^
| |
| this field references generic parameters

error[E0392]: lifetime parameter `'a` is never used
--> $DIR/do-not-ice-on-invalid-lifetime.rs:2:10
|
@@ -17,7 +28,7 @@ LL | struct A<'a> {
|
= help: consider removing `'a`, referring to it in a field, or using a marker such as `PhantomData`

error: aborting due to 2 previous errors
error: aborting due to 3 previous errors

Some errors have detailed explanations: E0106, E0392.
For more information about an error, try `rustc --explain E0106`.
16 changes: 16 additions & 0 deletions tests/ui/structs/default-field-values/field-references-param.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Make sure we don't ICE when a field default references a generic parameter via inference.

#![feature(default_field_values)]

struct W<const X: usize>;

impl<const X: usize> W<X> {
const fn new() -> Self { W }
}

struct Z<const X: usize> {
x: W<X> = W::new(),
//~^ ERROR default value for field cannot depend on generic parameters
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error: default value for field cannot depend on generic parameters
--> $DIR/field-references-param.rs:12:15
|
LL | struct Z<const X: usize> {
| ------------------------ in this struct
LL | x: W<X> = W::new(),
| ---- ^^^^^^^^
| |
| this field references generic parameters

error: aborting due to 1 previous error