Skip to content

Commit

Permalink
Auto merge of #115762 - oli-obk:early_const_prop_lint2, r=<try>
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Sep 18, 2023
2 parents 078eb11 + 28bfd7a commit e9ce6f9
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 29 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ impl<'tcx> SizeSkeleton<'tcx> {
param_env: ty::ParamEnv<'tcx>,
) -> Result<SizeSkeleton<'tcx>, &'tcx LayoutError<'tcx>> {
debug_assert!(!ty.has_non_region_infer());
let param_env = param_env.with_reveal_all_normalized(tcx);

// First try computing a static layout.
let err = match tcx.layout_of(param_env.and(ty)) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/const_prop_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
) -> ConstPropagator<'mir, 'tcx> {
let def_id = body.source.def_id();
let args = &GenericArgs::identity_for_item(tcx, def_id);
let param_env = tcx.param_env_reveal_all_normalized(def_id);
let param_env = tcx.param_env(def_id);

let can_const_prop = CanConstProp::check(tcx, param_env, body);
let mut ecx = InterpCx::new(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/layout_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn ensure_wf<'tcx>(
}

fn dump_layout_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
let param_env = tcx.param_env(item_def_id);
let param_env = tcx.param_env_reveal_all_normalized(item_def_id);
let ty = tcx.type_of(item_def_id).instantiate_identity();
let span = tcx.def_span(item_def_id.to_def_id());
if !ensure_wf(tcx, param_env, ty, item_def_id, span) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,10 @@ pub fn typeid_for_instance<'tcx>(
options: TypeIdOptions,
) -> String {
let fn_abi = tcx
.fn_abi_of_instance(tcx.param_env(instance.def_id()).and((*instance, ty::List::empty())))
.fn_abi_of_instance(
tcx.param_env_reveal_all_normalized(instance.def_id())
.and((*instance, ty::List::empty())),
)
.unwrap_or_else(|instance| {
bug!("typeid_for_instance: couldn't get fn_abi of instance {:?}", instance)
});
Expand Down
16 changes: 14 additions & 2 deletions compiler/rustc_ty_utils/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rustc_index::bit_set::BitSet;
use rustc_index::{IndexSlice, IndexVec};
use rustc_middle::mir::{GeneratorLayout, GeneratorSavedLocal};
use rustc_middle::query::Providers;
use rustc_middle::traits::Reveal;
use rustc_middle::ty::layout::{
IntegerExt, LayoutCx, LayoutError, LayoutOf, TyAndLayout, MAX_SIMD_LANES,
};
Expand Down Expand Up @@ -36,7 +37,6 @@ fn layout_of<'tcx>(
let (param_env, ty) = query.into_parts();
debug!(?ty);

let param_env = param_env.with_reveal_all_normalized(tcx);
let unnormalized_ty = ty;

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

if ty != unnormalized_ty {
if ty == unnormalized_ty {
// see comment in eval_to_allocation_raw_provider for what we're doing here
if param_env.reveal() == Reveal::All {
let mut query = query;
query.param_env = param_env.with_user_facing();
match tcx.layout_of(query) {
// try again with reveal all as requested
Err(LayoutError::Unknown(_) | LayoutError::NormalizationFailure(_, _)) => {}
// deduplicate calls
other => return other,
}
}
} else {
// Ensure this layout is also cached for the normalized type.
return tcx.layout_of(param_env.and(ty));
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/large_futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeFuture {
&& let ty = cx.typeck_results().expr_ty(expr)
&& let Some(future_trait_def_id) = cx.tcx.lang_items().future_trait()
&& implements_trait(cx, ty, future_trait_def_id, &[])
&& let Ok(layout) = cx.tcx.layout_of(cx.param_env.and(ty))
&& let Ok(layout) = cx.tcx.layout_of(cx.param_env.with_reveal_all_normalized(cx.tcx).and(ty))
&& let size = layout.layout.size()
&& size >= Size::from_bytes(self.future_size_threshold)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ error[E0391]: cycle detected when elaborating drops for `<impl at $DIR/issue-249
LL | const BAR: u32 = IMPL_REF_BAR;
| ^^^^^^^^^^^^
|
note: ...which requires const-evaluating + checking `IMPL_REF_BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:1
|
LL | const IMPL_REF_BAR: u32 = GlobalImplRef::BAR;
| ^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires const-evaluating + checking `IMPL_REF_BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:7:27
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ error[E0391]: cycle detected when elaborating drops for `FooDefault::BAR`
LL | const BAR: u32 = DEFAULT_REF_BAR;
| ^^^^^^^^^^^^^^^
|
note: ...which requires const-evaluating + checking `DEFAULT_REF_BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:1
|
LL | const DEFAULT_REF_BAR: u32 = <GlobalDefaultRef>::BAR;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires const-evaluating + checking `DEFAULT_REF_BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:11:30
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ error[E0391]: cycle detected when elaborating drops for `<impl at $DIR/issue-249
LL | const BAR: u32 = TRAIT_REF_BAR;
| ^^^^^^^^^^^^^
|
note: ...which requires const-evaluating + checking `TRAIT_REF_BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:1
|
LL | const TRAIT_REF_BAR: u32 = <GlobalTraitRef>::BAR;
| ^^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires const-evaluating + checking `TRAIT_REF_BAR`...
--> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:7:28
|
Expand Down
1 change: 1 addition & 0 deletions tests/ui/layout/layout-cycle.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ error[E0391]: cycle detected when computing layout of `S<S<()>>`
|
= note: ...which requires computing layout of `<S<()> as Tr>::I`...
= note: ...which again requires computing layout of `S<S<()>>`, completing the cycle
= note: cycle used when computing layout of `S<S<()>>`
= 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

error: failed to get layout for S<S<()>>: a cycle occurred during layout computation
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/recursion/issue-26548-recursion-via-normalize.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//~ ERROR cycle detected when computing layout of `core::option::Option<S>`
//~ ERROR cycle detected when computing layout of `core::option::Option<<S as Mirror>::It>`
//~| 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
//~| NOTE ...which requires computing layout of `S`...
//~| NOTE ...which requires computing layout of `core::option::Option<<S as Mirror>::It>`...
//~| NOTE ...which again requires computing layout of `core::option::Option<S>`, completing the cycle
//~| NOTE cycle used when computing layout of `core::option::Option<<S as Mirror>::It>`
//~| NOTE ...which again requires computing layout of `core::option::Option<<S as Mirror>::It>`, completing the cycle
//~| NOTE ...which requires computing layout of `core::option::Option<S>`...

trait Mirror {
//~^ NOTE: cycle used when checking deathness of variables in top-level module
type It: ?Sized;
}
impl<T: ?Sized> Mirror for T {
Expand Down
18 changes: 14 additions & 4 deletions tests/ui/recursion/issue-26548-recursion-via-normalize.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
error[E0391]: cycle detected when computing layout of `core::option::Option<S>`
error[E0391]: cycle detected when computing layout of `core::option::Option<<S as Mirror>::It>`
|
= note: ...which requires computing layout of `core::option::Option<S>`...
= note: ...which requires computing layout of `S`...
= note: ...which requires computing layout of `core::option::Option<<S as Mirror>::It>`...
= note: ...which again requires computing layout of `core::option::Option<S>`, completing the cycle
= note: cycle used when computing layout of `core::option::Option<<S as Mirror>::It>`
= note: ...which again requires computing layout of `core::option::Option<<S as Mirror>::It>`, completing the cycle
note: cycle used when checking deathness of variables in top-level module
--> $DIR/issue-26548-recursion-via-normalize.rs:7:1
|
LL | / trait Mirror {
LL | |
LL | | type It: ?Sized;
LL | | }
... |
LL | | let _s = S(None);
LL | | }
| |_^
= 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

error: aborting due to previous error
Expand Down

0 comments on commit e9ce6f9

Please sign in to comment.