-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
mgca: Propagate errors in const-to-valtree lowering #158401
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ use rustc_data_structures::intern::Interned; | |
| use rustc_error_messages::MultiSpan; | ||
| use rustc_macros::StableHash; | ||
| use rustc_type_ir::walk::TypeWalker; | ||
| use rustc_type_ir::{self as ir, TypeFlags, WithCachedTypeInfo}; | ||
| use rustc_type_ir::{self as ir, TypeFlags, TypeVisitableExt, WithCachedTypeInfo}; | ||
|
|
||
| use crate::mir::interpret::Scalar; | ||
| use crate::ty::{self, Ty, TyCtxt}; | ||
|
|
@@ -116,6 +116,22 @@ impl<'tcx> Const<'tcx> { | |
| Const::new(tcx, ty::ConstKind::Value(ty::Value { ty, valtree })) | ||
| } | ||
|
|
||
| pub fn new_value_from_branches( | ||
| tcx: TyCtxt<'tcx>, | ||
| branches: impl IntoIterator<Item = ty::Const<'tcx>>, | ||
| ty: Ty<'tcx>, | ||
| ) -> Const<'tcx> { | ||
| let branches: Vec<_> = branches.into_iter().collect(); | ||
|
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. Hmm I don't love that this requires collecting the iterator, looking through all the branches, and then passing it on.
Contributor
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 just saw this discussion. Please let me think about it for a while... https://rust-lang.zulipchat.com/#narrow/channel/260443-project-const-generics/topic/const.20generics.20talkies.2013.20jul.202026.201600pm/with/609875464
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. No worries. The TLDR is that we should probably instead make other parts of the compiler handle errors inside valtrees gracefully, rather than avoiding creating valtrees that contain errors in an ad hoc way like this PR does. |
||
| if let Some(e) = branches.iter().find_map(|c| c.error_reported().err()) { | ||
| return Const::new_error(tcx, e); | ||
| } | ||
| if let Err(e) = ty.error_reported() { | ||
| return Const::new_error(tcx, e); | ||
| } | ||
|
|
||
| Const::new_value(tcx, ty::ValTree::from_branches(tcx, branches), ty) | ||
| } | ||
|
|
||
| #[inline] | ||
| pub fn new_expr(tcx: TyCtxt<'tcx>, expr: ty::Expr<'tcx>) -> Const<'tcx> { | ||
| Const::new(tcx, ty::ConstKind::Expr(expr)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Regression test for https://github.com/rust-lang/rust/issues/154632 | ||
|
|
||
| #![feature(generic_const_exprs)] | ||
| #![feature(min_generic_const_args)] | ||
| #![feature(generic_const_items)] | ||
|
|
||
| type const ADD1<const N : usize> : usize = const { N + 1 }; | ||
| //~^ ERROR: unconstrained generic constant | ||
| type const AliasFnUnused: ADD1 = ADD1::<{ Some::<usize> {} }>; | ||
| //~^ ERROR: expected type, found constant `ADD1` [E0573] | ||
| //~| ERROR: struct expression with missing field initialiser for `0` | ||
|
|
||
| fn main() {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| error[E0573]: expected type, found constant `ADD1` | ||
| --> $DIR/type_const-adt-expr-missing-field.rs:9:27 | ||
| | | ||
| LL | type const AliasFnUnused: ADD1 = ADD1::<{ Some::<usize> {} }>; | ||
| | ^^^^ not a type | ||
|
|
||
| error: unconstrained generic constant | ||
| --> $DIR/type_const-adt-expr-missing-field.rs:7:1 | ||
| | | ||
| LL | type const ADD1<const N : usize> : usize = const { N + 1 }; | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: try adding a `where` bound | ||
| | | ||
| LL | type const ADD1<const N : usize> : usize where [(); const { N + 1 }]: = const { N + 1 }; | ||
| | ++++++++++++++++++++++++++++ | ||
|
|
||
| error: struct expression with missing field initialiser for `0` | ||
| --> $DIR/type_const-adt-expr-missing-field.rs:9:43 | ||
| | | ||
| LL | type const AliasFnUnused: ADD1 = ADD1::<{ Some::<usize> {} }>; | ||
| | ^^^^^^^^^^^^^^^^ | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0573`. |
|
Contributor
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. The case reported in the original issue goes through |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #![feature(generic_const_exprs)] | ||
| #![feature(min_generic_const_args)] | ||
| #![feature(adt_const_params)] | ||
| #![feature(generic_const_items)] | ||
|
|
||
| use std::marker::ConstParamTy; | ||
|
|
||
| #[derive(Eq, PartialEq, ConstParamTy)] | ||
| struct Wrap(usize); | ||
|
|
||
| type const ADD1<const N: usize>: usize = const { N + 1 }; | ||
| //~^ ERROR: unconstrained generic constant | ||
| type const AliasFnUnused: ADD1 = ADD1::<{ Wrap(Some::<usize> {}) }>; | ||
| //~^ ERROR: expected type, found constant `ADD1` [E0573] | ||
| //~| ERROR: struct expression with missing field initialiser for `0` | ||
|
|
||
| fn main() {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| error[E0573]: expected type, found constant `ADD1` | ||
| --> $DIR/type_const-tuple-call-expr-missing-field.rs:13:27 | ||
| | | ||
| LL | type const AliasFnUnused: ADD1 = ADD1::<{ Wrap(Some::<usize> {}) }>; | ||
| | ^^^^ not a type | ||
|
|
||
| error: unconstrained generic constant | ||
| --> $DIR/type_const-tuple-call-expr-missing-field.rs:11:1 | ||
| | | ||
| LL | type const ADD1<const N: usize>: usize = const { N + 1 }; | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: try adding a `where` bound | ||
| | | ||
| LL | type const ADD1<const N: usize>: usize where [(); const { N + 1 }]: = const { N + 1 }; | ||
| | ++++++++++++++++++++++++++++ | ||
|
|
||
| error: struct expression with missing field initialiser for `0` | ||
| --> $DIR/type_const-tuple-call-expr-missing-field.rs:13:48 | ||
| | | ||
| LL | type const AliasFnUnused: ADD1 = ADD1::<{ Wrap(Some::<usize> {}) }>; | ||
| | ^^^^^^^^^^^^^^^^ | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0573`. |
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.
I was not able to find a case that reproduces the similar ICE through
lower_const_arg_{array, tuple}()(unlike struct/tuple_call cases I mentioned above, I think there are few situations in which these would cause an ICE under the current evaluation order 🤔 ). However, I don't think we can rule out the possibility that these paths may receive branches containing error constants in the future. Therefore, I think it's reasonable to apply the same fix to these paths as well.View changes since the review