Skip to content

Commit 4a6ca9f

Browse files
committed
Run const_prop_lint in check builds, too
1 parent 870a01a commit 4a6ca9f

File tree

134 files changed

+1316
-593
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+1316
-593
lines changed

compiler/rustc_abi/src/layout.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,11 @@ where
426426
}
427427
}
428428
}
429-
_ => assert!(
430-
start == Bound::Unbounded && end == Bound::Unbounded,
431-
"nonscalar layout for layout_scalar_valid_range type: {st:#?}",
432-
),
429+
_ => {
430+
if start != Bound::Unbounded || end != Bound::Unbounded {
431+
return None;
432+
}
433+
}
433434
}
434435

435436
Some(st)

compiler/rustc_interface/src/passes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
738738
// Run unsafety check because it's responsible for stealing and
739739
// deallocating THIR.
740740
tcx.ensure().check_unsafety(def_id);
741+
tcx.ensure().const_prop_lint(def_id);
741742
tcx.ensure().mir_borrowck(def_id)
742743
});
743744
});

compiler/rustc_middle/src/query/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,12 @@ rustc_queries! {
10181018
cache_on_disk_if(tcx) { tcx.is_typeck_child(key.to_def_id()) }
10191019
}
10201020

1021+
/// Run the const prop lints on the `mir_promoted` of an item.
1022+
query const_prop_lint(key: LocalDefId) {
1023+
desc { |tcx| "checking const prop lints for `{}`", tcx.def_path_str(key) }
1024+
cache_on_disk_if { true }
1025+
}
1026+
10211027
/// Gets a complete map from all types to their inherent impls.
10221028
/// Not meant to be used directly outside of coherence.
10231029
query crate_inherent_impls(k: ()) -> Result<&'tcx CrateInherentImpls, ErrorGuaranteed> {

compiler/rustc_mir_transform/src/lib.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ pub fn provide(providers: &mut Providers) {
142142
is_ctfe_mir_available: |tcx, did| is_mir_available(tcx, did),
143143
mir_callgraph_reachable: inline::cycle::mir_callgraph_reachable,
144144
mir_inliner_callees: inline::cycle::mir_inliner_callees,
145+
const_prop_lint,
145146
promoted_mir,
146147
deduced_param_attrs: deduce_param_attrs::deduced_param_attrs,
147148
..*providers
@@ -398,6 +399,27 @@ fn inner_mir_for_ctfe(tcx: TyCtxt<'_>, def: LocalDefId) -> Body<'_> {
398399
body
399400
}
400401

402+
fn const_prop_lint(tcx: TyCtxt<'_>, def: LocalDefId) {
403+
let (body, _) = tcx.mir_promoted(def);
404+
let body = body.borrow();
405+
406+
let mir_borrowck = tcx.mir_borrowck(def);
407+
408+
// If there are impossible bounds on the body being const prop linted,
409+
// the const eval logic used in const prop may ICE unexpectedly.
410+
let predicates = tcx
411+
.predicates_of(body.source.def_id())
412+
.predicates
413+
.iter()
414+
.filter_map(|(p, _)| if p.is_global() { Some(*p) } else { None });
415+
if !traits::impossible_predicates(tcx, traits::elaborate(tcx, predicates).collect())
416+
&& mir_borrowck.tainted_by_errors.is_none()
417+
&& body.tainted_by_errors.is_none()
418+
{
419+
const_prop_lint::ConstPropLint.run_lint(tcx, &body);
420+
}
421+
}
422+
401423
/// Obtain just the main MIR (no promoteds) and run some cleanups on it. This also runs
402424
/// mir borrowck *before* doing so in order to ensure that borrowck can be run and doesn't
403425
/// end up missing the source MIR due to stealing happening.
@@ -406,6 +428,7 @@ fn mir_drops_elaborated_and_const_checked(tcx: TyCtxt<'_>, def: LocalDefId) -> &
406428
tcx.ensure_with_value().mir_coroutine_witnesses(def);
407429
}
408430
let mir_borrowck = tcx.mir_borrowck(def);
431+
tcx.ensure().const_prop_lint(def);
409432

410433
let is_fn_like = tcx.def_kind(def).is_fn_like();
411434
if is_fn_like {
@@ -538,7 +561,6 @@ fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
538561
&elaborate_box_derefs::ElaborateBoxDerefs,
539562
&coroutine::StateTransform,
540563
&add_retag::AddRetag,
541-
&Lint(const_prop_lint::ConstPropLint),
542564
];
543565
pm::run_passes_no_validate(tcx, body, passes, Some(MirPhase::Runtime(RuntimePhase::Initial)));
544566
}

src/tools/clippy/tests/ui-toml/suppress_lint_in_const/test.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ fn main() {
3535
x[const { idx() }]; // Ok, should not produce stderr.
3636
x[const { idx4() }]; // Ok, let rustc's `unconditional_panic` lint handle `usize` indexing on arrays.
3737
const { &ARR[idx()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true.
38-
const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true.
39-
//
40-
//~^^ ERROR: failed
38+
// FIXME errors in rustc and subsequently blocks all lints in this file. Can reenable once solved on the rustc side
39+
//const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true.
4140

4241
let y = &x;
4342
y[0]; // Ok, referencing shouldn't affect this lint. See the issue 6021
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
error[E0080]: evaluation of `main::{constant#3}` failed
2-
--> $DIR/test.rs:38:14
3-
|
4-
LL | const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true.
5-
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
6-
7-
note: erroneous constant encountered
8-
--> $DIR/test.rs:38:5
9-
|
10-
LL | const { &ARR[idx4()] }; // Ok, should not produce stderr, since `suppress-restriction-lint-in-const` is set true.
11-
| ^^^^^^^^^^^^^^^^^^^^^^
12-
131
error: indexing may panic
142
--> $DIR/test.rs:29:5
153
|
@@ -21,39 +9,39 @@ LL | x[index];
219
= help: to override `-D warnings` add `#[allow(clippy::indexing_slicing)]`
2210

2311
error: indexing may panic
24-
--> $DIR/test.rs:47:5
12+
--> $DIR/test.rs:46:5
2513
|
2614
LL | v[0];
2715
| ^^^^
2816
|
2917
= help: consider using `.get(n)` or `.get_mut(n)` instead
3018

3119
error: indexing may panic
32-
--> $DIR/test.rs:48:5
20+
--> $DIR/test.rs:47:5
3321
|
3422
LL | v[10];
3523
| ^^^^^
3624
|
3725
= help: consider using `.get(n)` or `.get_mut(n)` instead
3826

3927
error: indexing may panic
40-
--> $DIR/test.rs:49:5
28+
--> $DIR/test.rs:48:5
4129
|
4230
LL | v[1 << 3];
4331
| ^^^^^^^^^
4432
|
4533
= help: consider using `.get(n)` or `.get_mut(n)` instead
4634

4735
error: indexing may panic
48-
--> $DIR/test.rs:55:5
36+
--> $DIR/test.rs:54:5
4937
|
5038
LL | v[N];
5139
| ^^^^
5240
|
5341
= help: consider using `.get(n)` or `.get_mut(n)` instead
5442

5543
error: indexing may panic
56-
--> $DIR/test.rs:56:5
44+
--> $DIR/test.rs:55:5
5745
|
5846
LL | v[M];
5947
| ^^^^
@@ -66,6 +54,6 @@ error[E0080]: evaluation of constant value failed
6654
LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
6755
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
6856

69-
error: aborting due to 8 previous errors
57+
error: aborting due to 7 previous errors
7058

7159
For more information about this error, try `rustc --explain E0080`.

src/tools/clippy/tests/ui/indexing_slicing_index.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ fn main() {
4545
const { &ARR[idx()] };
4646
//~^ ERROR: indexing may panic
4747
// This should be linted, since `suppress-restriction-lint-in-const` default is false.
48-
const { &ARR[idx4()] };
49-
//~^ ERROR: indexing may panic
48+
// FIXME can't include here for now, as the error on it causes all lints to get silenced
49+
//const { &ARR[idx4()] };
5050

5151
let y = &x;
5252
// Ok, referencing shouldn't affect this lint. See the issue 6021

src/tools/clippy/tests/ui/indexing_slicing_index.stderr

+1-22
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,6 @@ LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
1818
= help: consider using `.get(n)` or `.get_mut(n)` instead
1919
= note: the suggestion might not be applicable in constant blocks
2020

21-
error[E0080]: evaluation of `main::{constant#3}` failed
22-
--> $DIR/indexing_slicing_index.rs:48:14
23-
|
24-
LL | const { &ARR[idx4()] };
25-
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
26-
27-
note: erroneous constant encountered
28-
--> $DIR/indexing_slicing_index.rs:48:5
29-
|
30-
LL | const { &ARR[idx4()] };
31-
| ^^^^^^^^^^^^^^^^^^^^^^
32-
3321
error: indexing may panic
3422
--> $DIR/indexing_slicing_index.rs:29:5
3523
|
@@ -62,15 +50,6 @@ LL | const { &ARR[idx()] };
6250
= help: consider using `.get(n)` or `.get_mut(n)` instead
6351
= note: the suggestion might not be applicable in constant blocks
6452

65-
error: indexing may panic
66-
--> $DIR/indexing_slicing_index.rs:48:14
67-
|
68-
LL | const { &ARR[idx4()] };
69-
| ^^^^^^^^^^^
70-
|
71-
= help: consider using `.get(n)` or `.get_mut(n)` instead
72-
= note: the suggestion might not be applicable in constant blocks
73-
7453
error: index is out of bounds
7554
--> $DIR/indexing_slicing_index.rs:55:5
7655
|
@@ -135,6 +114,6 @@ error[E0080]: evaluation of constant value failed
135114
LL | const REF_ERR: &i32 = &ARR[idx4()]; // Ok, let rustc handle const contexts.
136115
| ^^^^^^^^^^^ index out of bounds: the length is 2 but the index is 4
137116

138-
error: aborting due to 17 previous errors
117+
error: aborting due to 15 previous errors
139118

140119
For more information about this error, try `rustc --explain E0080`.

tests/mir-opt/dataflow-const-prop/enum.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fn statics() {
6262

6363
static RC: &E = &E::V2(4);
6464

65-
// CHECK: [[t:_.*]] = const {alloc2: &&E};
65+
// CHECK: [[t:_.*]] = const {alloc4: &&E};
6666
// CHECK: [[e2]] = (*[[t]]);
6767
let e2 = RC;
6868

tests/mir-opt/instsimplify/combine_transmutes.integer_transmutes.InstSimplify.diff

+5-9
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,15 @@
55
let mut _0: ();
66
let mut _1: u32;
77
let mut _2: i64;
8-
let mut _3: i64;
9-
let mut _4: u32;
10-
let mut _5: usize;
8+
let mut _3: usize;
119

1210
bb0: {
1311
- _1 = const 1_i32 as u32 (Transmute);
12+
- _2 = const 1_u64 as i64 (Transmute);
13+
- _3 = const 1_isize as usize (Transmute);
1414
+ _1 = const 1_i32 as u32 (IntToInt);
15-
_2 = const 1_i32 as i64 (Transmute);
16-
- _3 = const 1_u64 as i64 (Transmute);
17-
+ _3 = const 1_u64 as i64 (IntToInt);
18-
_4 = const 1_u64 as u32 (Transmute);
19-
- _5 = const 1_isize as usize (Transmute);
20-
+ _5 = const 1_isize as usize (IntToInt);
15+
+ _2 = const 1_u64 as i64 (IntToInt);
16+
+ _3 = const 1_isize as usize (IntToInt);
2117
return;
2218
}
2319
}

tests/mir-opt/instsimplify/combine_transmutes.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#![feature(custom_mir)]
77

88
use std::intrinsics::mir::*;
9-
use std::mem::{MaybeUninit, ManuallyDrop, transmute};
9+
use std::mem::{transmute, ManuallyDrop, MaybeUninit};
1010

1111
// EMIT_MIR combine_transmutes.identity_transmutes.InstSimplify.diff
1212
pub unsafe fn identity_transmutes() {
@@ -25,19 +25,15 @@ pub unsafe fn integer_transmutes() {
2525
// CHECK-LABEL: fn integer_transmutes(
2626
// CHECK-NOT: _i32 as u32 (Transmute);
2727
// CHECK: _i32 as u32 (IntToInt);
28-
// CHECK: _i32 as i64 (Transmute);
2928
// CHECK-NOT: _u64 as i64 (Transmute);
3029
// CHECK: _u64 as i64 (IntToInt);
31-
// CHECK: _u64 as u32 (Transmute);
3230
// CHECK-NOT: _isize as usize (Transmute);
3331
// CHECK: _isize as usize (IntToInt);
3432

3533
mir! {
3634
{
3735
let A = CastTransmute::<i32, u32>(1); // Can be a cast
38-
let B = CastTransmute::<i32, i64>(1); // UB
3936
let C = CastTransmute::<u64, i64>(1); // Can be a cast
40-
let D = CastTransmute::<u64, u32>(1); // UB
4137
let E = CastTransmute::<isize, usize>(1); // Can be a cast
4238
Return()
4339
}
@@ -62,4 +58,7 @@ pub unsafe fn adt_transmutes() {
6258
let _a: ManuallyDrop<String> = transmute(MaybeUninit::<String>::uninit());
6359
}
6460

65-
pub union Union32 { u32: u32, i32: i32 }
61+
pub union Union32 {
62+
u32: u32,
63+
i32: i32,
64+
}

tests/ui/array-slice-vec/array_const_index-0.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ error[E0080]: evaluation of constant value failed
44
LL | const B: i32 = (&A)[1];
55
| ^^^^^^^ index out of bounds: the length is 0 but the index is 1
66

7+
note: erroneous constant encountered
8+
--> $DIR/array_const_index-0.rs:7:13
9+
|
10+
LL | let _ = B;
11+
| ^
12+
13+
note: erroneous constant encountered
14+
--> $DIR/array_const_index-0.rs:7:13
15+
|
16+
LL | let _ = B;
17+
| ^
18+
|
19+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
20+
721
error: aborting due to 1 previous error
822

923
For more information about this error, try `rustc --explain E0080`.

tests/ui/array-slice-vec/array_const_index-1.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ error[E0080]: evaluation of constant value failed
44
LL | const B: i32 = A[1];
55
| ^^^^ index out of bounds: the length is 0 but the index is 1
66

7+
note: erroneous constant encountered
8+
--> $DIR/array_const_index-1.rs:7:13
9+
|
10+
LL | let _ = B;
11+
| ^
12+
13+
note: erroneous constant encountered
14+
--> $DIR/array_const_index-1.rs:7:13
15+
|
16+
LL | let _ = B;
17+
| ^
18+
|
19+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
20+
721
error: aborting due to 1 previous error
822

923
For more information about this error, try `rustc --explain E0080`.

tests/ui/associated-consts/defaults-cyclic-fail.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// build-fail
2-
31
// Cyclic assoc. const defaults don't error unless *used*
42
trait Tr {
53
const A: u8 = Self::B;

tests/ui/associated-consts/defaults-cyclic-fail.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
error[E0391]: cycle detected when simplifying constant for the type system `Tr::A`
2-
--> $DIR/defaults-cyclic-fail.rs:5:5
2+
--> $DIR/defaults-cyclic-fail.rs:3:5
33
|
44
LL | const A: u8 = Self::B;
55
| ^^^^^^^^^^^
66
|
77
note: ...which requires const-evaluating + checking `Tr::A`...
8-
--> $DIR/defaults-cyclic-fail.rs:5:19
8+
--> $DIR/defaults-cyclic-fail.rs:3:19
99
|
1010
LL | const A: u8 = Self::B;
1111
| ^^^^^^^
1212
note: ...which requires simplifying constant for the type system `Tr::B`...
13-
--> $DIR/defaults-cyclic-fail.rs:8:5
13+
--> $DIR/defaults-cyclic-fail.rs:6:5
1414
|
1515
LL | const B: u8 = Self::A;
1616
| ^^^^^^^^^^^
1717
note: ...which requires const-evaluating + checking `Tr::B`...
18-
--> $DIR/defaults-cyclic-fail.rs:8:19
18+
--> $DIR/defaults-cyclic-fail.rs:6:19
1919
|
2020
LL | const B: u8 = Self::A;
2121
| ^^^^^^^
2222
= note: ...which again requires simplifying constant for the type system `Tr::A`, completing the cycle
2323
note: cycle used when const-evaluating + checking `main::promoted[1]`
24-
--> $DIR/defaults-cyclic-fail.rs:16:16
24+
--> $DIR/defaults-cyclic-fail.rs:14:16
2525
|
2626
LL | assert_eq!(<() as Tr>::A, 0);
2727
| ^^^^^^^^^^^^^

tests/ui/associated-consts/defaults-not-assumed-fail.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// build-fail
2-
31
trait Tr {
42
const A: u8 = 255;
53

0 commit comments

Comments
 (0)