Skip to content

Commit 4b6f78d

Browse files
committed
Do not compute coroutine layout in non-Codegen typing mode.
1 parent dabbc7f commit 4b6f78d

8 files changed

Lines changed: 69 additions & 32 deletions

File tree

compiler/rustc_hir_typeck/src/intrinsicck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub(crate) fn check_transmutes(tcx: TyCtxt<'_>, owner: LocalDefId) -> Result<(),
147147
return Err(e);
148148
};
149149

150-
let typing_env = ty::TypingEnv::post_analysis(tcx, owner);
150+
let typing_env = ty::TypingEnv::codegen(tcx, owner);
151151
let mut result = Ok(());
152152
for &(from, to, hir_id) in &typeck_results.transmutes_to_check {
153153
result = result.and(check_transmute(tcx, typing_env, from, to, hir_id));

compiler/rustc_interface/src/passes.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,10 +1180,10 @@ fn run_required_analyses(tcx: TyCtxt<'_>) {
11801180
&& (!tcx.is_async_drop_in_place_coroutine(def_id.to_def_id()))
11811181
{
11821182
// Eagerly check the unsubstituted layout for cycles.
1183-
tcx.ensure_ok().layout_of(
1184-
ty::TypingEnv::post_analysis(tcx, def_id.to_def_id())
1185-
.as_query_input(tcx.type_of(def_id).instantiate_identity().skip_norm_wip()),
1186-
);
1183+
tcx.ensure_ok()
1184+
.layout_of(ty::TypingEnv::codegen(tcx, def_id.to_def_id()).as_query_input(
1185+
tcx.type_of(def_id).instantiate_identity().skip_norm_wip(),
1186+
));
11871187
}
11881188
});
11891189
});

compiler/rustc_middle/src/queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,7 @@ rustc_queries! {
16271627
/// Like `param_env`, but returns the `ParamEnv` after all opaque types have been
16281628
/// replaced with their hidden type. This is used in the old trait solver
16291629
/// when in `PostAnalysis` mode and should not be called directly.
1630-
query typing_env_normalized_for_post_analysis(def_id: DefId) -> ty::TypingEnv<'tcx> {
1630+
query param_env_normalized_for_post_analysis(def_id: DefId) -> ty::ParamEnv<'tcx> {
16311631
desc { "computing revealed normalized predicates of `{}`", tcx.def_path_str(def_id) }
16321632
}
16331633

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,17 @@ impl<'tcx> ParamEnv<'tcx> {
10401040
pub fn and<T: TypeVisitable<TyCtxt<'tcx>>>(self, value: T) -> ParamEnvAnd<'tcx, T> {
10411041
ParamEnvAnd { param_env: self, value }
10421042
}
1043+
1044+
/// Eagerly reveal all opaque types in the `param_env`.
1045+
pub fn with_normalized(self, tcx: TyCtxt<'tcx>) -> ParamEnv<'tcx> {
1046+
// No need to reveal opaques with the new solver enabled,
1047+
// since we have lazy norm.
1048+
if tcx.next_trait_solver_globally() {
1049+
self
1050+
} else {
1051+
ParamEnv::new(tcx.reveal_opaque_types_in_bounds(self.caller_bounds))
1052+
}
1053+
}
10431054
}
10441055

10451056
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, TypeFoldable, TypeVisitable)]
@@ -1102,31 +1113,44 @@ impl<'tcx> TypingEnv<'tcx> {
11021113
}
11031114

11041115
pub fn post_analysis(tcx: TyCtxt<'tcx>, def_id: impl IntoQueryKey<DefId>) -> TypingEnv<'tcx> {
1105-
let def_id = def_id.into_query_key();
1106-
tcx.typing_env_normalized_for_post_analysis(def_id)
1116+
TypingEnv::new(tcx.param_env_normalized_for_post_analysis(def_id), TypingMode::PostAnalysis)
1117+
}
1118+
1119+
pub fn codegen(tcx: TyCtxt<'tcx>, def_id: impl IntoQueryKey<DefId>) -> TypingEnv<'tcx> {
1120+
TypingEnv::new(tcx.param_env_normalized_for_post_analysis(def_id), TypingMode::Codegen)
11071121
}
11081122

11091123
/// Modify the `typing_mode` to `PostAnalysis` or `Codegen` and eagerly reveal all opaque types
11101124
/// in the `param_env`.
11111125
pub fn with_post_analysis_normalized(self, tcx: TyCtxt<'tcx>) -> TypingEnv<'tcx> {
11121126
let TypingEnv { typing_mode, param_env } = self;
1127+
match typing_mode.0.assert_not_erased() {
1128+
TypingMode::Coherence
1129+
| TypingMode::Analysis { .. }
1130+
| TypingMode::Borrowck { .. }
1131+
| TypingMode::PostBorrowckAnalysis { .. } => {}
1132+
TypingMode::PostAnalysis | TypingMode::Codegen => return self,
1133+
}
11131134

1114-
// No need to reveal opaques with the new solver enabled,
1115-
// since we have lazy norm.
1116-
let param_env = if tcx.next_trait_solver_globally() {
1117-
param_env
1118-
} else {
1119-
match typing_mode.0.assert_not_erased() {
1120-
TypingMode::Coherence
1121-
| TypingMode::Analysis { .. }
1122-
| TypingMode::Borrowck { .. }
1123-
| TypingMode::PostBorrowckAnalysis { .. } => {}
1124-
TypingMode::PostAnalysis | TypingMode::Codegen => return self,
1125-
}
1135+
let param_env = param_env.with_normalized(tcx);
1136+
TypingEnv::new(param_env, TypingMode::PostAnalysis)
1137+
}
11261138

1127-
ParamEnv::new(tcx.reveal_opaque_types_in_bounds(param_env.caller_bounds()))
1128-
};
1129-
TypingEnv { typing_mode: TypingModeEqWrapper(TypingMode::PostAnalysis), param_env }
1139+
/// Modify the `typing_mode` to `PostAnalysis` or `Codegen` and eagerly reveal all opaque types
1140+
/// in the `param_env`.
1141+
pub fn with_codegen_normalized(self, tcx: TyCtxt<'tcx>) -> TypingEnv<'tcx> {
1142+
let TypingEnv { typing_mode, param_env } = self;
1143+
match typing_mode.0.assert_not_erased() {
1144+
TypingMode::Coherence
1145+
| TypingMode::Analysis { .. }
1146+
| TypingMode::Borrowck { .. }
1147+
| TypingMode::PostBorrowckAnalysis { .. }
1148+
| TypingMode::PostAnalysis => {}
1149+
TypingMode::Codegen => return self,
1150+
}
1151+
1152+
let param_env = param_env.with_normalized(tcx);
1153+
TypingEnv::new(param_env, TypingMode::Codegen)
11301154
}
11311155

11321156
/// Combine this typing environment with the given `value` to be used by

compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,8 @@ impl<'tcx> ConstToPat<'tcx> {
102102
//
103103
// FIXME: `const_eval_resolve_for_typeck` should probably just modify the env itself
104104
// instead of having this logic here
105-
let typing_env = self
106-
.tcx
107-
.erase_and_anonymize_regions(self.typing_env)
108-
.with_post_analysis_normalized(self.tcx);
105+
let typing_env =
106+
self.tcx.erase_and_anonymize_regions(self.typing_env).with_codegen_normalized(self.tcx);
109107
let uv = self.tcx.erase_and_anonymize_regions(uv);
110108

111109
// try to resolve e.g. associated constants to their definition on an impl, and then

compiler/rustc_ty_utils/src/layout.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,18 @@ fn layout_of_uncached<'tcx>(
537537
}
538538

539539
ty::Coroutine(def_id, args) => {
540+
match cx.typing_env.typing_mode() {
541+
ty::TypingMode::Codegen => {}
542+
ty::TypingMode::Coherence
543+
| ty::TypingMode::Analysis { .. }
544+
| ty::TypingMode::Borrowck { .. }
545+
| ty::TypingMode::PostBorrowckAnalysis { .. }
546+
| ty::TypingMode::ErasedNotCoherence(_)
547+
| ty::TypingMode::PostAnalysis => {
548+
return Err(error(cx, LayoutError::TooGeneric(ty)));
549+
}
550+
}
551+
540552
use rustc_middle::ty::layout::PrimitiveExt as _;
541553

542554
let info = tcx.coroutine_layout(def_id, args)?;
@@ -704,7 +716,10 @@ fn layout_of_uncached<'tcx>(
704716

705717
let maybe_unsized = def.is_struct()
706718
&& def.non_enum_variant().tail_opt().is_some_and(|last_field| {
707-
let typing_env = ty::TypingEnv::post_analysis(tcx, def.did());
719+
let typing_env = ty::TypingEnv::new(
720+
tcx.param_env_normalized_for_post_analysis(def.did()),
721+
cx.typing_env.typing_mode(),
722+
);
708723
!tcx.type_of(last_field.did)
709724
.instantiate_identity()
710725
.skip_norm_wip()

compiler/rustc_ty_utils/src/ty.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ImplTraitInTraitFinder<'_, 'tcx> {
292292
}
293293
}
294294

295-
fn typing_env_normalized_for_post_analysis(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TypingEnv<'_> {
296-
ty::TypingEnv::non_body_analysis(tcx, def_id).with_post_analysis_normalized(tcx)
295+
fn param_env_normalized_for_post_analysis(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
296+
tcx.param_env(def_id).with_normalized(tcx)
297297
}
298298

299299
/// Check if a function is async.
@@ -405,7 +405,7 @@ pub(crate) fn provide(providers: &mut Providers) {
405405
asyncness,
406406
adt_sizedness_constraint,
407407
param_env,
408-
typing_env_normalized_for_post_analysis,
408+
param_env_normalized_for_post_analysis,
409409
defaultness,
410410
unsizing_params_for_adt,
411411
impl_self_is_guaranteed_unsized,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeFuture {
6464
&& let ty = cx.typeck_results().expr_ty(arg)
6565
&& let Some(future_trait_def_id) = cx.tcx.lang_items().future_trait()
6666
&& implements_trait(cx, ty, future_trait_def_id, &[])
67-
&& let Ok(layout) = cx.tcx.layout_of(cx.typing_env().as_query_input(ty))
67+
&& let Ok(layout) = cx.tcx.layout_of(cx.typing_env().with_codegen_normalized(cx.tcx).as_query_input(ty))
6868
&& let size = layout.layout.size()
6969
&& size >= Size::from_bytes(self.future_size_threshold)
7070
{

0 commit comments

Comments
 (0)