You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following output should only emit the error once:
#![warn(const_err)]fnmain(){&{[1,2,3][4]};}
error: index out of bounds: the len is 3 but the index is 4
--> $DIR/array-literal-index-oob.rs:2:7
|
LL | &{[1, 2, 3][4]};
| ^^^^^^^^^^^^
|
= note: `#[deny(const_err)]` on by default
error: reaching this expression at runtime will panic or abort
--> $DIR/array-literal-index-oob.rs:2:7
|
LL | &{[1, 2, 3][4]};
| --^^^^^^^^^^^^-
| |
| index out of bounds: the len is 3 but the index is 4
error: aborting due to 2 previous errors
changed the title [-]3 duplicated errors on index out of bound const evaluation instead[/-][+]Duplicated errors on index out of bound const evaluation instead[/+]on Sep 1, 2019
changed the title [-]Duplicated errors on index out of bound const evaluation instead[/-][+]Duplicated errors on index out of bound const evaluation[/+]on Sep 8, 2019
One of the two errors is coming from evaluating the Assert MIR terminator that does the bounds-check and panics if it fails. The other message comes from actually evaluating the place[4] even though the index is out-of-bounds.
What is surprising here is that the evaluation of the place expression even happens even though that should be dead code. @oli-obk@wesleywiser any idea?
I don't think const_err should include promoteds, since any checks for anything they might do are still in the runtime code (doing anything else would require something much more complex than what we have today).
But also, if we constant-fold the assert, we can know that using the promoted is dead code (I guess this doesn't apply to codegen generating those warnings instead of constant folding).
@eddyb so basically, const_prop should cease to emit warnings when it is working inside a promoted? Or maybe it shouldn't run there at all?
Cc @wesleywiser
Actually, I think the first ("error: index out of bounds") is not a warning in any meaningful way... trying to evaluate that promoted fails with a hard const-error ("UB: OOB array access") and produces no result. I does not seem wise to me to not report such hard errors.
I am pretty sure the "reaching this expression at runtime will panic or abort" part is not from inside the promoted. That one gets raised by const-prop when partially evaluating the Assert terminator that is left in the fn.
So @eddyb I don't think any of your proposals so far actually solves this problem. Or do you suggest we ignore UB in promoteds?
If we want to be clever we could only check promoteds that are still reachable after constant folding + simplify_cfg.
Because the Assert guarding the use of the erroring promoted would have become unconditional.
Actually, do we have an unconditional Panic terminator?
Activity
estebank commentedon Aug 27, 2019
[-]3 duplicated errors on index out of bound const evaluation instead[/-][+]Duplicated errors on index out of bound const evaluation instead[/+][-]Duplicated errors on index out of bound const evaluation instead[/-][+]Duplicated errors on index out of bound const evaluation[/+]RalfJung commentedon Dec 1, 2019
One of the two errors is coming from evaluating the
AssertMIR terminator that does the bounds-check and panics if it fails. The other message comes from actually evaluating theplace[4]even though the index is out-of-bounds.What is surprising here is that the evaluation of the place expression even happens even though that should be dead code. @oli-obk @wesleywiser any idea?
(This is not fixed by #66927.)
RalfJung commentedon Dec 1, 2019
The reason is that the MIR of the promoted actually looks like this:
Notice the unchecked array access. That's how this is not dead code.
I assume the other error comes from ConstProp (likely of the
Assertterminator), but I am not sure.RalfJung commentedon Dec 1, 2019
Cc @ecstatic-morse @eddyb because promotion is involved
eddyb commentedon Dec 2, 2019
I don't think
const_errshould include promoteds, since any checks for anything they might do are still in the runtime code (doing anything else would require something much more complex than what we have today).But also, if we constant-fold the assert, we can know that using the promoted is dead code (I guess this doesn't apply to codegen generating those warnings instead of constant folding).
RalfJung commentedon Feb 10, 2020
@eddyb so basically,
const_propshould cease to emit warnings when it is working inside a promoted? Or maybe it shouldn't run there at all?Cc @wesleywiser
eddyb commentedon Feb 10, 2020
You could still have warnings inside a promoted that are not from things covered by
Asserts, but other odd conditionsconst_propchecks.Basically "this expression will panic at runtime" type warnings shouldn't be emitted from a promoted, others might be fine.
RalfJung commentedon Feb 11, 2020
Actually, I think the first ("error: index out of bounds") is not a warning in any meaningful way... trying to evaluate that promoted fails with a hard const-error ("UB: OOB array access") and produces no result. I does not seem wise to me to not report such hard errors.
I am pretty sure the "reaching this expression at runtime will panic or abort" part is not from inside the promoted. That one gets raised by const-prop when partially evaluating the
Assertterminator that is left in thefn.So @eddyb I don't think any of your proposals so far actually solves this problem. Or do you suggest we ignore UB in promoteds?
Promote `Ref`s to constants instead of static
RalfJung commentedon Feb 11, 2020
Also, in latest master a third warning crept in again:
The third error was added by #67000
RalfJung commentedon Feb 11, 2020
FWIW, this affects not just array indexing but also div-by-zero. At least the two are consistent. ;)
[-]Duplicated errors on index out of bound const evaluation[/-][+]Duplicated errors on UB inside a promoted[/+]eddyb commentedon Feb 11, 2020
If we want to be clever we could only check promoteds that are still reachable after constant folding +
simplify_cfg.Because the
Assertguarding the use of the erroring promoted would have become unconditional.Actually, do we have an unconditional
Panicterminator?Rollup merge of rust-lang#69185 - RalfJung:const-prop-lints, r=oli-obk
steveklabnik commentedon Jan 14, 2021
Triage: no change
RalfJung commentedon Jan 15, 2021
#80579 will solve this by not promoting division and array indexing any more when they could fail.