Skip to content

Commit ebaf329

Browse files
committed
Introduce TypingMode::Codegen.
1 parent 042c759 commit ebaf329

31 files changed

Lines changed: 129 additions & 65 deletions

File tree

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -369,33 +369,49 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
369369
tcx: TyCtxt<'tcx>,
370370
key: ty::PseudoCanonicalInput<'tcx, GlobalId<'tcx>>,
371371
) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> {
372+
let ty::PseudoCanonicalInput { typing_env, value } = key;
373+
372374
// This shouldn't be used for statics, since statics are conceptually places,
373375
// not values -- so what we do here could break pointer identity.
374376
assert!(key.value.promoted.is_some() || !tcx.is_static(key.value.instance.def_id()));
375377

376-
if cfg!(debug_assertions) {
377-
match key.typing_env.typing_mode().assert_not_erased() {
378-
ty::TypingMode::PostAnalysis => {}
379-
ty::TypingMode::Coherence
380-
| ty::TypingMode::Analysis { .. }
381-
| ty::TypingMode::Borrowck { .. }
382-
| ty::TypingMode::PostBorrowckAnalysis { .. } => {
378+
match key.typing_env.typing_mode().assert_not_erased() {
379+
ty::TypingMode::PostAnalysis => {}
380+
// We are in codegen. It's very likely this constant has been evaluated in PostAnalysis before.
381+
// Try to reuse this evaluation, and only re-run if we hit a `TooGeneric` error.
382+
ty::TypingMode::Codegen => {
383+
let with_postanalysis =
384+
ty::TypingEnv::new(typing_env.param_env, ty::TypingMode::PostAnalysis);
385+
let with_postanalysis =
386+
tcx.eval_to_allocation_raw(with_postanalysis.as_query_input(value));
387+
match with_postanalysis {
388+
Ok(_) | Err(ErrorHandled::Reported(..)) => return with_postanalysis,
389+
Err(ErrorHandled::TooGeneric(_)) => {}
390+
}
391+
}
392+
ty::TypingMode::Coherence
393+
| ty::TypingMode::Analysis { .. }
394+
| ty::TypingMode::Borrowck { .. }
395+
| ty::TypingMode::PostBorrowckAnalysis { .. } => {
396+
if cfg!(debug_assertions) {
383397
bug!(
384-
"Const eval should always happens in PostAnalysis mode. See the comment in `InterpCx::new` for more details."
398+
"Const eval should always happens in PostAnalysis or Codegen mode. See the comment in `InterpCx::new` for more details."
385399
)
386400
}
387401
}
402+
}
388403

404+
if cfg!(debug_assertions) {
389405
// Make sure we format the instance even if we do not print it.
390406
// This serves as a regression test against an ICE on printing.
391407
// The next two lines concatenated contain some discussion:
392408
// https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/
393409
// subject/anon_const_instance_printing/near/135980032
394-
let instance = with_no_trimmed_paths!(key.value.instance.to_string());
395-
trace!("const eval: {:?} ({})", key, instance);
410+
let instance = with_no_trimmed_paths!(value.instance.to_string());
411+
trace!("const eval: {:?} ({}) inside {:?}", value, instance, typing_env);
396412
}
397413

398-
eval_in_interpreter(tcx, key.value, key.typing_env)
414+
eval_in_interpreter(tcx, value, typing_env)
399415
}
400416

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

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,13 @@ pub(crate) fn eval_to_valtree<'tcx>(
243243
) -> EvalToValTreeResult<'tcx> {
244244
if cfg!(debug_assertions) {
245245
match typing_env.typing_mode().assert_not_erased() {
246-
ty::TypingMode::PostAnalysis => {}
246+
ty::TypingMode::PostAnalysis | ty::TypingMode::Codegen => {}
247247
ty::TypingMode::Coherence
248248
| ty::TypingMode::Analysis { .. }
249249
| ty::TypingMode::Borrowck { .. }
250250
| ty::TypingMode::PostBorrowckAnalysis { .. } => {
251251
bug!(
252-
"Const eval should always happens in PostAnalysis mode. See the comment in `InterpCx::new` for more details."
252+
"Const eval should always happens in PostAnalysis or Codegen mode. See the comment in `InterpCx::new` for more details."
253253
)
254254
}
255255
}

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,12 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
244244
// already been revealed, so we'd be able to at least partially observe the hidden types anyways.
245245
if cfg!(debug_assertions) {
246246
match typing_env.typing_mode().assert_not_erased() {
247-
TypingMode::PostAnalysis => {}
247+
TypingMode::PostAnalysis | TypingMode::Codegen => {}
248248
TypingMode::Coherence
249249
| TypingMode::Analysis { .. }
250250
| TypingMode::Borrowck { .. }
251251
| TypingMode::PostBorrowckAnalysis { .. } => {
252-
bug!("Const eval should always happens in PostAnalysis mode.");
252+
bug!("Const eval should always happens in PostAnalysis or Codegen mode.");
253253
}
254254
}
255255
}

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
663663
ty::TypingMode::Coherence
664664
| ty::TypingMode::Borrowck { .. }
665665
| ty::TypingMode::PostBorrowckAnalysis { .. }
666-
| ty::TypingMode::PostAnalysis => {
666+
| ty::TypingMode::PostAnalysis
667+
| ty::TypingMode::Codegen => {
667668
bug!()
668669
}
669670
};

compiler/rustc_hir_typeck/src/opaque_types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
105105
ty::TypingMode::Coherence
106106
| ty::TypingMode::Borrowck { .. }
107107
| ty::TypingMode::PostBorrowckAnalysis { .. }
108-
| ty::TypingMode::PostAnalysis => {
108+
| ty::TypingMode::PostAnalysis
109+
| ty::TypingMode::Codegen => {
109110
bug!()
110111
}
111112
};

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ impl<'tcx> Drop for InferCtxt<'tcx> {
350350
TypingMode::Coherence
351351
| TypingMode::Analysis { .. }
352352
| TypingMode::PostBorrowckAnalysis { .. }
353-
| TypingMode::PostAnalysis => {}
353+
| TypingMode::PostAnalysis
354+
| TypingMode::Codegen => {}
354355
// In erased mode, the opaque type storage is always empty
355356
TypingMode::ErasedNotCoherence(..) => {}
356357
TypingMode::Borrowck { .. } => {
@@ -1156,7 +1157,8 @@ impl<'tcx> InferCtxt<'tcx> {
11561157
// to support PostBorrowckAnalysis in the old solver as well.
11571158
TypingMode::Coherence
11581159
| TypingMode::PostBorrowckAnalysis { .. }
1159-
| TypingMode::PostAnalysis => false,
1160+
| TypingMode::PostAnalysis
1161+
| TypingMode::Codegen => false,
11601162
}
11611163
}
11621164

@@ -1486,7 +1488,8 @@ impl<'tcx> InferCtxt<'tcx> {
14861488
}
14871489
mode @ (ty::TypingMode::Coherence
14881490
| ty::TypingMode::PostBorrowckAnalysis { .. }
1489-
| ty::TypingMode::PostAnalysis) => mode,
1491+
| ty::TypingMode::PostAnalysis
1492+
| ty::TypingMode::Codegen) => mode,
14901493
ty::TypingMode::ErasedNotCoherence(MayBeErased) => unreachable!(),
14911494
};
14921495
ty::TypingEnv::new(param_env, typing_mode)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,9 @@ impl<'tcx> InferCtxt<'tcx> {
283283
.map(|obligation| obligation.as_goal()),
284284
);
285285
}
286-
mode @ (ty::TypingMode::PostBorrowckAnalysis { .. } | ty::TypingMode::PostAnalysis) => {
286+
mode @ (ty::TypingMode::PostBorrowckAnalysis { .. }
287+
| ty::TypingMode::PostAnalysis
288+
| ty::TypingMode::Codegen) => {
287289
bug!("insert hidden type in {mode:?}")
288290
}
289291
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ impl<'tcx> TypingEnv<'tcx> {
10851085
/// use `TypingMode::PostAnalysis`, they may still have where-clauses
10861086
/// in scope.
10871087
pub fn fully_monomorphized() -> TypingEnv<'tcx> {
1088-
Self::new(ParamEnv::empty(), TypingMode::PostAnalysis)
1088+
Self::new(ParamEnv::empty(), TypingMode::Codegen)
10891089
}
10901090

10911091
/// Create a typing environment for use during analysis outside of a body.
@@ -1106,8 +1106,8 @@ impl<'tcx> TypingEnv<'tcx> {
11061106
tcx.typing_env_normalized_for_post_analysis(def_id)
11071107
}
11081108

1109-
/// Modify the `typing_mode` to `PostAnalysis` and eagerly reveal all
1110-
/// opaque types in the `param_env`.
1109+
/// Modify the `typing_mode` to `PostAnalysis` or `Codegen` and eagerly reveal all opaque types
1110+
/// in the `param_env`.
11111111
pub fn with_post_analysis_normalized(self, tcx: TyCtxt<'tcx>) -> TypingEnv<'tcx> {
11121112
let TypingEnv { typing_mode, param_env } = self;
11131113

@@ -1121,7 +1121,7 @@ impl<'tcx> TypingEnv<'tcx> {
11211121
| TypingMode::Analysis { .. }
11221122
| TypingMode::Borrowck { .. }
11231123
| TypingMode::PostBorrowckAnalysis { .. } => {}
1124-
TypingMode::PostAnalysis => return self,
1124+
TypingMode::PostAnalysis | TypingMode::Codegen => return self,
11251125
}
11261126

11271127
ParamEnv::new(tcx.reveal_opaque_types_in_bounds(param_env.caller_bounds()))

compiler/rustc_mir_transform/src/elaborate_drop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ where
780780
let tcx = self.tcx();
781781

782782
match self.elaborator.typing_env().typing_mode().assert_not_erased() {
783-
ty::TypingMode::PostAnalysis => {}
783+
ty::TypingMode::PostAnalysis | ty::TypingMode::Codegen => {}
784784
ty::TypingMode::Coherence
785785
| ty::TypingMode::Analysis { .. }
786786
| ty::TypingMode::Borrowck { .. }

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ where
484484
| TypingMode::Borrowck { .. }
485485
| TypingMode::PostBorrowckAnalysis { .. }
486486
| TypingMode::PostAnalysis
487+
| TypingMode::Codegen
487488
| TypingMode::ErasedNotCoherence(MayBeErased) => !candidates.iter().any(|c| {
488489
matches!(
489490
c.source,
@@ -1056,7 +1057,8 @@ where
10561057
TypingMode::Coherence
10571058
| TypingMode::Borrowck { .. }
10581059
| TypingMode::PostBorrowckAnalysis { .. }
1059-
| TypingMode::PostAnalysis => vec![],
1060+
| TypingMode::PostAnalysis
1061+
| TypingMode::Codegen => vec![],
10601062
TypingMode::ErasedNotCoherence(MayBeErased) => {
10611063
self.opaque_accesses
10621064
.rerun_if_any_opaque_has_infer_as_hidden_type(RerunReason::SelfTyInfer)?;

0 commit comments

Comments
 (0)