Skip to content

Commit

Permalink
Auto merge of rust-lang#120687 - oli-obk:early_const_prop, r=<try>
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Feb 5, 2024
2 parents ea37e80 + 74cde5f commit 63a199c
Show file tree
Hide file tree
Showing 127 changed files with 1,293 additions and 531 deletions.
9 changes: 5 additions & 4 deletions compiler/rustc_abi/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,11 @@ where
}
}
}
_ => assert!(
start == Bound::Unbounded && end == Bound::Unbounded,
"nonscalar layout for layout_scalar_valid_range type: {st:#?}",
),
_ => {
if start != Bound::Unbounded || end != Bound::Unbounded {
return None;
}
}
}

Some(st)
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
// Run unsafety check because it's responsible for stealing and
// deallocating THIR.
tcx.ensure().check_unsafety(def_id);
tcx.ensure().const_prop_lint(def_id);
tcx.ensure().mir_borrowck(def_id)
});
});
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,12 @@ rustc_queries! {
cache_on_disk_if(tcx) { tcx.is_typeck_child(key.to_def_id()) }
}

/// Run the const prop lints on the `mir_promoted` of an item.
query const_prop_lint(key: LocalDefId) {
desc { |tcx| "checking const prop lints for `{}`", tcx.def_path_str(key) }
cache_on_disk_if { true }
}

/// Gets a complete map from all types to their inherent impls.
/// Not meant to be used directly outside of coherence.
query crate_inherent_impls(k: ()) -> Result<&'tcx CrateInherentImpls, ErrorGuaranteed> {
Expand Down
24 changes: 23 additions & 1 deletion compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ pub fn provide(providers: &mut Providers) {
is_ctfe_mir_available: |tcx, did| is_mir_available(tcx, did),
mir_callgraph_reachable: inline::cycle::mir_callgraph_reachable,
mir_inliner_callees: inline::cycle::mir_inliner_callees,
const_prop_lint,
promoted_mir,
deduced_param_attrs: deduce_param_attrs::deduced_param_attrs,
..*providers
Expand Down Expand Up @@ -394,6 +395,27 @@ fn inner_mir_for_ctfe(tcx: TyCtxt<'_>, def: LocalDefId) -> Body<'_> {
body
}

fn const_prop_lint(tcx: TyCtxt<'_>, def: LocalDefId) {
let (body, _) = tcx.mir_promoted(def);
let body = body.borrow();

let mir_borrowck = tcx.mir_borrowck(def);

// If there are impossible bounds on the body being const prop linted,
// the const eval logic used in const prop may ICE unexpectedly.
let predicates = tcx
.predicates_of(body.source.def_id())
.predicates
.iter()
.filter_map(|(p, _)| if p.is_global() { Some(*p) } else { None });
if !traits::impossible_predicates(tcx, traits::elaborate(tcx, predicates).collect())
&& mir_borrowck.tainted_by_errors.is_none()
&& body.tainted_by_errors.is_none()
{
const_prop_lint::ConstPropLint.run_lint(tcx, &body);
}
}

/// Obtain just the main MIR (no promoteds) and run some cleanups on it. This also runs
/// mir borrowck *before* doing so in order to ensure that borrowck can be run and doesn't
/// end up missing the source MIR due to stealing happening.
Expand All @@ -402,6 +424,7 @@ fn mir_drops_elaborated_and_const_checked(tcx: TyCtxt<'_>, def: LocalDefId) -> &
tcx.ensure_with_value().mir_coroutine_witnesses(def);
}
let mir_borrowck = tcx.mir_borrowck(def);
tcx.ensure().const_prop_lint(def);

let is_fn_like = tcx.def_kind(def).is_fn_like();
if is_fn_like {
Expand Down Expand Up @@ -534,7 +557,6 @@ fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&elaborate_box_derefs::ElaborateBoxDerefs,
&coroutine::StateTransform,
&add_retag::AddRetag,
&Lint(const_prop_lint::ConstPropLint),
];
pm::run_passes_no_validate(tcx, body, passes, Some(MirPhase::Runtime(RuntimePhase::Initial)));
}
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/array-slice-vec/array_const_index-0.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ error[E0080]: evaluation of constant value failed
LL | const B: i32 = (&A)[1];
| ^^^^^^^ index out of bounds: the length is 0 but the index is 1

note: erroneous constant encountered
--> $DIR/array_const_index-0.rs:7:13
|
LL | let _ = B;
| ^

note: erroneous constant encountered
--> $DIR/array_const_index-0.rs:7:13
|
LL | let _ = B;
| ^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0080`.
14 changes: 14 additions & 0 deletions tests/ui/array-slice-vec/array_const_index-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ error[E0080]: evaluation of constant value failed
LL | const B: i32 = A[1];
| ^^^^ index out of bounds: the length is 0 but the index is 1

note: erroneous constant encountered
--> $DIR/array_const_index-1.rs:7:13
|
LL | let _ = B;
| ^

note: erroneous constant encountered
--> $DIR/array_const_index-1.rs:7:13
|
LL | let _ = B;
| ^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0080`.
2 changes: 0 additions & 2 deletions tests/ui/associated-consts/defaults-cyclic-fail.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// build-fail

// Cyclic assoc. const defaults don't error unless *used*
trait Tr {
const A: u8 = Self::B;
Expand Down
10 changes: 5 additions & 5 deletions tests/ui/associated-consts/defaults-cyclic-fail.stderr
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
error[E0391]: cycle detected when simplifying constant for the type system `Tr::A`
--> $DIR/defaults-cyclic-fail.rs:5:5
--> $DIR/defaults-cyclic-fail.rs:3:5
|
LL | const A: u8 = Self::B;
| ^^^^^^^^^^^
|
note: ...which requires const-evaluating + checking `Tr::A`...
--> $DIR/defaults-cyclic-fail.rs:5:19
--> $DIR/defaults-cyclic-fail.rs:3:19
|
LL | const A: u8 = Self::B;
| ^^^^^^^
note: ...which requires simplifying constant for the type system `Tr::B`...
--> $DIR/defaults-cyclic-fail.rs:8:5
--> $DIR/defaults-cyclic-fail.rs:6:5
|
LL | const B: u8 = Self::A;
| ^^^^^^^^^^^
note: ...which requires const-evaluating + checking `Tr::B`...
--> $DIR/defaults-cyclic-fail.rs:8:19
--> $DIR/defaults-cyclic-fail.rs:6:19
|
LL | const B: u8 = Self::A;
| ^^^^^^^
= note: ...which again requires simplifying constant for the type system `Tr::A`, completing the cycle
note: cycle used when const-evaluating + checking `main::promoted[1]`
--> $DIR/defaults-cyclic-fail.rs:16:16
--> $DIR/defaults-cyclic-fail.rs:14:16
|
LL | assert_eq!(<() as Tr>::A, 0);
| ^^^^^^^^^^^^^
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/associated-consts/defaults-not-assumed-fail.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// build-fail

trait Tr {
const A: u8 = 255;

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/associated-consts/defaults-not-assumed-fail.stderr
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
error[E0080]: evaluation of `<() as Tr>::B` failed
--> $DIR/defaults-not-assumed-fail.rs:8:19
--> $DIR/defaults-not-assumed-fail.rs:6:19
|
LL | const B: u8 = Self::A + 1;
| ^^^^^^^^^^^ attempt to compute `u8::MAX + 1_u8`, which would overflow

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

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

note: erroneous constant encountered
--> $DIR/defaults-not-assumed-fail.rs:33:5
--> $DIR/defaults-not-assumed-fail.rs:31:5
|
LL | assert_eq!(<() as Tr>::B, 0); // causes the error above
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0391]: cycle detected when elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 11:19>::BAR`
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`
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:12:22
|
LL | const BAR: u32 = IMPL_REF_BAR;
Expand Down Expand Up @@ -29,7 +29,12 @@ note: ...which requires caching mir of `<impl at $DIR/issue-24949-assoc-const-st
|
LL | const BAR: u32 = IMPL_REF_BAR;
| ^^^^^^^^^^^^^^
= 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
note: ...which requires elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 11:19>::BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:12:5
|
LL | const BAR: u32 = IMPL_REF_BAR;
| ^^^^^^^^^^^^^^
= 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
= note: cycle used when running analysis passes on this crate
= 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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0391]: cycle detected when elaborating drops for `FooDefault::BAR`
error[E0391]: cycle detected when checking const prop lints for `FooDefault::BAR`
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:22
|
LL | const BAR: u32 = DEFAULT_REF_BAR;
Expand Down Expand Up @@ -29,7 +29,12 @@ note: ...which requires caching mir of `FooDefault::BAR` for CTFE...
|
LL | const BAR: u32 = DEFAULT_REF_BAR;
| ^^^^^^^^^^^^^^
= note: ...which again requires elaborating drops for `FooDefault::BAR`, completing the cycle
note: ...which requires elaborating drops for `FooDefault::BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:5
|
LL | const BAR: u32 = DEFAULT_REF_BAR;
| ^^^^^^^^^^^^^^
= note: ...which again requires checking const prop lints for `FooDefault::BAR`, completing the cycle
= note: cycle used when running analysis passes on this crate
= 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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0391]: cycle detected when elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-trait.rs:11:1: 11:28>::BAR`
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`
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:12:22
|
LL | const BAR: u32 = TRAIT_REF_BAR;
Expand Down Expand Up @@ -29,7 +29,12 @@ note: ...which requires caching mir of `<impl at $DIR/issue-24949-assoc-const-st
|
LL | const BAR: u32 = TRAIT_REF_BAR;
| ^^^^^^^^^^^^^^
= 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
note: ...which requires elaborating drops for `<impl at $DIR/issue-24949-assoc-const-static-recursion-trait.rs:11:1: 11:28>::BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:12:5
|
LL | const BAR: u32 = TRAIT_REF_BAR;
| ^^^^^^^^^^^^^^
= 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
= note: cycle used when running analysis passes on this crate
= 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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// edition: 2021

// Test doesn't fail until monomorphization time, unfortunately.
// build-fail

fn main() {
let _ = async {
A.first().await.second().await;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0733]: recursion in an async fn requires boxing
--> $DIR/indirect-recursion-issue-112047.rs:34:5
--> $DIR/indirect-recursion-issue-112047.rs:31:5
|
LL | async fn second(self) {
| ^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/coherence/occurs-check/opaques.next.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0119]: conflicting implementations of trait `Trait<Alias<_>>` for type `Alias<_>`
--> $DIR/opaques.rs:29:1
--> $DIR/opaques.rs:33:1
|
LL | impl<T> Trait<T> for T {
| ---------------------- first implementation here
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/coherence/occurs-check/opaques.old.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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)) }] }
|
= 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)) }] }
|
= query stack during panic:
end of query stack
error: aborting due to 2 previous errors

6 changes: 5 additions & 1 deletion tests/ui/coherence/occurs-check/opaques.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
// A regression test for #105787

//[old] known-bug: #105787
//[old] check-pass
//[old] failure-status: 101
// normalize-stderr-test "note: .*\n\n" -> ""
// normalize-stderr-test "thread 'rustc' panicked.*\n" -> ""
// normalize-stderr-test "(error: internal compiler error: [^:]+):\d+:\d+: " -> "$1:LL:CC: "
// rustc-env:RUST_BACKTRACE=0
#![feature(type_alias_impl_trait)]
mod defining_scope {
use super::*;
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/const_prop/const-prop-ice.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// build-fail

fn main() {
[0; 3][3u64 as usize]; //~ ERROR this operation will panic at runtime
}
2 changes: 1 addition & 1 deletion tests/ui/const_prop/const-prop-ice.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this operation will panic at runtime
--> $DIR/const-prop-ice.rs:4:5
--> $DIR/const-prop-ice.rs:2:5
|
LL | [0; 3][3u64 as usize];
| ^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the length is 3 but the index is 3
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/const_prop/const-prop-ice2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// build-fail

fn main() {
enum Enum { One=1 }
let xs=[0;1 as usize];
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const_prop/const-prop-ice2.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this operation will panic at runtime
--> $DIR/const-prop-ice2.rs:6:20
--> $DIR/const-prop-ice2.rs:4:20
|
LL | println!("{}", xs[Enum::One as usize]);
| ^^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the length is 1 but the index is 1
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/consts/const-err-early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ fn main() {
let _c = C;
let _d = D;
let _e = E;
let _e = [6u8][1];
let _e = [6u8][1]; //~ ERROR: will panic at runtime
}

0 comments on commit 63a199c

Please sign in to comment.