Repository navigation
Make in-scope non-region params inhibit the evaluation of default field values #163182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,13 @@ | ||
| #![feature(default_field_values, generic_const_exprs)] | ||
| #![allow(incomplete_features)] | ||
| #![feature(default_field_values)] | ||
|
|
||
| pub struct Bat { | ||
| pub bax: u8 = panic!("asdf"), | ||
| //~^ ERROR evaluation panicked: asdf | ||
| pub bax: u8 = panic!("asdf"), //~ ERROR evaluation panicked: asdf | ||
| pub bat: u8 = 130 + 130, //~ ERROR attempt to compute `130_u8 + 130_u8`, which would overflow | ||
| } | ||
|
|
||
| pub struct Baz<const C: u8> { | ||
| pub bax: u8 = 130 + C, // ok | ||
| pub bat: u8 = 130 + 130, | ||
| //~^ ERROR attempt to compute `130_u8 + 130_u8`, which would overflow | ||
|
Comment on lines
-11
to
-12
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would still want us to stabilize this feature with some way this check still happens... Either way, we can probably land this change by adding a FIXME in the test and the code?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather not ship this PR with a new stabilization-blocking FIXME since that wouldn't buy us anything, as we'd just kick the can down the road. I'd prefer to settle this either in this PR or in an alternative PR that would get shipped instead. After all, this topic is the only known major blocker, right? I'm open to alternative proposals from you, T-types or anyone else (as long as they're principled :P).
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know of a way to check for unconditionally panic-ing too-generic code, other than things like the unconditional panic lint which I would have expected to catch something like this? 🤔 I think the only principled way to avoid this is too not support generic default values but that seems like it would kinda screw the usability of the feature 🤔
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cop-out solution: Explicit lint on structs with generics and default field values. You must explicitly allow it as a signal that "yes, I am aware that these consts are only evaluated when used". It will make the feature more annoying to use because of attribute, but at least then it will be clear. Edit: I just checked, and unconditionally doing It feels to me that we can do if let Some(def_id) = field.value {
if let Err(ErrorHandled::TooGeneric(span)) = tcx.const_eval_poly(def_id) {
let field_span = tcx.def_span(def_id);
let mut multispan: MultiSpan =
if span.is_dummy() { field_span.into() } else { span.into() };
multispan.push_span_label(field_span, "unevaluated default value");
multispan.push_span_context(tcx.def_span(field.did));
multispan.push_span_context(tcx.def_span(item).shrink_to_lo());
tcx.dcx().span_warn( // FIXME: turn into a lint
multispan,
format!(
"field `{}` has a default value that is only checked when a value \
of `{}` is constructed",
field.name,
tcx.def_path_str(item),
),
);
}
}and get pretty much the behavior we'd want: What do people think? (I'd love it if we improved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| pub bat: u8 = 130 + 130, // ok | ||
| pub bay: u8 = 1, // ok | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LazyCellprobably overkill/unnecessary -- gonna perf this at some pointView changes since the review