Skip to content

Commit 7a809ce

Browse files
committed
Auto merge of rust-lang#108677 - matthiaskrgr:rollup-i91cxuf, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#108022 (Support allocations with non-Box<[u8]> bytes) - rust-lang#108367 (Re-apply "switch to the macos-12-xl builder") - rust-lang#108557 (Point error span at Some constructor argument when trait resolution fails) - rust-lang#108573 (Explain compile-time vs run-time difference in env!() error message) - rust-lang#108584 (Put backtick content from rustdoc search errors into a `<code>` elements) - rust-lang#108624 (Make `ExprKind` the first field in `thir::Expr`) - rust-lang#108644 (Allow setting hashmap toml values in `./configure`) - rust-lang#108672 (Feed queries on impl side for RPITITs when using lower_impl_trait_in_trait_to_assoc_ty) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 13471d3 + e85df8d commit 7a809ce

File tree

32 files changed

+884
-145
lines changed

32 files changed

+884
-145
lines changed

.github/workflows/ci.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ jobs:
322322
NO_DEBUG_ASSERTIONS: 1
323323
NO_OVERFLOW_CHECKS: 1
324324
DIST_REQUIRE_ALL_TOOLS: 1
325-
os: macos-latest
325+
os: macos-12-xl
326326
- name: dist-apple-various
327327
env:
328328
SCRIPT: "./x.py dist bootstrap --include-default-paths --host='' --target=aarch64-apple-ios,x86_64-apple-ios,aarch64-apple-ios-sim"
@@ -333,7 +333,7 @@ jobs:
333333
NO_LLVM_ASSERTIONS: 1
334334
NO_DEBUG_ASSERTIONS: 1
335335
NO_OVERFLOW_CHECKS: 1
336-
os: macos-latest
336+
os: macos-12-xl
337337
- name: dist-x86_64-apple-alt
338338
env:
339339
SCRIPT: "./x.py dist bootstrap --include-default-paths"
@@ -344,7 +344,7 @@ jobs:
344344
NO_LLVM_ASSERTIONS: 1
345345
NO_DEBUG_ASSERTIONS: 1
346346
NO_OVERFLOW_CHECKS: 1
347-
os: macos-latest
347+
os: macos-12-xl
348348
- name: x86_64-apple-1
349349
env:
350350
SCRIPT: "./x.py --stage 2 test --exclude tests/ui --exclude tests/rustdoc --exclude tests/run-make-fulldeps"
@@ -355,7 +355,7 @@ jobs:
355355
NO_LLVM_ASSERTIONS: 1
356356
NO_DEBUG_ASSERTIONS: 1
357357
NO_OVERFLOW_CHECKS: 1
358-
os: macos-latest
358+
os: macos-12-xl
359359
- name: x86_64-apple-2
360360
env:
361361
SCRIPT: "./x.py --stage 2 test tests/ui tests/rustdoc tests/run-make-fulldeps"
@@ -366,7 +366,7 @@ jobs:
366366
NO_LLVM_ASSERTIONS: 1
367367
NO_DEBUG_ASSERTIONS: 1
368368
NO_OVERFLOW_CHECKS: 1
369-
os: macos-latest
369+
os: macos-12-xl
370370
- name: dist-aarch64-apple
371371
env:
372372
SCRIPT: "./x.py dist bootstrap --include-default-paths --stage 2"
@@ -381,7 +381,7 @@ jobs:
381381
NO_OVERFLOW_CHECKS: 1
382382
DIST_REQUIRE_ALL_TOOLS: 1
383383
JEMALLOC_SYS_WITH_LG_PAGE: 14
384-
os: macos-latest
384+
os: macos-12-xl
385385
- name: x86_64-msvc-1
386386
env:
387387
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-msvc --enable-profiler"

compiler/rustc_builtin_macros/src/env.rs

+30-10
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn expand_env<'cx>(
5353
tts: TokenStream,
5454
) -> Box<dyn base::MacResult + 'cx> {
5555
let mut exprs = match get_exprs_from_tts(cx, tts) {
56-
Some(exprs) if exprs.is_empty() => {
56+
Some(exprs) if exprs.is_empty() || exprs.len() > 2 => {
5757
cx.span_err(sp, "env! takes 1 or 2 arguments");
5858
return DummyResult::any(sp);
5959
}
@@ -64,28 +64,48 @@ pub fn expand_env<'cx>(
6464
let Some((var, _style)) = expr_to_string(cx, exprs.next().unwrap(), "expected string literal") else {
6565
return DummyResult::any(sp);
6666
};
67-
let msg = match exprs.next() {
68-
None => Symbol::intern(&format!("environment variable `{}` not defined", var)),
67+
68+
let custom_msg = match exprs.next() {
69+
None => None,
6970
Some(second) => match expr_to_string(cx, second, "expected string literal") {
7071
None => return DummyResult::any(sp),
71-
Some((s, _style)) => s,
72+
Some((s, _style)) => Some(s),
7273
},
7374
};
7475

75-
if exprs.next().is_some() {
76-
cx.span_err(sp, "env! takes 1 or 2 arguments");
77-
return DummyResult::any(sp);
78-
}
79-
8076
let sp = cx.with_def_site_ctxt(sp);
8177
let value = env::var(var.as_str()).ok().as_deref().map(Symbol::intern);
8278
cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value));
8379
let e = match value {
8480
None => {
85-
cx.span_err(sp, msg.as_str());
81+
let (msg, help) = match custom_msg {
82+
None => (
83+
format!("environment variable `{var}` not defined at compile time"),
84+
Some(help_for_missing_env_var(var.as_str())),
85+
),
86+
Some(s) => (s.to_string(), None),
87+
};
88+
let mut diag = cx.struct_span_err(sp, &msg);
89+
if let Some(help) = help {
90+
diag.help(help);
91+
}
92+
diag.emit();
8693
return DummyResult::any(sp);
8794
}
8895
Some(value) => cx.expr_str(sp, value),
8996
};
9097
MacEager::expr(e)
9198
}
99+
100+
fn help_for_missing_env_var(var: &str) -> String {
101+
if var.starts_with("CARGO_")
102+
|| var.starts_with("DEP_")
103+
|| matches!(var, "OUT_DIR" | "OPT_LEVEL" | "PROFILE" | "HOST" | "TARGET")
104+
{
105+
format!(
106+
"Cargo sets build script variables at run time. Use `std::env::var(\"{var}\")` instead"
107+
)
108+
} else {
109+
format!("Use `std::env::var(\"{var}\")` to read the variable at run time")
110+
}
111+
}

compiler/rustc_const_eval/src/interpret/machine.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use rustc_target::spec::abi::Abi as CallAbi;
1616
use crate::const_eval::CheckAlignment;
1717

1818
use super::{
19-
AllocId, AllocRange, Allocation, ConstAllocation, Frame, ImmTy, InterpCx, InterpResult,
20-
MemoryKind, OpTy, Operand, PlaceTy, Pointer, Provenance, Scalar, StackPopUnwind,
19+
AllocBytes, AllocId, AllocRange, Allocation, ConstAllocation, Frame, ImmTy, InterpCx,
20+
InterpResult, MemoryKind, OpTy, Operand, PlaceTy, Pointer, Provenance, Scalar, StackPopUnwind,
2121
};
2222

2323
/// Data returned by Machine::stack_pop,
@@ -105,10 +105,16 @@ pub trait Machine<'mir, 'tcx>: Sized {
105105
/// Extra data stored in every allocation.
106106
type AllocExtra: Debug + Clone + 'static;
107107

108+
/// Type for the bytes of the allocation.
109+
type Bytes: AllocBytes + 'static;
110+
108111
/// Memory's allocation map
109112
type MemoryMap: AllocMap<
110113
AllocId,
111-
(MemoryKind<Self::MemoryKind>, Allocation<Self::Provenance, Self::AllocExtra>),
114+
(
115+
MemoryKind<Self::MemoryKind>,
116+
Allocation<Self::Provenance, Self::AllocExtra, Self::Bytes>,
117+
),
112118
> + Default
113119
+ Clone;
114120

@@ -338,7 +344,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
338344
id: AllocId,
339345
alloc: Cow<'b, Allocation>,
340346
kind: Option<MemoryKind<Self::MemoryKind>>,
341-
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra>>>;
347+
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra, Self::Bytes>>>;
342348

343349
fn eval_inline_asm(
344350
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
@@ -459,6 +465,7 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
459465

460466
type AllocExtra = ();
461467
type FrameExtra = ();
468+
type Bytes = Box<[u8]>;
462469

463470
#[inline(always)]
464471
fn use_addr_for_alignment_check(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {

compiler/rustc_const_eval/src/interpret/memory.rs

+31-15
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ use rustc_target::abi::{Align, HasDataLayout, Size};
2121
use crate::const_eval::CheckAlignment;
2222

2323
use super::{
24-
alloc_range, AllocId, AllocMap, AllocRange, Allocation, CheckInAllocMsg, GlobalAlloc, InterpCx,
25-
InterpResult, Machine, MayLeak, Pointer, PointerArithmetic, Provenance, Scalar,
24+
alloc_range, AllocBytes, AllocId, AllocMap, AllocRange, Allocation, CheckInAllocMsg,
25+
GlobalAlloc, InterpCx, InterpResult, Machine, MayLeak, Pointer, PointerArithmetic, Provenance,
26+
Scalar,
2627
};
2728

2829
#[derive(Debug, PartialEq, Copy, Clone)]
@@ -114,16 +115,16 @@ pub struct Memory<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
114115
/// A reference to some allocation that was already bounds-checked for the given region
115116
/// and had the on-access machine hooks run.
116117
#[derive(Copy, Clone)]
117-
pub struct AllocRef<'a, 'tcx, Prov: Provenance, Extra> {
118-
alloc: &'a Allocation<Prov, Extra>,
118+
pub struct AllocRef<'a, 'tcx, Prov: Provenance, Extra, Bytes: AllocBytes = Box<[u8]>> {
119+
alloc: &'a Allocation<Prov, Extra, Bytes>,
119120
range: AllocRange,
120121
tcx: TyCtxt<'tcx>,
121122
alloc_id: AllocId,
122123
}
123124
/// A reference to some allocation that was already bounds-checked for the given region
124125
/// and had the on-access machine hooks run.
125-
pub struct AllocRefMut<'a, 'tcx, Prov: Provenance, Extra> {
126-
alloc: &'a mut Allocation<Prov, Extra>,
126+
pub struct AllocRefMut<'a, 'tcx, Prov: Provenance, Extra, Bytes: AllocBytes = Box<[u8]>> {
127+
alloc: &'a mut Allocation<Prov, Extra, Bytes>,
127128
range: AllocRange,
128129
tcx: TyCtxt<'tcx>,
129130
alloc_id: AllocId,
@@ -483,7 +484,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
483484
&self,
484485
id: AllocId,
485486
is_write: bool,
486-
) -> InterpResult<'tcx, Cow<'tcx, Allocation<M::Provenance, M::AllocExtra>>> {
487+
) -> InterpResult<'tcx, Cow<'tcx, Allocation<M::Provenance, M::AllocExtra, M::Bytes>>> {
487488
let (alloc, def_id) = match self.tcx.try_get_global_alloc(id) {
488489
Some(GlobalAlloc::Memory(mem)) => {
489490
// Memory of a constant or promoted or anonymous memory referenced by a static.
@@ -526,14 +527,25 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
526527
)
527528
}
528529

530+
/// Get the base address for the bytes in an `Allocation` specified by the
531+
/// `AllocID` passed in; error if no such allocation exists.
532+
///
533+
/// It is up to the caller to take sufficient care when using this address:
534+
/// there could be provenance or uninit memory in there, and other memory
535+
/// accesses could invalidate the exposed pointer.
536+
pub fn alloc_base_addr(&self, id: AllocId) -> InterpResult<'tcx, *const u8> {
537+
let alloc = self.get_alloc_raw(id)?;
538+
Ok(alloc.base_addr())
539+
}
540+
529541
/// Gives raw access to the `Allocation`, without bounds or alignment checks.
530542
/// The caller is responsible for calling the access hooks!
531543
///
532544
/// You almost certainly want to use `get_ptr_alloc`/`get_ptr_alloc_mut` instead.
533545
fn get_alloc_raw(
534546
&self,
535547
id: AllocId,
536-
) -> InterpResult<'tcx, &Allocation<M::Provenance, M::AllocExtra>> {
548+
) -> InterpResult<'tcx, &Allocation<M::Provenance, M::AllocExtra, M::Bytes>> {
537549
// The error type of the inner closure here is somewhat funny. We have two
538550
// ways of "erroring": An actual error, or because we got a reference from
539551
// `get_global_alloc` that we can actually use directly without inserting anything anywhere.
@@ -569,7 +581,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
569581
ptr: Pointer<Option<M::Provenance>>,
570582
size: Size,
571583
align: Align,
572-
) -> InterpResult<'tcx, Option<AllocRef<'a, 'tcx, M::Provenance, M::AllocExtra>>> {
584+
) -> InterpResult<'tcx, Option<AllocRef<'a, 'tcx, M::Provenance, M::AllocExtra, M::Bytes>>>
585+
{
573586
let ptr_and_alloc = self.check_and_deref_ptr(
574587
ptr,
575588
size,
@@ -612,7 +625,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
612625
fn get_alloc_raw_mut(
613626
&mut self,
614627
id: AllocId,
615-
) -> InterpResult<'tcx, (&mut Allocation<M::Provenance, M::AllocExtra>, &mut M)> {
628+
) -> InterpResult<'tcx, (&mut Allocation<M::Provenance, M::AllocExtra, M::Bytes>, &mut M)> {
616629
// We have "NLL problem case #3" here, which cannot be worked around without loss of
617630
// efficiency even for the common case where the key is in the map.
618631
// <https://rust-lang.github.io/rfcs/2094-nll.html#problem-case-3-conditional-control-flow-across-functions>
@@ -641,7 +654,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
641654
ptr: Pointer<Option<M::Provenance>>,
642655
size: Size,
643656
align: Align,
644-
) -> InterpResult<'tcx, Option<AllocRefMut<'a, 'tcx, M::Provenance, M::AllocExtra>>> {
657+
) -> InterpResult<'tcx, Option<AllocRefMut<'a, 'tcx, M::Provenance, M::AllocExtra, M::Bytes>>>
658+
{
645659
let parts = self.get_ptr_access(ptr, size, align)?;
646660
if let Some((alloc_id, offset, prov)) = parts {
647661
let tcx = *self.tcx;
@@ -840,11 +854,11 @@ pub struct DumpAllocs<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> {
840854
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> std::fmt::Debug for DumpAllocs<'a, 'mir, 'tcx, M> {
841855
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
842856
// Cannot be a closure because it is generic in `Prov`, `Extra`.
843-
fn write_allocation_track_relocs<'tcx, Prov: Provenance, Extra>(
857+
fn write_allocation_track_relocs<'tcx, Prov: Provenance, Extra, Bytes: AllocBytes>(
844858
fmt: &mut std::fmt::Formatter<'_>,
845859
tcx: TyCtxt<'tcx>,
846860
allocs_to_print: &mut VecDeque<AllocId>,
847-
alloc: &Allocation<Prov, Extra>,
861+
alloc: &Allocation<Prov, Extra, Bytes>,
848862
) -> std::fmt::Result {
849863
for alloc_id in alloc.provenance().provenances().filter_map(|prov| prov.get_alloc_id())
850864
{
@@ -912,7 +926,9 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> std::fmt::Debug for DumpAllocs<'a,
912926
}
913927

914928
/// Reading and writing.
915-
impl<'tcx, 'a, Prov: Provenance, Extra> AllocRefMut<'a, 'tcx, Prov, Extra> {
929+
impl<'tcx, 'a, Prov: Provenance, Extra, Bytes: AllocBytes>
930+
AllocRefMut<'a, 'tcx, Prov, Extra, Bytes>
931+
{
916932
/// `range` is relative to this allocation reference, not the base of the allocation.
917933
pub fn write_scalar(&mut self, range: AllocRange, val: Scalar<Prov>) -> InterpResult<'tcx> {
918934
let range = self.range.subrange(range);
@@ -937,7 +953,7 @@ impl<'tcx, 'a, Prov: Provenance, Extra> AllocRefMut<'a, 'tcx, Prov, Extra> {
937953
}
938954
}
939955

940-
impl<'tcx, 'a, Prov: Provenance, Extra> AllocRef<'a, 'tcx, Prov, Extra> {
956+
impl<'tcx, 'a, Prov: Provenance, Extra, Bytes: AllocBytes> AllocRef<'a, 'tcx, Prov, Extra, Bytes> {
941957
/// `range` is relative to this allocation reference, not the base of the allocation.
942958
pub fn read_scalar(
943959
&self,

compiler/rustc_const_eval/src/interpret/place.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ where
353353
pub(super) fn get_place_alloc(
354354
&self,
355355
place: &MPlaceTy<'tcx, M::Provenance>,
356-
) -> InterpResult<'tcx, Option<AllocRef<'_, 'tcx, M::Provenance, M::AllocExtra>>> {
356+
) -> InterpResult<'tcx, Option<AllocRef<'_, 'tcx, M::Provenance, M::AllocExtra, M::Bytes>>>
357+
{
357358
assert!(place.layout.is_sized());
358359
assert!(!place.meta.has_meta());
359360
let size = place.layout.size;
@@ -364,7 +365,8 @@ where
364365
pub(super) fn get_place_alloc_mut(
365366
&mut self,
366367
place: &MPlaceTy<'tcx, M::Provenance>,
367-
) -> InterpResult<'tcx, Option<AllocRefMut<'_, 'tcx, M::Provenance, M::AllocExtra>>> {
368+
) -> InterpResult<'tcx, Option<AllocRefMut<'_, 'tcx, M::Provenance, M::AllocExtra, M::Bytes>>>
369+
{
368370
assert!(place.layout.is_sized());
369371
assert!(!place.meta.has_meta());
370372
let size = place.layout.size;

compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -714,12 +714,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
714714
self.tcx.parent(expr_ctor_def_id)
715715
}
716716
hir::def::DefKind::Ctor(hir::def::CtorOf::Variant, hir::def::CtorKind::Fn) => {
717-
// If this is a variant, its parent is the type definition.
718-
if in_ty_adt.did() != self.tcx.parent(expr_ctor_def_id) {
717+
// For a typical enum like
718+
// `enum Blah<T> { Variant(T) }`
719+
// we get the following resolutions:
720+
// - expr_ctor_def_id ::: DefId(0:29 ~ source_file[b442]::Blah::Variant::{constructor#0})
721+
// - self.tcx.parent(expr_ctor_def_id) ::: DefId(0:28 ~ source_file[b442]::Blah::Variant)
722+
// - self.tcx.parent(self.tcx.parent(expr_ctor_def_id)) ::: DefId(0:26 ~ source_file[b442]::Blah)
723+
724+
// Therefore, we need to go up once to obtain the variant and up twice to obtain the type.
725+
// Note that this pattern still holds even when we `use` a variant or `use` an enum type to rename it, or chain `use` expressions
726+
// together; this resolution is handled automatically by `qpath_res`.
727+
728+
// FIXME: Deal with type aliases?
729+
if in_ty_adt.did() == self.tcx.parent(self.tcx.parent(expr_ctor_def_id)) {
730+
// The constructor definition refers to the "constructor" of the variant:
731+
// For example, `Some(5)` triggers this case.
732+
self.tcx.parent(expr_ctor_def_id)
733+
} else {
719734
// FIXME: Deal with type aliases?
720735
return Err(expr);
721736
}
722-
expr_ctor_def_id
723737
}
724738
_ => {
725739
return Err(expr);

0 commit comments

Comments
 (0)