Skip to content

Commit e9ce6f9

Browse files
committed
Auto merge of #115762 - oli-obk:early_const_prop_lint2, r=<try>
Avoid revealing in layout_of r? `@compiler-errors` I feel like `layout_of` is doing too many things at once, and I don't really know why. It could allow us to if callers could decide whether to reveal opaque types.
2 parents 078eb11 + 28bfd7a commit e9ce6f9

File tree

12 files changed

+41
-29
lines changed

12 files changed

+41
-29
lines changed

compiler/rustc_middle/src/ty/layout.rs

+1
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ impl<'tcx> SizeSkeleton<'tcx> {
324324
param_env: ty::ParamEnv<'tcx>,
325325
) -> Result<SizeSkeleton<'tcx>, &'tcx LayoutError<'tcx>> {
326326
debug_assert!(!ty.has_non_region_infer());
327+
let param_env = param_env.with_reveal_all_normalized(tcx);
327328

328329
// First try computing a static layout.
329330
let err = match tcx.layout_of(param_env.and(ty)) {

compiler/rustc_mir_transform/src/const_prop_lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
176176
) -> ConstPropagator<'mir, 'tcx> {
177177
let def_id = body.source.def_id();
178178
let args = &GenericArgs::identity_for_item(tcx, def_id);
179-
let param_env = tcx.param_env_reveal_all_normalized(def_id);
179+
let param_env = tcx.param_env(def_id);
180180

181181
let can_const_prop = CanConstProp::check(tcx, param_env, body);
182182
let mut ecx = InterpCx::new(

compiler/rustc_passes/src/layout_test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub fn ensure_wf<'tcx>(
6666
}
6767

6868
fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
69-
let param_env = tcx.param_env(item_def_id);
69+
let param_env = tcx.param_env_reveal_all_normalized(item_def_id);
7070
let ty = tcx.type_of(item_def_id).instantiate_identity();
7171
let span = tcx.def_span(item_def_id.to_def_id());
7272
if !ensure_wf(tcx, param_env, ty, item_def_id, span) {

compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,10 @@ pub fn typeid_for_instance<'tcx>(
11061106
options: TypeIdOptions,
11071107
) -> String {
11081108
let fn_abi = tcx
1109-
.fn_abi_of_instance(tcx.param_env(instance.def_id()).and((*instance, ty::List::empty())))
1109+
.fn_abi_of_instance(
1110+
tcx.param_env_reveal_all_normalized(instance.def_id())
1111+
.and((*instance, ty::List::empty())),
1112+
)
11101113
.unwrap_or_else(|instance| {
11111114
bug!("typeid_for_instance: couldn't get fn_abi of instance {:?}", instance)
11121115
});

compiler/rustc_ty_utils/src/layout.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_index::bit_set::BitSet;
44
use rustc_index::{IndexSlice, IndexVec};
55
use rustc_middle::mir::{GeneratorLayout, GeneratorSavedLocal};
66
use rustc_middle::query::Providers;
7+
use rustc_middle::traits::Reveal;
78
use rustc_middle::ty::layout::{
89
IntegerExt, LayoutCx, LayoutError, LayoutOf, TyAndLayout, MAX_SIMD_LANES,
910
};
@@ -36,7 +37,6 @@ fn layout_of<'tcx>(
3637
let (param_env, ty) = query.into_parts();
3738
debug!(?ty);
3839

39-
let param_env = param_env.with_reveal_all_normalized(tcx);
4040
let unnormalized_ty = ty;
4141

4242
// FIXME: We might want to have two different versions of `layout_of`:
@@ -52,7 +52,19 @@ fn layout_of<'tcx>(
5252
}
5353
};
5454

55-
if ty != unnormalized_ty {
55+
if ty == unnormalized_ty {
56+
// see comment in eval_to_allocation_raw_provider for what we're doing here
57+
if param_env.reveal() == Reveal::All {
58+
let mut query = query;
59+
query.param_env = param_env.with_user_facing();
60+
match tcx.layout_of(query) {
61+
// try again with reveal all as requested
62+
Err(LayoutError::Unknown(_) | LayoutError::NormalizationFailure(_, _)) => {}
63+
// deduplicate calls
64+
other => return other,
65+
}
66+
}
67+
} else {
5668
// Ensure this layout is also cached for the normalized type.
5769
return tcx.layout_of(param_env.and(ty));
5870
}

src/tools/clippy/clippy_lints/src/large_futures.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeFuture {
6969
&& let ty = cx.typeck_results().expr_ty(expr)
7070
&& let Some(future_trait_def_id) = cx.tcx.lang_items().future_trait()
7171
&& implements_trait(cx, ty, future_trait_def_id, &[])
72-
&& let Ok(layout) = cx.tcx.layout_of(cx.param_env.and(ty))
72+
&& let Ok(layout) = cx.tcx.layout_of(cx.param_env.with_reveal_all_normalized(cx.tcx).and(ty))
7373
&& let size = layout.layout.size()
7474
&& size >= Size::from_bytes(self.future_size_threshold)
7575
{

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

-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ error[E0391]: cycle detected when elaborating drops for `<impl at $DIR/issue-249
44
LL | const BAR: u32 = IMPL_REF_BAR;
55
| ^^^^^^^^^^^^
66
|
7-
note: ...which requires const-evaluating + checking `IMPL_REF_BAR`...
8-
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
9-
|
10-
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
11-
| ^^^^^^^^^^^^^^^^^^^^^^^
127
note: ...which requires const-evaluating + checking `IMPL_REF_BAR`...
138
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:27
149
|

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

-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ error[E0391]: cycle detected when elaborating drops for `FooDefault::BAR`
44
LL | const BAR: u32 = DEFAULT_REF_BAR;
55
| ^^^^^^^^^^^^^^^
66
|
7-
note: ...which requires const-evaluating + checking `DEFAULT_REF_BAR`...
8-
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:1
9-
|
10-
LL | const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR;
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
127
note: ...which requires const-evaluating + checking `DEFAULT_REF_BAR`...
138
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:30
149
|

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

-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ error[E0391]: cycle detected when elaborating drops for `<impl at $DIR/issue-249
44
LL | const BAR: u32 = TRAIT_REF_BAR;
55
| ^^^^^^^^^^^^^
66
|
7-
note: ...which requires const-evaluating + checking `TRAIT_REF_BAR`...
8-
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:1
9-
|
10-
LL | const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^
127
note: ...which requires const-evaluating + checking `TRAIT_REF_BAR`...
138
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:28
149
|

tests/ui/layout/layout-cycle.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ error[E0391]: cycle detected when computing layout of `S<S<()>>`
22
|
33
= note: ...which requires computing layout of `<S<()> as Tr>::I`...
44
= note: ...which again requires computing layout of `S<S<()>>`, completing the cycle
5+
= note: cycle used when computing layout of `S<S<()>>`
56
= 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
67

78
error: failed to get layout for S<S<()>>: a cycle occurred during layout computation

tests/ui/recursion/issue-26548-recursion-via-normalize.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
//~ ERROR cycle detected when computing layout of `core::option::Option<S>`
1+
//~ ERROR cycle detected when computing layout of `core::option::Option<<S as Mirror>::It>`
22
//~| 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
33
//~| NOTE ...which requires computing layout of `S`...
4-
//~| NOTE ...which requires computing layout of `core::option::Option<<S as Mirror>::It>`...
5-
//~| NOTE ...which again requires computing layout of `core::option::Option<S>`, completing the cycle
6-
//~| NOTE cycle used when computing layout of `core::option::Option<<S as Mirror>::It>`
4+
//~| NOTE ...which again requires computing layout of `core::option::Option<<S as Mirror>::It>`, completing the cycle
5+
//~| NOTE ...which requires computing layout of `core::option::Option<S>`...
76

87
trait Mirror {
8+
//~^ NOTE: cycle used when checking deathness of variables in top-level module
99
type It: ?Sized;
1010
}
1111
impl<T: ?Sized> Mirror for T {

tests/ui/recursion/issue-26548-recursion-via-normalize.stderr

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
error[E0391]: cycle detected when computing layout of `core::option::Option<S>`
1+
error[E0391]: cycle detected when computing layout of `core::option::Option<<S as Mirror>::It>`
22
|
3+
= note: ...which requires computing layout of `core::option::Option<S>`...
34
= note: ...which requires computing layout of `S`...
4-
= note: ...which requires computing layout of `core::option::Option<<S as Mirror>::It>`...
5-
= note: ...which again requires computing layout of `core::option::Option<S>`, completing the cycle
6-
= note: cycle used when computing layout of `core::option::Option<<S as Mirror>::It>`
5+
= note: ...which again requires computing layout of `core::option::Option<<S as Mirror>::It>`, completing the cycle
6+
note: cycle used when checking deathness of variables in top-level module
7+
--> $DIR/issue-26548-recursion-via-normalize.rs:7:1
8+
|
9+
LL | / trait Mirror {
10+
LL | |
11+
LL | | type It: ?Sized;
12+
LL | | }
13+
... |
14+
LL | | let _s = S(None);
15+
LL | | }
16+
| |_^
717
= 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
818

919
error: aborting due to previous error

0 commit comments

Comments
 (0)