-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Do not panic in ty::consts::Const::try_to_target_usize()
in case of size mismatch
#126382
Conversation
r? @davidtwco rustbot has assigned @davidtwco. Use |
We intentionally started ICEing here because it is genuinely a bug to be doing this. This PR Is effectively a partial revert of #126159. Ideally what should be happening is we should be tainting errors somewhere so that we can check for |
Looking it into it a little bit the problem is that edit: wrote a bit more in the actual issue itself, should read that too |
r? @BoxyUwU |
--> $DIR/ice-const-size-relate-126359.rs:12:39 | ||
| | ||
LL | let _ = OppOrder::<3, u32> { arr: [0, 0, 0] }; | ||
| ^^^^^^^^^ expected `3`, found `3` |
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.
This diagnostic still looks buggy.
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.
Yeah, it should be suppressed. Will look into it
Yeah, as you said for most users of The special situation here is that this is called in an error path. |
Yeah, that's why I felt the solution was simply to prevent the panic. First I thought I should make the change here in rust/compiler/rustc_type_ir/src/relate.rs Lines 502 to 514 in 56e112a
but then it seemed easier to do it inside |
I don't think |
@rustbot author |
= note: expected array `[u32; 3]` | ||
found array `[u32; 3]` |
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.
wooondering if its worth having a debug_assert
against this kind of stuff ('note: expected X, found Y' where x.display() == y.display()) because that just seems silly from a diagnostic point of view 😅
10bc842
to
f67d045
Compare
I have undone my changes to As for the confusing diagnostics:
and
I'll tackle them later in a separate PR. @rustbot review |
This comment has been minimized.
This comment has been minimized.
f67d045
to
a1a0865
Compare
Occurred due to an overdue rebase. Rebased now. |
That seems potentially quite confusing if there are two #126159 was written under the assumption that the compiler can reliably avoid feeding ill-typed programs to later phases. If that is not the case, maybe we should go back to having the |
What if we rename the |
Is it clear where one would want to call which function? Sounds a bit like the one on the inherent impl is just an ICE waiting to happen, if the compiler feeds ill-typed terms so deep into the type system. Is it clear why that happens / why it cannot be prevented? Boxy wrote above "Looking it into it a little bit the problem is that super_relate_tys for TyKind::Array has diagnostic logic in it that unconditionally evaluates the array length to a usize. This codepath should correctly handle length arguments that are incorrectly typed instead of assuming they can be evaluated to a usize." Have you dug to the bottom of why that does not work? |
The only place the rust/compiler/rustc_type_ir/src/relate.rs Lines 499 to 517 in 89aefb9
A simple solution would be to remove this method from the trait and expose the underlying method Currently fn try_to_target_usize(self, interner: TyCtxt<'tcx>) -> Option<u64> {
let scalar = self.try_to_valtree()?.try_to_scalar_int()?;
if scalar.size() != interner.data_layout.pointer_size {
return None;
}
Some(scalar.to_target_usize(interner))
} If we expose |
Will try and get to this in the next couple of days, didn't realise there had been a bunch more conversation |
Ah, there's the extra abstraction layer in
That does sound like a good plan to me, but I don't know if it is possible with the |
I find the Regardless, I do not believe making |
I'm a bit confused by this comment.^^
So |
Argh sorry, I misremembered and what I said is wrong. The key difference is between I am not entirely sure why the non-evaluating functions even exist, probably that's for when you don't have a ParamEnv? If you have a param_env, I think you should always call the I agree the |
Cc @oli-obk , maybe you have an idea? |
I think it'd probably be good to open an issue or a zulip thread to talk about this properly rather than having the discussion on this PR since I don't think any progress there is actually going to solve the ICE. |
a1a0865
to
ef1925a
Compare
This comment has been minimized.
This comment has been minimized.
rust/compiler/rustc_middle/src/ty/error.rs Lines 62 to 69 in b5723af
If we change To prevent confusion we can give this new method a different name like |
Actually it's used at a third location as well which is here: rust/compiler/rustc_type_ir/src/error.rs Line 77 in b5723af
but no const evaluation is required there so it's not relevant. |
Can we avoid even getting to this method call by some appropriate tainting elsewhere? |
ef1925a
to
8974c7e
Compare
Thanks. I have now implemented the approach to catch the error earlier in @rustbot review |
@@ -1402,13 +1402,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |||
expr: &'tcx hir::Expr<'tcx>, | |||
) -> Ty<'tcx> { | |||
let element_ty = if !args.is_empty() { | |||
let mut size_ct = None; |
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.
This isn't a sufficient fix. The expectation may always be None
, typically by just separating the expression from the place that gives that expectation. For example, I believe this still ICEs:
struct OppOrder<const N: u8 = 3, T = u32> {
arr: [T; N],
}
fn main() {
let arr = [0, 0, 0];
let _ = OppOrder::<3, u32> { arr };
}
I want to echo Boxy's suggestions about making @rustbot author |
Hi, sorry for this being kind of a bad contribution experience with a lot of back and forth on what's being asked of you :< I'm going to close this since there's not been any activity for a month but you're more than welcome to re-open this PR if you're interested in continuing this (though make sure to re-open before pushing any new commits if you do decide to) |
No worries @BoxyUwU and @compiler-errors. Received a lot of valuable feedback here. Apologies for not being responsive. I'm a little busy with some other stuff these days. Will certainly return to it later. Thank you :) |
Fixes #126359
This is the failing code:
The ICE occurred while unifying the type of array
[0, 0, 0]
withOppOrder::<3, u32>.arr
field.arr
's type is malformed because it hasu8
for size instead ofusize
. So when we try to unifyusize
withu8
a method inValtree
panics causing the ICE.This PR makes
ty::consts::Const::try_to_target_usize
, which callsValtree
methods, check for size mismatch and gracefully returnNone
instead of callingValtree
.Edit
Now changed the approach to catch wrongly typed array size in
FnCtxt::check_expr_array
so that we never reach a point where we try to unifyusize
withu8
.