Skip to content

Commit 63a199c

Browse files
committed
Auto merge of rust-lang#120687 - oli-obk:early_const_prop, r=<try>
Run const_prop_lint in check builds, too implements rust-lang#108730 (comment) Turns the const_prop_lint pass into a query that is run alongside borrowck.
2 parents ea37e80 + 74cde5f commit 63a199c

File tree

127 files changed

+1293
-531
lines changed

Some content is hidden

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

127 files changed

+1293
-531
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
@@ -1013,6 +1013,12 @@ rustc_queries! {
10131013
cache_on_disk_if(tcx) { tcx.is_typeck_child(key.to_def_id()) }
10141014
}
10151015

1016+
/// Run the const prop lints on the `mir_promoted` of an item.
1017+
query const_prop_lint(key: LocalDefId) {
1018+
desc { |tcx| "checking const prop lints for `{}`", tcx.def_path_str(key) }
1019+
cache_on_disk_if { true }
1020+
}
1021+
10161022
/// Gets a complete map from all types to their inherent impls.
10171023
/// Not meant to be used directly outside of coherence.
10181024
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
@@ -394,6 +395,27 @@ fn inner_mir_for_ctfe(tcx: TyCtxt<'_>, def: LocalDefId) -> Body<'_> {
394395
body
395396
}
396397

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

406429
let is_fn_like = tcx.def_kind(def).is_fn_like();
407430
if is_fn_like {
@@ -534,7 +557,6 @@ fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
534557
&elaborate_box_derefs::ElaborateBoxDerefs,
535558
&coroutine::StateTransform,
536559
&add_retag::AddRetag,
537-
&Lint(const_prop_lint::ConstPropLint),
538560
];
539561
pm::run_passes_no_validate(tcx, body, passes, Some(MirPhase::Runtime(RuntimePhase::Initial)));
540562
}

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

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
error[E0080]: evaluation of `<() as Tr>::B` failed
2-
--> $DIR/defaults-not-assumed-fail.rs:8:19
2+
--> $DIR/defaults-not-assumed-fail.rs:6:19
33
|
44
LL | const B: u8 = Self::A + 1;
55
| ^^^^^^^^^^^ attempt to compute `u8::MAX + 1_u8`, which would overflow
66

77
note: erroneous constant encountered
8-
--> $DIR/defaults-not-assumed-fail.rs:33:16
8+
--> $DIR/defaults-not-assumed-fail.rs:31:16
99
|
1010
LL | assert_eq!(<() as Tr>::B, 0); // causes the error above
1111
| ^^^^^^^^^^^^^
1212

1313
note: erroneous constant encountered
14-
--> $DIR/defaults-not-assumed-fail.rs:33:5
14+
--> $DIR/defaults-not-assumed-fail.rs:31:5
1515
|
1616
LL | assert_eq!(<() as Tr>::B, 0); // causes the error above
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818
|
1919
= note: this note originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
2020

2121
note: erroneous constant encountered
22-
--> $DIR/defaults-not-assumed-fail.rs:33:5
22+
--> $DIR/defaults-not-assumed-fail.rs:31:5
2323
|
2424
LL | assert_eq!(<() as Tr>::B, 0); // causes the error above
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

tests/ui/associated-consts/issue-24949-assoc-const-static-recursion-impl.stderr

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0391]: cycle detected when elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 11:19>::BAR`
1+
error[E0391]: cycle detected when checking const prop lints for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 11:19>::BAR`
22
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:12:22
33
|
44
LL | const BAR: u32 = IMPL_REF_BAR;
@@ -29,7 +29,12 @@ note: ...which requires caching mir of `<impl at $DIR/issue-24949-assoc-const-st
2929
|
3030
LL | const BAR: u32 = IMPL_REF_BAR;
3131
| ^^^^^^^^^^^^^^
32-
= note: ...which again requires elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 11:19>::BAR`, completing the cycle
32+
note: ...which requires elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 11:19>::BAR`...
33+
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:12:5
34+
|
35+
LL | const BAR: u32 = IMPL_REF_BAR;
36+
| ^^^^^^^^^^^^^^
37+
= note: ...which again requires checking const prop lints for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 11:19>::BAR`, completing the cycle
3338
= note: cycle used when running analysis passes on this crate
3439
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
3540

tests/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait-default.stderr

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0391]: cycle detected when elaborating drops for `FooDefault::BAR`
1+
error[E0391]: cycle detected when checking const prop lints for `FooDefault::BAR`
22
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:22
33
|
44
LL | const BAR: u32 = DEFAULT_REF_BAR;
@@ -29,7 +29,12 @@ note: ...which requires caching mir of `FooDefault::BAR` for CTFE...
2929
|
3030
LL | const BAR: u32 = DEFAULT_REF_BAR;
3131
| ^^^^^^^^^^^^^^
32-
= note: ...which again requires elaborating drops for `FooDefault::BAR`, completing the cycle
32+
note: ...which requires elaborating drops for `FooDefault::BAR`...
33+
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:5
34+
|
35+
LL | const BAR: u32 = DEFAULT_REF_BAR;
36+
| ^^^^^^^^^^^^^^
37+
= note: ...which again requires checking const prop lints for `FooDefault::BAR`, completing the cycle
3338
= note: cycle used when running analysis passes on this crate
3439
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
3540

tests/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait.stderr

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0391]: cycle detected when elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-trait.rs:11:1: 11:28>::BAR`
1+
error[E0391]: cycle detected when checking const prop lints for `<impl at $DIR/issue-24949-assoc-const-static-recursion-trait.rs:11:1: 11:28>::BAR`
22
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:12:22
33
|
44
LL | const BAR: u32 = TRAIT_REF_BAR;
@@ -29,7 +29,12 @@ note: ...which requires caching mir of `<impl at $DIR/issue-24949-assoc-const-st
2929
|
3030
LL | const BAR: u32 = TRAIT_REF_BAR;
3131
| ^^^^^^^^^^^^^^
32-
= note: ...which again requires elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-trait.rs:11:1: 11:28>::BAR`, completing the cycle
32+
note: ...which requires elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-trait.rs:11:1: 11:28>::BAR`...
33+
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:12:5
34+
|
35+
LL | const BAR: u32 = TRAIT_REF_BAR;
36+
| ^^^^^^^^^^^^^^
37+
= note: ...which again requires checking const prop lints for `<impl at $DIR/issue-24949-assoc-const-static-recursion-trait.rs:11:1: 11:28>::BAR`, completing the cycle
3338
= note: cycle used when running analysis passes on this crate
3439
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
3540

tests/ui/async-await/in-trait/indirect-recursion-issue-112047.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
// edition: 2021
22

3-
// Test doesn't fail until monomorphization time, unfortunately.
4-
// build-fail
5-
63
fn main() {
74
let _ = async {
85
A.first().await.second().await;

tests/ui/async-await/in-trait/indirect-recursion-issue-112047.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0733]: recursion in an async fn requires boxing
2-
--> $DIR/indirect-recursion-issue-112047.rs:34:5
2+
--> $DIR/indirect-recursion-issue-112047.rs:31:5
33
|
44
LL | async fn second(self) {
55
| ^^^^^^^^^^^^^^^^^^^^^

tests/ui/coherence/occurs-check/opaques.next.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0119]: conflicting implementations of trait `Trait<Alias<_>>` for type `Alias<_>`
2-
--> $DIR/opaques.rs:29:1
2+
--> $DIR/opaques.rs:33:1
33
|
44
LL | impl<T> Trait<T> for T {
55
| ---------------------- first implementation here
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: internal compiler error: unexpected ambiguity: Canonical { value: ParamEnvAnd { param_env: ParamEnv { caller_bounds: [Binder { value: TraitPredicate(<T as std::marker::Sized>, polarity:Positive), bound_vars: [] }], reveal: All }, value: AliasTy { args: [T/#0, T/#0], def_id: DefId(0:15 ~ opaques[e7ab]::Trait::Assoc) } }, max_universe: U0, variables: [] } Canonical { value: QueryResponse { var_values: CanonicalVarValues { var_values: [] }, region_constraints: QueryRegionConstraints { outlives: [], member_constraints: [] }, certainty: Ambiguous, opaque_types: [], value: NormalizationResult { normalized_ty: ^0 } }, max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }] }
2+
|
3+
= error: internal compiler error: unexpected ambiguity: Canonical { value: ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: All }, value: AliasTy { args: [(), ()], def_id: DefId(0:15 ~ opaques[e7ab]::Trait::Assoc) } }, max_universe: U0, variables: [] } Canonical { value: QueryResponse { var_values: CanonicalVarValues { var_values: [] }, region_constraints: QueryRegionConstraints { outlives: [], member_constraints: [] }, certainty: Ambiguous, opaque_types: [], value: NormalizationResult { normalized_ty: ^0 } }, max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }] }
4+
|
5+
= query stack during panic:
6+
end of query stack
7+
error: aborting due to 2 previous errors
8+

tests/ui/coherence/occurs-check/opaques.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
// A regression test for #105787
55

66
//[old] known-bug: #105787
7-
//[old] check-pass
7+
//[old] failure-status: 101
8+
// normalize-stderr-test "note: .*\n\n" -> ""
9+
// normalize-stderr-test "thread 'rustc' panicked.*\n" -> ""
10+
// normalize-stderr-test "(error: internal compiler error: [^:]+):\d+:\d+: " -> "$1:LL:CC: "
11+
// rustc-env:RUST_BACKTRACE=0
812
#![feature(type_alias_impl_trait)]
913
mod defining_scope {
1014
use super::*;

tests/ui/const_prop/const-prop-ice.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// build-fail
2-
31
fn main() {
42
[0; 3][3u64 as usize]; //~ ERROR this operation will panic at runtime
53
}

tests/ui/const_prop/const-prop-ice.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this operation will panic at runtime
2-
--> $DIR/const-prop-ice.rs:4:5
2+
--> $DIR/const-prop-ice.rs:2:5
33
|
44
LL | [0; 3][3u64 as usize];
55
| ^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the length is 3 but the index is 3

tests/ui/const_prop/const-prop-ice2.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// build-fail
2-
31
fn main() {
42
enum Enum { One=1 }
53
let xs=[0;1 as usize];

tests/ui/const_prop/const-prop-ice2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this operation will panic at runtime
2-
--> $DIR/const-prop-ice2.rs:6:20
2+
--> $DIR/const-prop-ice2.rs:4:20
33
|
44
LL | println!("{}", xs[Enum::One as usize]);
55
| ^^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the length is 1 but the index is 1

tests/ui/consts/const-err-early.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ fn main() {
1010
let _c = C;
1111
let _d = D;
1212
let _e = E;
13-
let _e = [6u8][1];
13+
let _e = [6u8][1]; //~ ERROR: will panic at runtime
1414
}

0 commit comments

Comments
 (0)