Skip to content

Commit 1f0c447

Browse files
committed
Introduce TypingMode::Codegen.
1 parent 6d6a08c commit 1f0c447

File tree

31 files changed

+182
-101
lines changed

31 files changed

+182
-101
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::assert_matches::debug_assert_matches;
12
use std::sync::atomic::Ordering::Relaxed;
23

34
use either::{Left, Right};
@@ -331,23 +332,44 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
331332
tcx: TyCtxt<'tcx>,
332333
key: ty::PseudoCanonicalInput<'tcx, GlobalId<'tcx>>,
333334
) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> {
335+
let ty::PseudoCanonicalInput { typing_env, value } = key;
336+
334337
// This shouldn't be used for statics, since statics are conceptually places,
335338
// not values -- so what we do here could break pointer identity.
336-
assert!(key.value.promoted.is_some() || !tcx.is_static(key.value.instance.def_id()));
337-
// Const eval always happens in PostAnalysis mode . See the comment in
339+
assert!(value.promoted.is_some() || !tcx.is_static(value.instance.def_id()));
340+
341+
// Const eval always happens in PostAnalysis or Codegen mode. See the comment in
338342
// `InterpCx::new` for more details.
339-
debug_assert_eq!(key.typing_env.typing_mode, ty::TypingMode::PostAnalysis);
343+
debug_assert_matches!(
344+
typing_env.typing_mode,
345+
ty::TypingMode::PostAnalysis | ty::TypingMode::Codegen
346+
);
347+
340348
if cfg!(debug_assertions) {
341349
// Make sure we format the instance even if we do not print it.
342350
// This serves as a regression test against an ICE on printing.
343351
// The next two lines concatenated contain some discussion:
344352
// https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/
345353
// subject/anon_const_instance_printing/near/135980032
346-
let instance = with_no_trimmed_paths!(key.value.instance.to_string());
347-
trace!("const eval: {:?} ({})", key, instance);
354+
let instance = with_no_trimmed_paths!(value.instance.to_string());
355+
trace!("const eval: {:?} ({}) inside {:?}", value, instance, typing_env);
356+
}
357+
358+
// We are in codegen. It's very likely this constant has been evaluated in PostAnalysis before.
359+
// Try to reuse this evaluation, and only re-run if we hit a `TooGeneric` error.
360+
if let ty::TypingMode::Codegen = typing_env.typing_mode {
361+
let with_postanalysis = ty::TypingEnv {
362+
typing_mode: ty::TypingMode::PostAnalysis,
363+
param_env: typing_env.param_env,
364+
};
365+
let with_postanalysis = tcx.eval_to_allocation_raw(with_postanalysis.as_query_input(value));
366+
match with_postanalysis {
367+
Ok(_) | Err(ErrorHandled::Reported(..)) => return with_postanalysis,
368+
Err(ErrorHandled::TooGeneric(_)) => {}
369+
}
348370
}
349371

350-
eval_in_interpreter(tcx, key.value, key.typing_env)
372+
eval_in_interpreter(tcx, value, typing_env)
351373
}
352374

353375
fn eval_in_interpreter<'tcx, R: InterpretationResult<'tcx>>(

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::assert_matches::debug_assert_matches;
2+
13
use rustc_abi::{BackendRepr, FieldIdx, VariantIdx};
24
use rustc_data_structures::stack::ensure_sufficient_stack;
35
use rustc_middle::mir::interpret::{EvalToValTreeResult, GlobalId, ValTreeCreationError};
@@ -230,9 +232,12 @@ pub(crate) fn eval_to_valtree<'tcx>(
230232
typing_env: ty::TypingEnv<'tcx>,
231233
cid: GlobalId<'tcx>,
232234
) -> EvalToValTreeResult<'tcx> {
233-
// Const eval always happens in PostAnalysis mode . See the comment in
235+
// Const eval always happens in PostAnalysis or Codegen mode . See the comment in
234236
// `InterpCx::new` for more details.
235-
debug_assert_eq!(typing_env.typing_mode, ty::TypingMode::PostAnalysis);
237+
debug_assert_matches!(
238+
typing_env.typing_mode,
239+
ty::TypingMode::PostAnalysis | ty::TypingMode::Codegen
240+
);
236241
let const_alloc = tcx.eval_to_allocation_raw(typing_env.as_query_input(cid))?;
237242

238243
// FIXME Need to provide a span to `eval_to_valtree`

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,15 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
226226
typing_env: ty::TypingEnv<'tcx>,
227227
machine: M,
228228
) -> Self {
229-
// Const eval always happens in post analysis mode in order to be able to use the hidden types of
230-
// opaque types. This is needed for trivial things like `size_of`, but also for using associated
231-
// types that are not specified in the opaque type. We also use MIR bodies whose opaque types have
232-
// already been revealed, so we'd be able to at least partially observe the hidden types anyways.
233-
debug_assert_matches!(typing_env.typing_mode, ty::TypingMode::PostAnalysis);
229+
// Const eval always happens in post analysis or codegen mode in order to be able to use
230+
// the hidden types of opaque types. This is needed for trivial things like `size_of`, but
231+
// also for using associated types that are not specified in the opaque type. We also use
232+
// MIR bodies whose opaque types have already been revealed, so we'd be able to at least
233+
// partially observe the hidden types anyways.
234+
debug_assert_matches!(
235+
typing_env.typing_mode,
236+
ty::TypingMode::PostAnalysis | ty::TypingMode::Codegen
237+
);
234238
InterpCx {
235239
machine,
236240
tcx: tcx.at(root_span),

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,8 @@ impl<'tcx> InferCtxt<'tcx> {
10131013
// to support PostBorrowckAnalysis in the old solver as well.
10141014
TypingMode::Coherence
10151015
| TypingMode::PostBorrowckAnalysis { .. }
1016-
| TypingMode::PostAnalysis => false,
1016+
| TypingMode::PostAnalysis
1017+
| TypingMode::Codegen => false,
10171018
}
10181019
}
10191020

@@ -1328,7 +1329,8 @@ impl<'tcx> InferCtxt<'tcx> {
13281329
}
13291330
mode @ (ty::TypingMode::Coherence
13301331
| ty::TypingMode::PostBorrowckAnalysis { .. }
1331-
| ty::TypingMode::PostAnalysis) => mode,
1332+
| ty::TypingMode::PostAnalysis
1333+
| ty::TypingMode::Codegen) => mode,
13321334
};
13331335
ty::TypingEnv { typing_mode, param_env }
13341336
}

compiler/rustc_infer/src/infer/opaque_types/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ impl<'tcx> InferCtxt<'tcx> {
276276
.map(|obligation| obligation.as_goal()),
277277
);
278278
}
279-
mode @ (ty::TypingMode::PostBorrowckAnalysis { .. } | ty::TypingMode::PostAnalysis) => {
279+
mode @ (ty::TypingMode::PostBorrowckAnalysis { .. }
280+
| ty::TypingMode::PostAnalysis
281+
| ty::TypingMode::Codegen) => {
280282
bug!("insert hidden type in {mode:?}")
281283
}
282284
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ impl<'tcx> TypingEnv<'tcx> {
10451045
/// use `TypingMode::PostAnalysis`, they may still have where-clauses
10461046
/// in scope.
10471047
pub fn fully_monomorphized() -> TypingEnv<'tcx> {
1048-
TypingEnv { typing_mode: TypingMode::PostAnalysis, param_env: ParamEnv::empty() }
1048+
TypingEnv { typing_mode: TypingMode::Codegen, param_env: ParamEnv::empty() }
10491049
}
10501050

10511051
/// Create a typing environment for use during analysis outside of a body.
@@ -1064,11 +1064,11 @@ impl<'tcx> TypingEnv<'tcx> {
10641064
tcx.typing_env_normalized_for_post_analysis(def_id)
10651065
}
10661066

1067-
/// Modify the `typing_mode` to `PostAnalysis` and eagerly reveal all
1068-
/// opaque types in the `param_env`.
1067+
/// Modify the `typing_mode` to `PostAnalysis` or `Codegen` and eagerly reveal all opaque types
1068+
/// in the `param_env`.
10691069
pub fn with_post_analysis_normalized(self, tcx: TyCtxt<'tcx>) -> TypingEnv<'tcx> {
10701070
let TypingEnv { typing_mode, param_env } = self;
1071-
if let TypingMode::PostAnalysis = typing_mode {
1071+
if let TypingMode::PostAnalysis | TypingMode::Codegen = typing_mode {
10721072
return self;
10731073
}
10741074

compiler/rustc_mir_transform/src/elaborate_drop.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::assert_matches::debug_assert_matches;
12
use std::{fmt, iter, mem};
23

34
use rustc_abi::{FIRST_VARIANT, FieldIdx, VariantIdx};
@@ -510,7 +511,10 @@ where
510511
let subpath = self.elaborator.field_subpath(variant_path, field_idx);
511512
let tcx = self.tcx();
512513

513-
assert_eq!(self.elaborator.typing_env().typing_mode, ty::TypingMode::PostAnalysis);
514+
debug_assert_matches!(
515+
self.elaborator.typing_env().typing_mode,
516+
ty::TypingMode::PostAnalysis | ty::TypingMode::Codegen
517+
);
514518
let field_ty = match tcx.try_normalize_erasing_regions(
515519
self.elaborator.typing_env(),
516520
field.ty(tcx, args),

compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,8 @@ where
904904
TypingMode::Analysis { .. }
905905
| TypingMode::Borrowck { .. }
906906
| TypingMode::PostBorrowckAnalysis { .. }
907-
| TypingMode::PostAnalysis => {}
907+
| TypingMode::PostAnalysis
908+
| TypingMode::Codegen => {}
908909
}
909910

910911
let mut i = 0;

compiler/rustc_next_trait_solver/src/solve/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ where
363363
fn opaque_type_is_rigid(&self, def_id: I::DefId) -> bool {
364364
match self.typing_mode() {
365365
// Opaques are never rigid outside of analysis mode.
366-
TypingMode::Coherence | TypingMode::PostAnalysis => false,
366+
TypingMode::Coherence | TypingMode::PostAnalysis | TypingMode::Codegen => false,
367367
// During analysis, opaques are rigid unless they may be defined by
368368
// the current body.
369369
TypingMode::Analysis { defining_opaque_types_and_generators: non_rigid_opaques }

compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@ where
281281
ty::TypingMode::Analysis { .. }
282282
| ty::TypingMode::Borrowck { .. }
283283
| ty::TypingMode::PostBorrowckAnalysis { .. }
284-
| ty::TypingMode::PostAnalysis => {
284+
| ty::TypingMode::PostAnalysis
285+
| ty::TypingMode::Codegen => {
285286
ecx.structurally_instantiate_normalizes_to_term(
286287
goal,
287288
goal.predicate.alias,
@@ -320,7 +321,8 @@ where
320321
ty::TypingMode::Analysis { .. }
321322
| ty::TypingMode::Borrowck { .. }
322323
| ty::TypingMode::PostBorrowckAnalysis { .. }
323-
| ty::TypingMode::PostAnalysis => {
324+
| ty::TypingMode::PostAnalysis
325+
| ty::TypingMode::Codegen => {
324326
ecx.structurally_instantiate_normalizes_to_term(
325327
goal,
326328
goal.predicate.alias,

0 commit comments

Comments
 (0)