Skip to content

Commit 87c1566

Browse files
committed
Introduce TypingMode::Codegen.
1 parent fdb1804 commit 87c1566

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};
@@ -324,23 +325,44 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
324325
tcx: TyCtxt<'tcx>,
325326
key: ty::PseudoCanonicalInput<'tcx, GlobalId<'tcx>>,
326327
) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> {
328+
let ty::PseudoCanonicalInput { typing_env, value } = key;
329+
327330
// This shouldn't be used for statics, since statics are conceptually places,
328331
// not values -- so what we do here could break pointer identity.
329-
assert!(key.value.promoted.is_some() || !tcx.is_static(key.value.instance.def_id()));
330-
// Const eval always happens in PostAnalysis mode . See the comment in
332+
assert!(value.promoted.is_some() || !tcx.is_static(value.instance.def_id()));
333+
334+
// Const eval always happens in PostAnalysis or Codegen mode. See the comment in
331335
// `InterpCx::new` for more details.
332-
debug_assert_eq!(key.typing_env.typing_mode, ty::TypingMode::PostAnalysis);
336+
debug_assert_matches!(
337+
typing_env.typing_mode,
338+
ty::TypingMode::PostAnalysis | ty::TypingMode::Codegen
339+
);
340+
333341
if cfg!(debug_assertions) {
334342
// Make sure we format the instance even if we do not print it.
335343
// This serves as a regression test against an ICE on printing.
336344
// The next two lines concatenated contain some discussion:
337345
// https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/
338346
// subject/anon_const_instance_printing/near/135980032
339-
let instance = with_no_trimmed_paths!(key.value.instance.to_string());
340-
trace!("const eval: {:?} ({})", key, instance);
347+
let instance = with_no_trimmed_paths!(value.instance.to_string());
348+
trace!("const eval: {:?} ({}) inside {:?}", value, instance, typing_env);
349+
}
350+
351+
// We are in codegen. It's very likely this constant has been evaluated in PostAnalysis before.
352+
// Try to reuse this evaluation, and only re-run if we hit a `TooGeneric` error.
353+
if let ty::TypingMode::Codegen = typing_env.typing_mode {
354+
let with_postanalysis = ty::TypingEnv {
355+
typing_mode: ty::TypingMode::PostAnalysis,
356+
param_env: typing_env.param_env,
357+
};
358+
let with_postanalysis = tcx.eval_to_allocation_raw(with_postanalysis.as_query_input(value));
359+
match with_postanalysis {
360+
Ok(_) | Err(ErrorHandled::Reported(..)) => return with_postanalysis,
361+
Err(ErrorHandled::TooGeneric(_)) => {}
362+
}
341363
}
342364

343-
eval_in_interpreter(tcx, key.value, key.typing_env)
365+
eval_in_interpreter(tcx, value, typing_env)
344366
}
345367

346368
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
@@ -1016,7 +1016,8 @@ impl<'tcx> InferCtxt<'tcx> {
10161016
// to support PostBorrowckAnalysis in the old solver as well.
10171017
TypingMode::Coherence
10181018
| TypingMode::PostBorrowckAnalysis { .. }
1019-
| TypingMode::PostAnalysis => false,
1019+
| TypingMode::PostAnalysis
1020+
| TypingMode::Codegen => false,
10201021
}
10211022
}
10221023

@@ -1331,7 +1332,8 @@ impl<'tcx> InferCtxt<'tcx> {
13311332
}
13321333
mode @ (ty::TypingMode::Coherence
13331334
| ty::TypingMode::PostBorrowckAnalysis { .. }
1334-
| ty::TypingMode::PostAnalysis) => mode,
1335+
| ty::TypingMode::PostAnalysis
1336+
| ty::TypingMode::Codegen) => mode,
13351337
};
13361338
ty::TypingEnv { typing_mode, param_env }
13371339
}

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
@@ -1046,7 +1046,7 @@ impl<'tcx> TypingEnv<'tcx> {
10461046
/// use `TypingMode::PostAnalysis`, they may still have where-clauses
10471047
/// in scope.
10481048
pub fn fully_monomorphized() -> TypingEnv<'tcx> {
1049-
TypingEnv { typing_mode: TypingMode::PostAnalysis, param_env: ParamEnv::empty() }
1049+
TypingEnv { typing_mode: TypingMode::Codegen, param_env: ParamEnv::empty() }
10501050
}
10511051

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

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

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
@@ -868,7 +868,8 @@ where
868868
TypingMode::Analysis { .. }
869869
| TypingMode::Borrowck { .. }
870870
| TypingMode::PostBorrowckAnalysis { .. }
871-
| TypingMode::PostAnalysis => {}
871+
| TypingMode::PostAnalysis
872+
| TypingMode::Codegen => {}
872873
}
873874

874875
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
@@ -355,7 +355,7 @@ where
355355
fn opaque_type_is_rigid(&self, def_id: I::DefId) -> bool {
356356
match self.typing_mode() {
357357
// Opaques are never rigid outside of analysis mode.
358-
TypingMode::Coherence | TypingMode::PostAnalysis => false,
358+
TypingMode::Coherence | TypingMode::PostAnalysis | TypingMode::Codegen => false,
359359
// During analysis, opaques are rigid unless they may be defined by
360360
// the current body.
361361
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)