Skip to content

Commit 558d253

Browse files
committed
Auto merge of #143667 - tgross35:rollup-yqitltm, r=tgross35
Rollup of 9 pull requests Successful merges: - #142357 (Simplify LLVM bitcode linker in bootstrap and add tests for it) - #143177 (Remove false label when `self` resolve failure does not relate to macro) - #143339 (Respect endianness correctly in CheckEnums test suite) - #143426 (clippy fix: indentation) - #143475 (tests: Use `cfg_target_has_reliable_f16_f128` in `conv-bits-runtime-const`) - #143499 (Don't call `predicates_of` on a dummy obligation cause's body id) - #143520 (Fix perf regression caused by tracing) - #143532 (More carefully consider span context when suggesting remove `&mut`) - #143606 (configure.py: Write last key in each section) r? `@ghost` `@rustbot` modify labels: rollup
2 parents d350797 + 45b63a4 commit 558d253

Some content is hidden

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

48 files changed

+347
-169
lines changed

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
9696
/// This inherent method takes priority over the trait method with the same name in LayoutOf,
9797
/// and allows wrapping the actual [LayoutOf::layout_of] with a tracing span.
9898
/// See [LayoutOf::layout_of] for the original documentation.
99-
#[inline]
99+
#[inline(always)]
100100
pub fn layout_of(
101101
&self,
102102
ty: Ty<'tcx>,

compiler/rustc_const_eval/src/interpret/machine.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use rustc_target::callconv::FnAbi;
1818

1919
use super::{
2020
AllocBytes, AllocId, AllocKind, AllocRange, Allocation, CTFE_ALLOC_SALT, ConstAllocation,
21-
CtfeProvenance, FnArg, Frame, ImmTy, InterpCx, InterpResult, MPlaceTy, MemoryKind,
22-
Misalignment, OpTy, PlaceTy, Pointer, Provenance, RangeSet, interp_ok, throw_unsup,
21+
CtfeProvenance, EnteredTraceSpan, FnArg, Frame, ImmTy, InterpCx, InterpResult, MPlaceTy,
22+
MemoryKind, Misalignment, OpTy, PlaceTy, Pointer, Provenance, RangeSet, interp_ok, throw_unsup,
2323
};
2424

2525
/// Data returned by [`Machine::after_stack_pop`], and consumed by
@@ -147,12 +147,6 @@ pub trait Machine<'tcx>: Sized {
147147
/// already been checked before.
148148
const ALL_CONSTS_ARE_PRECHECKED: bool = true;
149149

150-
/// Determines whether rustc_const_eval functions that make use of the [Machine] should make
151-
/// tracing calls (to the `tracing` library). By default this is `false`, meaning the tracing
152-
/// calls will supposedly be optimized out. This flag is set to `true` inside Miri, to allow
153-
/// tracing the interpretation steps, among other things.
154-
const TRACING_ENABLED: bool = false;
155-
156150
/// Whether memory accesses should be alignment-checked.
157151
fn enforce_alignment(ecx: &InterpCx<'tcx, Self>) -> bool;
158152

@@ -634,6 +628,17 @@ pub trait Machine<'tcx>: Sized {
634628
/// Compute the value passed to the constructors of the `AllocBytes` type for
635629
/// abstract machine allocations.
636630
fn get_default_alloc_params(&self) -> <Self::Bytes as AllocBytes>::AllocParams;
631+
632+
/// Allows enabling/disabling tracing calls from within `rustc_const_eval` at compile time, by
633+
/// delegating the entering of [tracing::Span]s to implementors of the [Machine] trait. The
634+
/// default implementation corresponds to tracing being disabled, meaning the tracing calls will
635+
/// supposedly be optimized out completely. To enable tracing, override this trait method and
636+
/// return `span.entered()`. Also see [crate::enter_trace_span].
637+
#[must_use]
638+
#[inline(always)]
639+
fn enter_trace_span(_span: impl FnOnce() -> tracing::Span) -> impl EnteredTraceSpan {
640+
()
641+
}
637642
}
638643

639644
/// A lot of the flexibility above is just needed for `Miri`, but all "compile-time" machines

compiler/rustc_const_eval/src/interpret/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub use self::place::{MPlaceTy, MemPlaceMeta, PlaceTy, Writeable};
3737
use self::place::{MemPlace, Place};
3838
pub use self::projection::{OffsetMode, Projectable};
3939
pub use self::stack::{Frame, FrameInfo, LocalState, ReturnContinuation, StackPopInfo};
40+
pub use self::util::EnteredTraceSpan;
4041
pub(crate) use self::util::create_static_alloc;
4142
pub use self::validity::{CtfeValidationMode, RangeSet, RefTracking};
4243
pub use self::visitor::ValueVisitor;

compiler/rustc_const_eval/src/interpret/util.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,20 @@ pub(crate) fn create_static_alloc<'tcx>(
4646
interp_ok(ecx.ptr_to_mplace(Pointer::from(alloc_id).into(), layout))
4747
}
4848

49-
/// This struct is needed to enforce `#[must_use]` on [tracing::span::EnteredSpan]
50-
/// while wrapping them in an `Option`.
51-
#[must_use]
52-
pub enum MaybeEnteredSpan {
53-
Some(tracing::span::EnteredSpan),
54-
None,
55-
}
49+
/// A marker trait returned by [crate::interpret::Machine::enter_trace_span], identifying either a
50+
/// real [tracing::span::EnteredSpan] in case tracing is enabled, or the dummy type `()` when
51+
/// tracing is disabled.
52+
pub trait EnteredTraceSpan {}
53+
impl EnteredTraceSpan for () {}
54+
impl EnteredTraceSpan for tracing::span::EnteredSpan {}
5655

56+
/// Shortand for calling [crate::interpret::Machine::enter_trace_span] on a [tracing::info_span].
57+
/// This is supposed to be compiled out when [crate::interpret::Machine::enter_trace_span] has the
58+
/// default implementation (i.e. when it does not actually enter the span but instead returns `()`).
59+
/// Note: the result of this macro **must be used** because the span is exited when it's dropped.
5760
#[macro_export]
5861
macro_rules! enter_trace_span {
5962
($machine:ident, $($tt:tt)*) => {
60-
if $machine::TRACING_ENABLED {
61-
$crate::interpret::util::MaybeEnteredSpan::Some(tracing::info_span!($($tt)*).entered())
62-
} else {
63-
$crate::interpret::util::MaybeEnteredSpan::None
64-
}
63+
$machine::enter_trace_span(|| tracing::info_span!($($tt)*))
6564
}
6665
}

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,15 +1183,23 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
11831183
_ => "`self` value is a keyword only available in methods with a `self` parameter",
11841184
},
11851185
);
1186+
1187+
// using `let self` is wrong even if we're not in an associated method or if we're in a macro expansion.
1188+
// So, we should return early if we're in a pattern, see issue #143134.
1189+
if matches!(source, PathSource::Pat) {
1190+
return true;
1191+
}
1192+
11861193
let is_assoc_fn = self.self_type_is_available();
11871194
let self_from_macro = "a `self` parameter, but a macro invocation can only \
11881195
access identifiers it receives from parameters";
1189-
if let Some((fn_kind, span)) = &self.diag_metadata.current_function {
1196+
if let Some((fn_kind, fn_span)) = &self.diag_metadata.current_function {
11901197
// The current function has a `self` parameter, but we were unable to resolve
11911198
// a reference to `self`. This can only happen if the `self` identifier we
1192-
// are resolving came from a different hygiene context.
1199+
// are resolving came from a different hygiene context or a variable binding.
1200+
// But variable binding error is returned early above.
11931201
if fn_kind.decl().inputs.get(0).is_some_and(|p| p.is_self()) {
1194-
err.span_label(*span, format!("this function has {self_from_macro}"));
1202+
err.span_label(*fn_span, format!("this function has {self_from_macro}"));
11951203
} else {
11961204
let doesnt = if is_assoc_fn {
11971205
let (span, sugg) = fn_kind
@@ -1204,7 +1212,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
12041212
// This avoids placing the suggestion into the visibility specifier.
12051213
let span = fn_kind
12061214
.ident()
1207-
.map_or(*span, |ident| span.with_lo(ident.span.hi()));
1215+
.map_or(*fn_span, |ident| fn_span.with_lo(ident.span.hi()));
12081216
(
12091217
self.r
12101218
.tcx

compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_errors::{Applicability, Diag, E0283, E0284, E0790, MultiSpan, struct_s
44
use rustc_hir as hir;
55
use rustc_hir::LangItem;
66
use rustc_hir::def::{DefKind, Res};
7-
use rustc_hir::def_id::DefId;
7+
use rustc_hir::def_id::{CRATE_DEF_ID, DefId};
88
use rustc_hir::intravisit::Visitor as _;
99
use rustc_infer::infer::{BoundRegionConversionTime, InferCtxt};
1010
use rustc_infer::traits::util::elaborate;
@@ -128,19 +128,26 @@ pub fn compute_applicable_impls_for_diagnostics<'tcx>(
128128
},
129129
);
130130

131-
let predicates =
132-
tcx.predicates_of(obligation.cause.body_id.to_def_id()).instantiate_identity(tcx);
133-
for (pred, span) in elaborate(tcx, predicates.into_iter()) {
134-
let kind = pred.kind();
135-
if let ty::ClauseKind::Trait(trait_pred) = kind.skip_binder()
136-
&& param_env_candidate_may_apply(kind.rebind(trait_pred))
137-
{
138-
if kind.rebind(trait_pred.trait_ref)
139-
== ty::Binder::dummy(ty::TraitRef::identity(tcx, trait_pred.def_id()))
131+
// If our `body_id` has been set (and isn't just from a dummy obligation cause),
132+
// then try to look for a param-env clause that would apply. The way we compute
133+
// this is somewhat manual, since we need the spans, so we elaborate this directly
134+
// from `predicates_of` rather than actually looking at the param-env which
135+
// otherwise would be more appropriate.
136+
let body_id = obligation.cause.body_id;
137+
if body_id != CRATE_DEF_ID {
138+
let predicates = tcx.predicates_of(body_id.to_def_id()).instantiate_identity(tcx);
139+
for (pred, span) in elaborate(tcx, predicates.into_iter()) {
140+
let kind = pred.kind();
141+
if let ty::ClauseKind::Trait(trait_pred) = kind.skip_binder()
142+
&& param_env_candidate_may_apply(kind.rebind(trait_pred))
140143
{
141-
ambiguities.push(CandidateSource::ParamEnv(tcx.def_span(trait_pred.def_id())))
142-
} else {
143-
ambiguities.push(CandidateSource::ParamEnv(span))
144+
if kind.rebind(trait_pred.trait_ref)
145+
== ty::Binder::dummy(ty::TraitRef::identity(tcx, trait_pred.def_id()))
146+
{
147+
ambiguities.push(CandidateSource::ParamEnv(tcx.def_span(trait_pred.def_id())))
148+
} else {
149+
ambiguities.push(CandidateSource::ParamEnv(span))
150+
}
144151
}
145152
}
146153
}

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,12 +1581,15 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
15811581
'outer: loop {
15821582
while let hir::ExprKind::AddrOf(_, _, borrowed) = expr.kind {
15831583
count += 1;
1584-
let span = if expr.span.eq_ctxt(borrowed.span) {
1585-
expr.span.until(borrowed.span)
1586-
} else {
1587-
expr.span.with_hi(expr.span.lo() + BytePos(1))
1588-
};
1584+
let span =
1585+
if let Some(borrowed_span) = borrowed.span.find_ancestor_inside(expr.span) {
1586+
expr.span.until(borrowed_span)
1587+
} else {
1588+
break 'outer;
1589+
};
15891590

1591+
// Double check that the span we extracted actually corresponds to a borrow,
1592+
// rather than some macro garbage.
15901593
match self.tcx.sess.source_map().span_to_snippet(span) {
15911594
Ok(snippet) if snippet.starts_with("&") => {}
15921595
_ => break 'outer,

library/core/src/alloc/layout.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ impl Layout {
6161
/// * `align` must be a power of two,
6262
///
6363
/// * `size`, when rounded up to the nearest multiple of `align`,
64-
/// must not overflow `isize` (i.e., the rounded value must be
65-
/// less than or equal to `isize::MAX`).
64+
/// must not overflow `isize` (i.e., the rounded value must be
65+
/// less than or equal to `isize::MAX`).
6666
#[stable(feature = "alloc_layout", since = "1.28.0")]
6767
#[rustc_const_stable(feature = "const_alloc_layout_size_align", since = "1.50.0")]
6868
#[inline]

library/core/src/cell.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,32 +1940,32 @@ impl<T: ?Sized + fmt::Display> fmt::Display for RefMut<'_, T> {
19401940
/// The precise Rust aliasing rules are somewhat in flux, but the main points are not contentious:
19411941
///
19421942
/// - If you create a safe reference with lifetime `'a` (either a `&T` or `&mut T` reference), then
1943-
/// you must not access the data in any way that contradicts that reference for the remainder of
1944-
/// `'a`. For example, this means that if you take the `*mut T` from an `UnsafeCell<T>` and cast it
1945-
/// to an `&T`, then the data in `T` must remain immutable (modulo any `UnsafeCell` data found
1946-
/// within `T`, of course) until that reference's lifetime expires. Similarly, if you create a `&mut
1947-
/// T` reference that is released to safe code, then you must not access the data within the
1948-
/// `UnsafeCell` until that reference expires.
1943+
/// you must not access the data in any way that contradicts that reference for the remainder of
1944+
/// `'a`. For example, this means that if you take the `*mut T` from an `UnsafeCell<T>` and cast it
1945+
/// to an `&T`, then the data in `T` must remain immutable (modulo any `UnsafeCell` data found
1946+
/// within `T`, of course) until that reference's lifetime expires. Similarly, if you create a
1947+
/// `&mut T` reference that is released to safe code, then you must not access the data within the
1948+
/// `UnsafeCell` until that reference expires.
19491949
///
19501950
/// - For both `&T` without `UnsafeCell<_>` and `&mut T`, you must also not deallocate the data
1951-
/// until the reference expires. As a special exception, given an `&T`, any part of it that is
1952-
/// inside an `UnsafeCell<_>` may be deallocated during the lifetime of the reference, after the
1953-
/// last time the reference is used (dereferenced or reborrowed). Since you cannot deallocate a part
1954-
/// of what a reference points to, this means the memory an `&T` points to can be deallocated only if
1955-
/// *every part of it* (including padding) is inside an `UnsafeCell`.
1951+
/// until the reference expires. As a special exception, given an `&T`, any part of it that is
1952+
/// inside an `UnsafeCell<_>` may be deallocated during the lifetime of the reference, after the
1953+
/// last time the reference is used (dereferenced or reborrowed). Since you cannot deallocate a part
1954+
/// of what a reference points to, this means the memory an `&T` points to can be deallocated only if
1955+
/// *every part of it* (including padding) is inside an `UnsafeCell`.
19561956
///
1957-
/// However, whenever a `&UnsafeCell<T>` is constructed or dereferenced, it must still point to
1957+
/// However, whenever a `&UnsafeCell<T>` is constructed or dereferenced, it must still point to
19581958
/// live memory and the compiler is allowed to insert spurious reads if it can prove that this
19591959
/// memory has not yet been deallocated.
19601960
///
19611961
/// To assist with proper design, the following scenarios are explicitly declared legal
19621962
/// for single-threaded code:
19631963
///
19641964
/// 1. A `&T` reference can be released to safe code and there it can co-exist with other `&T`
1965-
/// references, but not with a `&mut T`
1965+
/// references, but not with a `&mut T`
19661966
///
19671967
/// 2. A `&mut T` reference may be released to safe code provided neither other `&mut T` nor `&T`
1968-
/// co-exist with it. A `&mut T` must always be unique.
1968+
/// co-exist with it. A `&mut T` must always be unique.
19691969
///
19701970
/// Note that whilst mutating the contents of an `&UnsafeCell<T>` (even while other
19711971
/// `&UnsafeCell<T>` references alias the cell) is

library/core/src/error.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -447,28 +447,28 @@ where
447447
/// separated by API boundaries:
448448
///
449449
/// * Consumer - the consumer requests objects using a Request instance; eg a crate that offers
450-
/// fancy `Error`/`Result` reporting to users wants to request a Backtrace from a given `dyn Error`.
450+
/// fancy `Error`/`Result` reporting to users wants to request a Backtrace from a given `dyn Error`.
451451
///
452452
/// * Producer - the producer provides objects when requested via Request; eg. a library with an
453-
/// an `Error` implementation that automatically captures backtraces at the time instances are
454-
/// created.
453+
/// an `Error` implementation that automatically captures backtraces at the time instances are
454+
/// created.
455455
///
456456
/// The consumer only needs to know where to submit their request and are expected to handle the
457457
/// request not being fulfilled by the use of `Option<T>` in the responses offered by the producer.
458458
///
459459
/// * A Producer initializes the value of one of its fields of a specific type. (or is otherwise
460-
/// prepared to generate a value requested). eg, `backtrace::Backtrace` or
461-
/// `std::backtrace::Backtrace`
460+
/// prepared to generate a value requested). eg, `backtrace::Backtrace` or
461+
/// `std::backtrace::Backtrace`
462462
/// * A Consumer requests an object of a specific type (say `std::backtrace::Backtrace`). In the
463-
/// case of a `dyn Error` trait object (the Producer), there are functions called `request_ref` and
464-
/// `request_value` to simplify obtaining an `Option<T>` for a given type.
463+
/// case of a `dyn Error` trait object (the Producer), there are functions called `request_ref` and
464+
/// `request_value` to simplify obtaining an `Option<T>` for a given type.
465465
/// * The Producer, when requested, populates the given Request object which is given as a mutable
466-
/// reference.
466+
/// reference.
467467
/// * The Consumer extracts a value or reference to the requested type from the `Request` object
468-
/// wrapped in an `Option<T>`; in the case of `dyn Error` the aforementioned `request_ref` and `
469-
/// request_value` methods mean that `dyn Error` users don't have to deal with the `Request` type at
470-
/// all (but `Error` implementors do). The `None` case of the `Option` suggests only that the
471-
/// Producer cannot currently offer an instance of the requested type, not it can't or never will.
468+
/// wrapped in an `Option<T>`; in the case of `dyn Error` the aforementioned `request_ref` and `
469+
/// request_value` methods mean that `dyn Error` users don't have to deal with the `Request` type at
470+
/// all (but `Error` implementors do). The `None` case of the `Option` suggests only that the
471+
/// Producer cannot currently offer an instance of the requested type, not it can't or never will.
472472
///
473473
/// # Examples
474474
///

0 commit comments

Comments
 (0)