Skip to content

Commit 586bd1f

Browse files
committed
wip wip
1 parent 9ca1f3e commit 586bd1f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+405
-350
lines changed

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,10 @@ pub(crate) fn linking_symbol_name_for_instance_in_crate<'tcx>(
595595

596596
let (conv, args) = instance
597597
.map(|i| {
598-
tcx.fn_abi_of_instance(ty::ParamEnv::reveal_all().and((i, ty::List::empty())))
599-
.unwrap_or_else(|_| bug!("fn_abi_of_instance({i:?}) failed"))
598+
tcx.fn_abi_of_instance(
599+
ty::TypingEnv::fully_monomorphized().as_query_input((i, ty::List::empty())),
600+
)
601+
.unwrap_or_else(|_| bug!("fn_abi_of_instance({i:?}) failed"))
600602
})
601603
.map(|fnabi| (fnabi.conv, &fnabi.args[..]))
602604
.unwrap_or((Conv::Rust, &[]));

compiler/rustc_codegen_ssa/src/base.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
467467
// regions must appear in the argument
468468
// listing.
469469
let main_ret_ty = cx.tcx().normalize_erasing_regions(
470-
ty::ParamEnv::reveal_all(),
470+
ty::TypingEnv::fully_monomorphized(),
471471
main_ret_ty.no_bound_vars().unwrap(),
472472
);
473473

@@ -495,8 +495,7 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
495495
let start_def_id = cx.tcx().require_lang_item(LangItem::Start, None);
496496
let start_instance = ty::Instance::expect_resolve(
497497
cx.tcx(),
498-
ty::TypingMode::PostAnalysis,
499-
ty::ParamEnv::reveal_all(),
498+
ty::TypingEnv::fully_monomorphized(),
500499
start_def_id,
501500
cx.tcx().mk_args(&[main_ret_ty.into()]),
502501
DUMMY_SP,

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathD
2020
use rustc_hir::{CoroutineDesugaring, CoroutineKind, CoroutineSource, Mutability};
2121
use rustc_middle::bug;
2222
use rustc_middle::ty::layout::{IntegerExt, TyAndLayout};
23-
use rustc_middle::ty::{
24-
self, ExistentialProjection, GenericArgKind, GenericArgsRef, ParamEnv, Ty, TyCtxt,
25-
};
23+
use rustc_middle::ty::{self, ExistentialProjection, GenericArgKind, GenericArgsRef, Ty, TyCtxt};
2624
use rustc_target::abi::Integer;
2725
use smallvec::SmallVec;
2826

@@ -82,7 +80,7 @@ fn push_debuginfo_type_name<'tcx>(
8280
ty::Adt(def, args) => {
8381
// `layout_for_cpp_like_fallback` will be `Some` if we want to use the fallback encoding.
8482
let layout_for_cpp_like_fallback = if cpp_like_debuginfo && def.is_enum() {
85-
match tcx.layout_of(ParamEnv::reveal_all().and(t)) {
83+
match tcx.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(t)) {
8684
Ok(layout) => {
8785
if !wants_c_like_enum_debuginfo(tcx, layout) {
8886
Some(layout)
@@ -248,8 +246,10 @@ fn push_debuginfo_type_name<'tcx>(
248246
};
249247

250248
if let Some(principal) = trait_data.principal() {
251-
let principal =
252-
tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), principal);
249+
let principal = tcx.normalize_erasing_late_bound_regions(
250+
ty::TypingEnv::fully_monomorphized(),
251+
principal,
252+
);
253253
push_item_name(tcx, principal.def_id, qualified, output);
254254
let principal_has_generic_params =
255255
push_generic_params_internal(tcx, principal.args, output, visited);
@@ -350,8 +350,10 @@ fn push_debuginfo_type_name<'tcx>(
350350
return;
351351
}
352352

353-
let sig =
354-
tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), t.fn_sig(tcx));
353+
let sig = tcx.normalize_erasing_late_bound_regions(
354+
ty::TypingEnv::fully_monomorphized(),
355+
t.fn_sig(tcx),
356+
);
355357

356358
if cpp_like_debuginfo {
357359
// Format as a C++ function pointer: return_type (*)(params...)
@@ -415,7 +417,8 @@ fn push_debuginfo_type_name<'tcx>(
415417
// In the case of cpp-like debuginfo, the name additionally gets wrapped inside of
416418
// an artificial `enum2$<>` type, as defined in msvc_enum_fallback().
417419
if cpp_like_debuginfo && t.is_coroutine() {
418-
let ty_and_layout = tcx.layout_of(ParamEnv::reveal_all().and(t)).unwrap();
420+
let ty_and_layout =
421+
tcx.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(t)).unwrap();
419422
msvc_enum_fallback(
420423
tcx,
421424
ty_and_layout,
@@ -529,8 +532,8 @@ pub fn compute_debuginfo_vtable_name<'tcx>(
529532
}
530533

531534
if let Some(trait_ref) = trait_ref {
532-
let trait_ref =
533-
tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), trait_ref);
535+
let trait_ref = tcx
536+
.normalize_erasing_late_bound_regions(ty::TypingEnv::fully_monomorphized(), trait_ref);
534537
push_item_name(tcx, trait_ref.def_id, true, &mut vtable_name);
535538
visited.clear();
536539
push_generic_params_internal(tcx, trait_ref.args, &mut vtable_name, &mut visited);
@@ -639,7 +642,7 @@ fn push_generic_params_internal<'tcx>(
639642
output: &mut String,
640643
visited: &mut FxHashSet<Ty<'tcx>>,
641644
) -> bool {
642-
assert_eq!(args, tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), args));
645+
assert_eq!(args, tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), args));
643646
let mut args = args.non_erasable_generics().peekable();
644647
if args.peek().is_none() {
645648
return false;
@@ -678,14 +681,14 @@ fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: ty::Const<'tcx>, output: &mut S
678681
// FIXME: directly extract the bits from a valtree instead of evaluating an
679682
// already evaluated `Const` in order to get the bits.
680683
let bits = ct
681-
.try_to_bits(tcx, ty::ParamEnv::reveal_all())
684+
.try_to_bits(tcx, ty::TypingEnv::fully_monomorphized())
682685
.expect("expected monomorphic const in codegen");
683686
let val = Integer::from_int_ty(&tcx, *ity).size().sign_extend(bits) as i128;
684687
write!(output, "{val}")
685688
}
686689
ty::Uint(_) => {
687690
let val = ct
688-
.try_to_bits(tcx, ty::ParamEnv::reveal_all())
691+
.try_to_bits(tcx, ty::TypingEnv::fully_monomorphized())
689692
.expect("expected monomorphic const in codegen");
690693
write!(output, "{val}")
691694
}

compiler/rustc_codegen_ssa/src/mir/block.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
766766

767767
let do_panic = !bx
768768
.tcx()
769-
.check_validity_requirement((requirement, bx.param_env().and(ty)))
769+
.check_validity_requirement((requirement, bx.typing_env().as_query_input(ty)))
770770
.expect("expect to have layout during codegen");
771771

772772
let layout = bx.layout_of(ty);
@@ -839,8 +839,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
839839
Some(
840840
ty::Instance::expect_resolve(
841841
bx.tcx(),
842-
ty::TypingMode::PostAnalysis,
843-
ty::ParamEnv::reveal_all(),
842+
ty::TypingEnv::fully_monomorphized(),
844843
def_id,
845844
args,
846845
fn_span,
@@ -1181,8 +1180,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
11811180
if let ty::FnDef(def_id, args) = *const_.ty().kind() {
11821181
let instance = ty::Instance::resolve_for_fn_ptr(
11831182
bx.tcx(),
1184-
ty::TypingMode::PostAnalysis,
1185-
ty::ParamEnv::reveal_all(),
1183+
ty::TypingEnv::fully_monomorphized(),
11861184
def_id,
11871185
args,
11881186
)

compiler/rustc_codegen_ssa/src/mir/constant.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
2424
// `MirUsedCollector` visited all required_consts before codegen began, so if we got here
2525
// there can be no more constants that fail to evaluate.
2626
self.monomorphize(constant.const_)
27-
.eval(
28-
self.cx.tcx(),
29-
ty::TypingMode::PostAnalysis,
30-
ty::ParamEnv::reveal_all(),
31-
constant.span,
32-
)
27+
.eval(self.cx.tcx(), ty::TypingEnv::fully_monomorphized(), constant.span)
3328
.expect("erroneous constant missed by mono item collection")
3429
}
3530

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
5959
llresult: Bx::Value,
6060
span: Span,
6161
) -> Result<(), ty::Instance<'tcx>> {
62-
let callee_ty = instance.ty(bx.tcx(), ty::ParamEnv::reveal_all());
62+
let callee_ty = instance.ty(bx.tcx(), ty::TypingEnv::fully_monomorphized());
6363

6464
let ty::FnDef(def_id, fn_args) = *callee_ty.kind() else {
6565
bug!("expected fn item type, found {}", callee_ty);
6666
};
6767

6868
let sig = callee_ty.fn_sig(bx.tcx());
69-
let sig = bx.tcx().normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), sig);
69+
let sig = bx
70+
.tcx()
71+
.normalize_erasing_late_bound_regions(ty::TypingEnv::fully_monomorphized(), sig);
7072
let arg_tys = sig.inputs();
7173
let ret_ty = sig.output();
7274
let name = bx.tcx().item_name(def_id);

compiler/rustc_codegen_ssa/src/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
124124
debug!("monomorphize: self.instance={:?}", self.instance);
125125
self.instance.instantiate_mir_and_normalize_erasing_regions(
126126
self.cx.tcx(),
127-
ty::ParamEnv::reveal_all(),
127+
ty::TypingEnv::fully_monomorphized(),
128128
ty::EarlyBinder::bind(value),
129129
)
130130
}

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
474474
ty::FnDef(def_id, args) => {
475475
let instance = ty::Instance::resolve_for_fn_ptr(
476476
bx.tcx(),
477-
ty::TypingMode::PostAnalysis,
478-
ty::ParamEnv::reveal_all(),
477+
ty::TypingEnv::fully_monomorphized(),
479478
def_id,
480479
args,
481480
)
@@ -710,7 +709,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
710709
mir::NullOp::OffsetOf(fields) => {
711710
let val = bx
712711
.tcx()
713-
.offset_of_subfield(bx.param_env(), layout, fields.iter())
712+
.offset_of_subfield(bx.typing_env(), layout, fields.iter())
714713
.bytes();
715714
bx.cx().const_usize(val)
716715
}

compiler/rustc_codegen_ssa/src/traits/type_.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub trait DerivedTypeCodegenMethods<'tcx>:
7474
}
7575

7676
fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool {
77-
ty.needs_drop(self.tcx(), ty::ParamEnv::reveal_all())
77+
ty.needs_drop(self.tcx(), ty::TypingEnv::fully_monomorphized())
7878
}
7979

8080
fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
@@ -86,12 +86,12 @@ pub trait DerivedTypeCodegenMethods<'tcx>:
8686
}
8787

8888
fn type_has_metadata(&self, ty: Ty<'tcx>) -> bool {
89-
let param_env = ty::ParamEnv::reveal_all();
90-
if ty.is_sized(self.tcx(), param_env) {
89+
let typing_env = ty::TypingEnv::fully_monomorphized();
90+
if ty.is_sized(self.tcx(), typing_env.param_env) {
9191
return false;
9292
}
9393

94-
let tail = self.tcx().struct_tail_for_codegen(ty, param_env);
94+
let tail = self.tcx().struct_tail_for_codegen(ty, typing_env);
9595
match tail.kind() {
9696
ty::Foreign(..) => false,
9797
ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,

compiler/rustc_const_eval/src/check_consts/check.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -625,13 +625,9 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
625625
// This skips the check below that ensures we only call `const fn`.
626626
is_trait = true;
627627

628-
if let Ok(Some(instance)) = Instance::try_resolve(
629-
tcx,
630-
infcx.typing_mode(param_env),
631-
param_env,
632-
callee,
633-
fn_args,
634-
) && let InstanceKind::Item(def) = instance.def
628+
if let Ok(Some(instance)) =
629+
Instance::try_resolve(tcx, infcx.typing_env(param_env), callee, fn_args)
630+
&& let InstanceKind::Item(def) = instance.def
635631
{
636632
// Resolve a trait method call to its concrete implementation, which may be in a
637633
// `const` trait impl. This is only used for the const stability check below, since

compiler/rustc_const_eval/src/check_consts/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
use rustc_errors::DiagCtxtHandle;
88
use rustc_hir::def_id::{DefId, LocalDefId};
9-
use rustc_middle::ty::{self, PolyFnSig, TyCtxt};
9+
use rustc_middle::ty::{self, PolyFnSig, TyCtxt, TypingEnv};
1010
use rustc_middle::{bug, mir};
1111
use rustc_span::Symbol;
1212
use {rustc_attr as attr, rustc_hir as hir};
@@ -41,6 +41,13 @@ impl<'mir, 'tcx> ConstCx<'mir, 'tcx> {
4141
self.tcx.dcx()
4242
}
4343

44+
pub fn typing_env(&self) -> TypingEnv<'tcx> {
45+
// FIXME(#132279): Using a `TypingEnv` while potentially inside of
46+
// a body during analysis is broken, as we may need to handle already
47+
// defined opaques.
48+
TypingEnv { typing_mode: self.body.typing_mode(self.tcx), param_env: self.param_env }
49+
}
50+
4451
pub fn def_id(&self) -> LocalDefId {
4552
self.body.source.def_id().expect_local()
4653
}

compiler/rustc_const_eval/src/check_consts/ops.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,8 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
132132
}
133133
};
134134

135-
let call_kind = call_kind(
136-
tcx,
137-
body.typing_mode(tcx),
138-
ccx.param_env,
139-
callee,
140-
args,
141-
span,
142-
call_source.from_hir_call(),
143-
None,
144-
);
135+
let call_kind =
136+
call_kind(tcx, ccx.typing_env(), callee, args, span, call_source.from_hir_call(), None);
145137

146138
debug!(?call_kind);
147139

compiler/rustc_const_eval/src/check_consts/qualifs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl Qualif for NeedsDrop {
156156
}
157157

158158
fn in_any_value_of_ty<'tcx>(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
159-
ty.needs_drop(cx.tcx, cx.param_env)
159+
ty.needs_drop(cx.tcx, cx.typing_env())
160160
}
161161

162162
fn in_adt_inherently<'tcx>(

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ pub(super) fn op_to_const<'tcx>(
221221
let pointee_ty = imm.layout.ty.builtin_deref(false).unwrap(); // `false` = no raw ptrs
222222
debug_assert!(
223223
matches!(
224-
ecx.tcx.struct_tail_for_codegen(pointee_ty, ecx.param_env).kind(),
224+
ecx.tcx.struct_tail_for_codegen(pointee_ty, ecx.typing_env()).kind(),
225225
ty::Str | ty::Slice(..),
226226
),
227227
"`ConstValue::Slice` is for slice-tailed types only, but got {}",
@@ -280,15 +280,17 @@ pub fn eval_to_const_value_raw_provider<'tcx>(
280280
// opaque types. This is needed for trivial things like `size_of`, but also for using associated
281281
// types that are not specified in the opaque type.
282282
assert_eq!(key.param_env.reveal(), Reveal::All);
283+
let typing_env =
284+
ty::TypingEnv { typing_mode: ty::TypingMode::PostAnalysis, param_env: key.param_env };
283285

284286
// We call `const_eval` for zero arg intrinsics, too, in order to cache their value.
285287
// Catch such calls and evaluate them instead of trying to load a constant's MIR.
286288
if let ty::InstanceKind::Intrinsic(def_id) = key.value.instance.def {
287-
let ty = key.value.instance.ty(tcx, key.param_env);
289+
let ty = key.value.instance.ty(tcx, typing_env);
288290
let ty::FnDef(_, args) = ty.kind() else {
289291
bug!("intrinsic with type {:?}", ty);
290292
};
291-
return eval_nullary_intrinsic(tcx, key.param_env, def_id, args).report_err().map_err(
293+
return eval_nullary_intrinsic(tcx, typing_env, def_id, args).report_err().map_err(
292294
|error| {
293295
let span = tcx.def_span(def_id);
294296

compiler/rustc_const_eval/src/const_eval/machine.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,11 @@ impl<'tcx> CompileTimeInterpCx<'tcx> {
251251
} else if self.tcx.is_lang_item(def_id, LangItem::PanicFmt) {
252252
// For panic_fmt, call const_panic_fmt instead.
253253
let const_def_id = self.tcx.require_lang_item(LangItem::ConstPanicFmt, None);
254+
255+
// TODO why does this use an empty env if we've got a `param_env` right here.
254256
let new_instance = ty::Instance::expect_resolve(
255257
*self.tcx,
256-
// TODO why does this use an empty env if we've got a `param_env` right here.
257-
ty::TypingMode::PostAnalysis,
258-
ty::ParamEnv::reveal_all(),
258+
ty::TypingEnv::fully_monomorphized(),
259259
const_def_id,
260260
instance.args,
261261
self.cur_span(),

compiler/rustc_const_eval/src/const_eval/valtrees.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rustc_abi::{BackendRepr, VariantIdx};
22
use rustc_data_structures::stack::ensure_sufficient_stack;
33
use rustc_middle::mir::interpret::{EvalToValTreeResult, GlobalId};
44
use rustc_middle::ty::layout::{LayoutCx, LayoutOf, TyAndLayout};
5+
use rustc_middle::ty::solve::Reveal;
56
use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt};
67
use rustc_middle::{bug, mir};
78
use rustc_span::DUMMY_SP;
@@ -281,8 +282,9 @@ pub fn valtree_to_const_value<'tcx>(
281282
// the `ValTree` and using `place_projection` and `place_field` to
282283
// create inner `MPlace`s which are filled recursively.
283284
// FIXME Does this need an example?
284-
285285
let (param_env, ty) = param_env_ty.into_parts();
286+
debug_assert_eq!(param_env.reveal(), Reveal::All);
287+
let typing_env = ty::TypingEnv { typing_mode: ty::TypingMode::PostAnalysis, param_env };
286288

287289
match *ty.kind() {
288290
ty::FnDef(..) => {
@@ -302,11 +304,12 @@ pub fn valtree_to_const_value<'tcx>(
302304
let mut ecx =
303305
mk_eval_cx_to_read_const_val(tcx, DUMMY_SP, param_env, CanAccessMutGlobal::No);
304306
let imm = valtree_to_ref(&mut ecx, valtree, inner_ty);
305-
let imm = ImmTy::from_immediate(imm, tcx.layout_of(param_env_ty).unwrap());
307+
let imm =
308+
ImmTy::from_immediate(imm, tcx.layout_of(typing_env.as_query_input(ty)).unwrap());
306309
op_to_const(&ecx, &imm.into(), /* for diagnostics */ false)
307310
}
308311
ty::Tuple(_) | ty::Array(_, _) | ty::Adt(..) => {
309-
let layout = tcx.layout_of(param_env_ty).unwrap();
312+
let layout = tcx.layout_of(typing_env.as_query_input(ty)).unwrap();
310313
if layout.is_zst() {
311314
// Fast path to avoid some allocations.
312315
return mir::ConstValue::ZeroSized;
@@ -319,7 +322,7 @@ pub fn valtree_to_const_value<'tcx>(
319322
let branches = valtree.unwrap_branch();
320323
// Find the non-ZST field. (There can be aligned ZST!)
321324
for (i, &inner_valtree) in branches.iter().enumerate() {
322-
let field = layout.field(&LayoutCx::new(tcx, param_env), i);
325+
let field = layout.field(&LayoutCx::new(tcx, typing_env), i);
323326
if !field.is_zst() {
324327
return valtree_to_const_value(tcx, param_env.and(field.ty), inner_valtree);
325328
}

0 commit comments

Comments
 (0)