From 28bfd7ab96ff1c8d57384c7e264c6d891c4572b2 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 18 Sep 2023 10:38:25 +0000 Subject: [PATCH] Try to deduplicate layout_of query work --- compiler/rustc_ty_utils/src/layout.rs | 15 ++++++++++++++- tests/ui/layout/layout-cycle.stderr | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_ty_utils/src/layout.rs b/compiler/rustc_ty_utils/src/layout.rs index 54c883788a5ca..aa4fbb4a4b3f1 100644 --- a/compiler/rustc_ty_utils/src/layout.rs +++ b/compiler/rustc_ty_utils/src/layout.rs @@ -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, }; @@ -51,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)); } diff --git a/tests/ui/layout/layout-cycle.stderr b/tests/ui/layout/layout-cycle.stderr index a3cdb7edcc232..ccaeda9acaa4c 100644 --- a/tests/ui/layout/layout-cycle.stderr +++ b/tests/ui/layout/layout-cycle.stderr @@ -2,6 +2,7 @@ error[E0391]: cycle detected when computing layout of `S>` | = note: ...which requires computing layout of ` as Tr>::I`... = note: ...which again requires computing layout of `S>`, completing the cycle + = note: cycle used when computing layout of `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>: a cycle occurred during layout computation