Skip to content

Commit f911e57

Browse files
committed
Auto merge of rust-lang#122900 - matthiaskrgr:rollup-nls90mb, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#114009 (compiler: allow transmute of ZST arrays with generics) - rust-lang#122195 (Note that the caller chooses a type for type param) - rust-lang#122651 (Suggest `_` for missing generic arguments in turbofish) - rust-lang#122784 (Add `tag_for_variant` query) - rust-lang#122839 (Split out `PredicatePolarity` from `ImplPolarity`) - rust-lang#122873 (Merge my contributor emails into one using mailmap) - rust-lang#122885 (Adjust better spastorino membership to triagebot's adhoc_groups) - rust-lang#122888 (add a couple more tests) r? `@ghost` `@rustbot` modify labels: rollup
2 parents b3df0d7 + 4879338 commit f911e57

File tree

72 files changed

+773
-421
lines changed

Some content is hidden

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

72 files changed

+773
-421
lines changed

.mailmap

+1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ James Hinshelwood <[email protected]> <[email protected]>
259259
260260
James Perry <[email protected]>
261261
James Sanderson <[email protected]>
262+
262263
Jaro Fietz <[email protected]>
263264
Jason Fager <[email protected]>
264265

compiler/rustc_borrowck/src/type_check/canonical.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
8282
) {
8383
self.prove_predicate(
8484
ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::Trait(
85-
ty::TraitPredicate { trait_ref, polarity: ty::ImplPolarity::Positive },
85+
ty::TraitPredicate { trait_ref, polarity: ty::PredicatePolarity::Positive },
8686
))),
8787
locations,
8888
category,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
use crate::interpret::{self, HasStaticRootDefId, ImmTy, Immediate, InterpCx, PointerArithmetic};
2+
use rustc_middle::mir::interpret::{AllocId, ConstAllocation, InterpResult};
3+
use rustc_middle::mir::*;
4+
use rustc_middle::query::TyCtxtAt;
5+
use rustc_middle::ty;
6+
use rustc_middle::ty::layout::TyAndLayout;
7+
use rustc_span::def_id::DefId;
8+
9+
/// Macro for machine-specific `InterpError` without allocation.
10+
/// (These will never be shown to the user, but they help diagnose ICEs.)
11+
pub macro throw_machine_stop_str($($tt:tt)*) {{
12+
// We make a new local type for it. The type itself does not carry any information,
13+
// but its vtable (for the `MachineStopType` trait) does.
14+
#[derive(Debug)]
15+
struct Zst;
16+
// Printing this type shows the desired string.
17+
impl std::fmt::Display for Zst {
18+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19+
write!(f, $($tt)*)
20+
}
21+
}
22+
23+
impl rustc_middle::mir::interpret::MachineStopType for Zst {
24+
fn diagnostic_message(&self) -> rustc_errors::DiagMessage {
25+
self.to_string().into()
26+
}
27+
28+
fn add_args(
29+
self: Box<Self>,
30+
_: &mut dyn FnMut(rustc_errors::DiagArgName, rustc_errors::DiagArgValue),
31+
) {}
32+
}
33+
throw_machine_stop!(Zst)
34+
}}
35+
36+
pub struct DummyMachine;
37+
38+
impl HasStaticRootDefId for DummyMachine {
39+
fn static_def_id(&self) -> Option<rustc_hir::def_id::LocalDefId> {
40+
None
41+
}
42+
}
43+
44+
impl<'mir, 'tcx: 'mir> interpret::Machine<'mir, 'tcx> for DummyMachine {
45+
interpret::compile_time_machine!(<'mir, 'tcx>);
46+
type MemoryKind = !;
47+
const PANIC_ON_ALLOC_FAIL: bool = true;
48+
49+
#[inline(always)]
50+
fn enforce_alignment(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
51+
false // no reason to enforce alignment
52+
}
53+
54+
fn enforce_validity(_ecx: &InterpCx<'mir, 'tcx, Self>, _layout: TyAndLayout<'tcx>) -> bool {
55+
false
56+
}
57+
58+
fn before_access_global(
59+
_tcx: TyCtxtAt<'tcx>,
60+
_machine: &Self,
61+
_alloc_id: AllocId,
62+
alloc: ConstAllocation<'tcx>,
63+
_static_def_id: Option<DefId>,
64+
is_write: bool,
65+
) -> InterpResult<'tcx> {
66+
if is_write {
67+
throw_machine_stop_str!("can't write to global");
68+
}
69+
70+
// If the static allocation is mutable, then we can't const prop it as its content
71+
// might be different at runtime.
72+
if alloc.inner().mutability.is_mut() {
73+
throw_machine_stop_str!("can't access mutable globals in ConstProp");
74+
}
75+
76+
Ok(())
77+
}
78+
79+
fn find_mir_or_eval_fn(
80+
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
81+
_instance: ty::Instance<'tcx>,
82+
_abi: rustc_target::spec::abi::Abi,
83+
_args: &[interpret::FnArg<'tcx, Self::Provenance>],
84+
_destination: &interpret::MPlaceTy<'tcx, Self::Provenance>,
85+
_target: Option<BasicBlock>,
86+
_unwind: UnwindAction,
87+
) -> interpret::InterpResult<'tcx, Option<(&'mir Body<'tcx>, ty::Instance<'tcx>)>> {
88+
unimplemented!()
89+
}
90+
91+
fn panic_nounwind(
92+
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
93+
_msg: &str,
94+
) -> interpret::InterpResult<'tcx> {
95+
unimplemented!()
96+
}
97+
98+
fn call_intrinsic(
99+
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
100+
_instance: ty::Instance<'tcx>,
101+
_args: &[interpret::OpTy<'tcx, Self::Provenance>],
102+
_destination: &interpret::MPlaceTy<'tcx, Self::Provenance>,
103+
_target: Option<BasicBlock>,
104+
_unwind: UnwindAction,
105+
) -> interpret::InterpResult<'tcx> {
106+
unimplemented!()
107+
}
108+
109+
fn assert_panic(
110+
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
111+
_msg: &rustc_middle::mir::AssertMessage<'tcx>,
112+
_unwind: UnwindAction,
113+
) -> interpret::InterpResult<'tcx> {
114+
unimplemented!()
115+
}
116+
117+
fn binary_ptr_op(
118+
ecx: &InterpCx<'mir, 'tcx, Self>,
119+
bin_op: BinOp,
120+
left: &interpret::ImmTy<'tcx, Self::Provenance>,
121+
right: &interpret::ImmTy<'tcx, Self::Provenance>,
122+
) -> interpret::InterpResult<'tcx, (ImmTy<'tcx, Self::Provenance>, bool)> {
123+
use rustc_middle::mir::BinOp::*;
124+
Ok(match bin_op {
125+
Eq | Ne | Lt | Le | Gt | Ge => {
126+
// Types can differ, e.g. fn ptrs with different `for`.
127+
assert_eq!(left.layout.abi, right.layout.abi);
128+
let size = ecx.pointer_size();
129+
// Just compare the bits. ScalarPairs are compared lexicographically.
130+
// We thus always compare pairs and simply fill scalars up with 0.
131+
// If the pointer has provenance, `to_bits` will return `Err` and we bail out.
132+
let left = match **left {
133+
Immediate::Scalar(l) => (l.to_bits(size)?, 0),
134+
Immediate::ScalarPair(l1, l2) => (l1.to_bits(size)?, l2.to_bits(size)?),
135+
Immediate::Uninit => panic!("we should never see uninit data here"),
136+
};
137+
let right = match **right {
138+
Immediate::Scalar(r) => (r.to_bits(size)?, 0),
139+
Immediate::ScalarPair(r1, r2) => (r1.to_bits(size)?, r2.to_bits(size)?),
140+
Immediate::Uninit => panic!("we should never see uninit data here"),
141+
};
142+
let res = match bin_op {
143+
Eq => left == right,
144+
Ne => left != right,
145+
Lt => left < right,
146+
Le => left <= right,
147+
Gt => left > right,
148+
Ge => left >= right,
149+
_ => bug!(),
150+
};
151+
(ImmTy::from_bool(res, *ecx.tcx), false)
152+
}
153+
154+
// Some more operations are possible with atomics.
155+
// The return value always has the provenance of the *left* operand.
156+
Add | Sub | BitOr | BitAnd | BitXor => {
157+
throw_machine_stop_str!("pointer arithmetic is not handled")
158+
}
159+
160+
_ => span_bug!(ecx.cur_span(), "Invalid operator on pointers: {:?}", bin_op),
161+
})
162+
}
163+
164+
fn expose_ptr(
165+
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
166+
_ptr: interpret::Pointer<Self::Provenance>,
167+
) -> interpret::InterpResult<'tcx> {
168+
unimplemented!()
169+
}
170+
171+
fn init_frame_extra(
172+
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
173+
_frame: interpret::Frame<'mir, 'tcx, Self::Provenance>,
174+
) -> interpret::InterpResult<
175+
'tcx,
176+
interpret::Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>,
177+
> {
178+
unimplemented!()
179+
}
180+
181+
fn stack<'a>(
182+
_ecx: &'a InterpCx<'mir, 'tcx, Self>,
183+
) -> &'a [interpret::Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>] {
184+
// Return an empty stack instead of panicking, as `cur_span` uses it to evaluate constants.
185+
&[]
186+
}
187+
188+
fn stack_mut<'a>(
189+
_ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
190+
) -> &'a mut Vec<interpret::Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>> {
191+
unimplemented!()
192+
}
193+
}

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use either::{Left, Right};
33
use rustc_hir::def::DefKind;
44
use rustc_middle::mir::interpret::{AllocId, ErrorHandled, InterpErrorInfo};
55
use rustc_middle::mir::{self, ConstAlloc, ConstValue};
6-
use rustc_middle::query::TyCtxtAt;
6+
use rustc_middle::query::{Key, TyCtxtAt};
77
use rustc_middle::traits::Reveal;
88
use rustc_middle::ty::layout::LayoutOf;
99
use rustc_middle::ty::print::with_no_trimmed_paths;
@@ -243,6 +243,24 @@ pub(crate) fn turn_into_const_value<'tcx>(
243243
op_to_const(&ecx, &mplace.into(), /* for diagnostics */ false)
244244
}
245245

246+
/// Computes the tag (if any) for a given type and variant.
247+
#[instrument(skip(tcx), level = "debug")]
248+
pub fn tag_for_variant_provider<'tcx>(
249+
tcx: TyCtxt<'tcx>,
250+
(ty, variant_index): (Ty<'tcx>, abi::VariantIdx),
251+
) -> Option<ty::ScalarInt> {
252+
assert!(ty.is_enum());
253+
254+
let ecx = InterpCx::new(
255+
tcx,
256+
ty.default_span(tcx),
257+
ty::ParamEnv::reveal_all(),
258+
crate::const_eval::DummyMachine,
259+
);
260+
261+
ecx.tag_for_variant(ty, variant_index).unwrap().map(|(tag, _tag_field)| tag)
262+
}
263+
246264
#[instrument(skip(tcx), level = "debug")]
247265
pub fn eval_to_const_value_raw_provider<'tcx>(
248266
tcx: TyCtxt<'tcx>,

compiler/rustc_const_eval/src/const_eval/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ use rustc_middle::ty::{self, Ty};
77

88
use crate::interpret::format_interp_error;
99

10+
mod dummy_machine;
1011
mod error;
1112
mod eval_queries;
1213
mod fn_queries;
1314
mod machine;
1415
mod valtrees;
1516

17+
pub use dummy_machine::*;
1618
pub use error::*;
1719
pub use eval_queries::*;
1820
pub use fn_queries::*;

0 commit comments

Comments
 (0)