Skip to content

Commit c3774be

Browse files
committed
Auto merge of rust-lang#127197 - matthiaskrgr:rollup-aqpvn5q, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - rust-lang#126923 (test: dont optimize to invalid bitcasts) - rust-lang#127090 (Reduce merge conflicts from rustfmt's wrapping) - rust-lang#127105 (Only update `Eq` operands in GVN if it can update both sides) - rust-lang#127150 (Fix x86_64 code being produced for bare-metal LoongArch targets' `compiler_builtins`) - rust-lang#127181 (Introduce a `rustc_` attribute to dump all the `DefId` parents of a `DefId`) - rust-lang#127182 (Fix error in documentation for IpAddr::to_canonical and Ipv6Addr::to_canonical) - rust-lang#127191 (Ensure `out_of_scope_macro_calls` lint is registered) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 7b21c18 + f5ef1cd commit c3774be

File tree

20 files changed

+436
-18
lines changed

20 files changed

+436
-18
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
11171117
TEST, rustc_dump_predicates, Normal, template!(Word),
11181118
WarnFollowing, EncodeCrossCrate::No
11191119
),
1120+
rustc_attr!(
1121+
TEST, rustc_dump_def_parents, Normal, template!(Word),
1122+
WarnFollowing, EncodeCrossCrate::No
1123+
),
11201124
rustc_attr!(
11211125
TEST, rustc_object_lifetime_default, Normal, template!(Word),
11221126
WarnFollowing, EncodeCrossCrate::No

compiler/rustc_hir_analysis/src/collect/dump.rs

+49-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use rustc_hir::def::DefKind;
2-
use rustc_hir::def_id::CRATE_DEF_ID;
2+
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
3+
use rustc_hir::intravisit;
4+
use rustc_middle::hir::nested_filter::OnlyBodies;
35
use rustc_middle::ty::TyCtxt;
46
use rustc_span::sym;
57

@@ -41,3 +43,49 @@ pub(crate) fn predicates_and_item_bounds(tcx: TyCtxt<'_>) {
4143
}
4244
}
4345
}
46+
47+
pub(crate) fn def_parents(tcx: TyCtxt<'_>) {
48+
for did in tcx.hir().body_owners() {
49+
if tcx.has_attr(did, sym::rustc_dump_def_parents) {
50+
struct AnonConstFinder<'tcx> {
51+
tcx: TyCtxt<'tcx>,
52+
anon_consts: Vec<LocalDefId>,
53+
}
54+
55+
impl<'tcx> intravisit::Visitor<'tcx> for AnonConstFinder<'tcx> {
56+
type NestedFilter = OnlyBodies;
57+
58+
fn nested_visit_map(&mut self) -> Self::Map {
59+
self.tcx.hir()
60+
}
61+
62+
fn visit_anon_const(&mut self, c: &'tcx rustc_hir::AnonConst) {
63+
self.anon_consts.push(c.def_id);
64+
intravisit::walk_anon_const(self, c)
65+
}
66+
}
67+
68+
// Look for any anon consts inside of this body owner as there is no way to apply
69+
// the `rustc_dump_def_parents` attribute to the anon const so it would not be possible
70+
// to see what its def parent is.
71+
let mut anon_ct_finder = AnonConstFinder { tcx, anon_consts: vec![] };
72+
intravisit::walk_expr(&mut anon_ct_finder, tcx.hir().body_owned_by(did).value);
73+
74+
for did in [did].into_iter().chain(anon_ct_finder.anon_consts) {
75+
let span = tcx.def_span(did);
76+
77+
let mut diag = tcx.dcx().struct_span_err(
78+
span,
79+
format!("{}: {did:?}", sym::rustc_dump_def_parents.as_str()),
80+
);
81+
82+
let mut current_did = did.to_def_id();
83+
while let Some(parent_did) = tcx.opt_parent(current_did) {
84+
current_did = parent_did;
85+
diag.span_note(tcx.def_span(parent_did), format!("{parent_did:?}"));
86+
}
87+
diag.emit();
88+
}
89+
}
90+
}
91+
}

compiler/rustc_hir_analysis/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
175175
tcx.sess.time("variance_dumping", || variance::dump::variances(tcx));
176176
collect::dump::opaque_hidden_types(tcx);
177177
collect::dump::predicates_and_item_bounds(tcx);
178+
collect::dump::def_parents(tcx);
178179
}
179180

180181
// Make sure we evaluate all static and (non-associated) const items, even if unused.

compiler/rustc_interface/src/tests.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22
use crate::interface::{initialize_checked_jobserver, parse_cfg};
33
use rustc_data_structures::profiling::TimePassesFormat;
44
use rustc_errors::{emitter::HumanReadableErrorType, registry, ColorConfig};
5+
use rustc_session::config::{build_configuration, build_session_options, rustc_optgroups};
56
use rustc_session::config::{
6-
build_configuration, build_session_options, rustc_optgroups, BranchProtection, CFGuard, Cfg,
7-
CollapseMacroDebuginfo, CoverageLevel, CoverageOptions, DebugInfo, DumpMonoStatsFormat,
8-
ErrorOutputType, ExternEntry, ExternLocation, Externs, FunctionReturn, InliningThreshold,
9-
Input, InstrumentCoverage, InstrumentXRay, LinkSelfContained, LinkerPluginLto, LocationDetail,
10-
LtoCli, NextSolverConfig, OomStrategy, Options, OutFileName, OutputType, OutputTypes, PAuthKey,
11-
PacRet, Passes, PatchableFunctionEntry, Polonius, ProcMacroExecutionStrategy, Strip,
12-
SwitchWithOptPath, SymbolManglingVersion, WasiExecModel,
7+
BranchProtection, CFGuard, Cfg, CollapseMacroDebuginfo, CoverageLevel, CoverageOptions,
8+
DebugInfo, DumpMonoStatsFormat, ErrorOutputType,
9+
};
10+
use rustc_session::config::{
11+
ExternEntry, ExternLocation, Externs, FunctionReturn, InliningThreshold, Input,
12+
InstrumentCoverage, InstrumentXRay, LinkSelfContained, LinkerPluginLto,
13+
};
14+
use rustc_session::config::{
15+
LocationDetail, LtoCli, NextSolverConfig, OomStrategy, Options, OutFileName, OutputType,
16+
OutputTypes, PAuthKey, PacRet, Passes, PatchableFunctionEntry,
17+
};
18+
use rustc_session::config::{
19+
Polonius, ProcMacroExecutionStrategy, Strip, SwitchWithOptPath, SymbolManglingVersion,
20+
WasiExecModel,
1321
};
1422
use rustc_session::lint::Level;
1523
use rustc_session::search_paths::SearchPath;

compiler/rustc_lint_defs/src/builtin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ declare_lint_pass! {
7474
NON_CONTIGUOUS_RANGE_ENDPOINTS,
7575
NON_EXHAUSTIVE_OMITTED_PATTERNS,
7676
ORDER_DEPENDENT_TRAIT_OBJECTS,
77+
OUT_OF_SCOPE_MACRO_CALLS,
7778
OVERLAPPING_RANGE_ENDPOINTS,
7879
PATTERNS_IN_FNS_WITHOUT_BODY,
7980
PRIVATE_BOUNDS,

compiler/rustc_mir_transform/src/gvn.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1074,11 +1074,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
10741074
{
10751075
lhs = *lhs_value;
10761076
rhs = *rhs_value;
1077-
if let Some(op) = self.try_as_operand(lhs, location) {
1078-
*lhs_operand = op;
1079-
}
1080-
if let Some(op) = self.try_as_operand(rhs, location) {
1081-
*rhs_operand = op;
1077+
if let Some(lhs_op) = self.try_as_operand(lhs, location)
1078+
&& let Some(rhs_op) = self.try_as_operand(rhs, location)
1079+
{
1080+
*lhs_operand = lhs_op;
1081+
*rhs_operand = rhs_op;
10821082
}
10831083
}
10841084

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,7 @@ symbols! {
16141614
rustc_do_not_const_check,
16151615
rustc_doc_primitive,
16161616
rustc_dummy,
1617+
rustc_dump_def_parents,
16171618
rustc_dump_item_bounds,
16181619
rustc_dump_predicates,
16191620
rustc_dump_user_args,

library/core/src/net/ip_addr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ impl IpAddr {
406406
matches!(self, IpAddr::V6(_))
407407
}
408408

409-
/// Converts this address to an `IpAddr::V4` if it is an IPv4-mapped IPv6 addresses, otherwise it
409+
/// Converts this address to an `IpAddr::V4` if it is an IPv4-mapped IPv6 address, otherwise it
410410
/// returns `self` as-is.
411411
///
412412
/// # Examples
@@ -1879,7 +1879,7 @@ impl Ipv6Addr {
18791879
}
18801880
}
18811881

1882-
/// Converts this address to an `IpAddr::V4` if it is an IPv4-mapped addresses, otherwise it
1882+
/// Converts this address to an `IpAddr::V4` if it is an IPv4-mapped address, otherwise it
18831883
/// returns self wrapped in an `IpAddr::V6`.
18841884
///
18851885
/// # Examples

src/ci/docker/host-x86_64/dist-loongarch64-linux/Dockerfile

+19-1
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,30 @@ ENV CC_loongarch64_unknown_linux_gnu=loongarch64-unknown-linux-gnu-gcc \
2323
AR_loongarch64_unknown_linux_gnu=loongarch64-unknown-linux-gnu-ar \
2424
CXX_loongarch64_unknown_linux_gnu=loongarch64-unknown-linux-gnu-g++
2525

26+
# We re-use the Linux toolchain for bare-metal, because upstream bare-metal
27+
# target support for LoongArch is only available from GCC 14+.
28+
#
29+
# See: https://github.com/gcc-mirror/gcc/commit/976f4f9e4770
30+
ENV CC_loongarch64_unknown_none=loongarch64-unknown-linux-gnu-gcc \
31+
AR_loongarch64_unknown_none=loongarch64-unknown-linux-gnu-ar \
32+
CXX_loongarch64_unknown_none=loongarch64-unknown-linux-gnu-g++ \
33+
CFLAGS_loongarch64_unknown_none="-ffreestanding -mabi=lp64d" \
34+
CXXFLAGS_loongarch64_unknown_none="-ffreestanding -mabi=lp64d" \
35+
CC_loongarch64_unknown_none_softfloat=loongarch64-unknown-linux-gnu-gcc \
36+
AR_loongarch64_unknown_none_softfloat=loongarch64-unknown-linux-gnu-ar \
37+
CXX_loongarch64_unknown_none_softfloat=loongarch64-unknown-linux-gnu-g++ \
38+
CFLAGS_loongarch64_unknown_none_softfloat="-ffreestanding -mabi=lp64s -mfpu=none" \
39+
CXXFLAGS_loongarch64_unknown_none_softfloat="-ffreestanding -mabi=lp64s -mfpu=none"
40+
2641
ENV HOSTS=loongarch64-unknown-linux-gnu
42+
ENV TARGETS=$HOSTS
43+
ENV TARGETS=$TARGETS,loongarch64-unknown-none
44+
ENV TARGETS=$TARGETS,loongarch64-unknown-none-softfloat
2745

2846
ENV RUST_CONFIGURE_ARGS \
2947
--enable-extended \
3048
--enable-full-tools \
3149
--enable-profiler \
3250
--disable-docs
3351

34-
ENV SCRIPT python3 ../x.py dist --host $HOSTS --target $HOSTS
52+
ENV SCRIPT python3 ../x.py dist --host $HOSTS --target $TARGETS

src/ci/docker/host-x86_64/dist-various-2/Dockerfile

-2
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ ENV TARGETS=$TARGETS,armv7-unknown-linux-gnueabi
121121
ENV TARGETS=$TARGETS,armv7-unknown-linux-musleabi
122122
ENV TARGETS=$TARGETS,i686-unknown-freebsd
123123
ENV TARGETS=$TARGETS,x86_64-unknown-none
124-
ENV TARGETS=$TARGETS,loongarch64-unknown-none
125-
ENV TARGETS=$TARGETS,loongarch64-unknown-none-softfloat
126124
ENV TARGETS=$TARGETS,aarch64-unknown-uefi
127125
ENV TARGETS=$TARGETS,i686-unknown-uefi
128126
ENV TARGETS=$TARGETS,x86_64-unknown-uefi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
- // MIR for `remove_casts_must_change_both_sides` before GVN
2+
+ // MIR for `remove_casts_must_change_both_sides` after GVN
3+
4+
fn remove_casts_must_change_both_sides(_1: &*mut u8, _2: *mut u8) -> bool {
5+
let mut _0: bool;
6+
let mut _3: *const u8;
7+
let mut _4: *const u8;
8+
9+
bb0: {
10+
_3 = (*_1) as *const u8 (PtrToPtr);
11+
_4 = _2 as *const u8 (PtrToPtr);
12+
_0 = Eq(_3, _4);
13+
return;
14+
}
15+
}
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
- // MIR for `remove_casts_must_change_both_sides` before GVN
2+
+ // MIR for `remove_casts_must_change_both_sides` after GVN
3+
4+
fn remove_casts_must_change_both_sides(_1: &*mut u8, _2: *mut u8) -> bool {
5+
let mut _0: bool;
6+
let mut _3: *const u8;
7+
let mut _4: *const u8;
8+
9+
bb0: {
10+
_3 = (*_1) as *const u8 (PtrToPtr);
11+
_4 = _2 as *const u8 (PtrToPtr);
12+
_0 = Eq(_3, _4);
13+
return;
14+
}
15+
}
16+

tests/mir-opt/gvn.rs

+20
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,25 @@ unsafe fn cast_pointer_then_transmute(thin: *mut u32, fat: *mut [u8]) {
926926
let fat_addr: usize = std::intrinsics::transmute(fat as *const ());
927927
}
928928

929+
#[custom_mir(dialect = "analysis")]
930+
fn remove_casts_must_change_both_sides(mut_a: &*mut u8, mut_b: *mut u8) -> bool {
931+
// CHECK-LABEL: fn remove_casts_must_change_both_sides(
932+
mir! {
933+
// We'd like to remove these casts, but we can't change *both* of them
934+
// to be locals, so make sure we don't change one without the other, as
935+
// that would be a type error.
936+
{
937+
// CHECK: [[A:_.+]] = (*_1) as *const u8 (PtrToPtr);
938+
let a = *mut_a as *const u8;
939+
// CHECK: [[B:_.+]] = _2 as *const u8 (PtrToPtr);
940+
let b = mut_b as *const u8;
941+
// CHECK: _0 = Eq([[A]], [[B]]);
942+
RET = a == b;
943+
Return()
944+
}
945+
}
946+
}
947+
929948
fn main() {
930949
subexpression_elimination(2, 4, 5);
931950
wrap_unwrap(5);
@@ -995,3 +1014,4 @@ fn identity<T>(x: T) -> T {
9951014
// EMIT_MIR gvn.generic_cast_metadata.GVN.diff
9961015
// EMIT_MIR gvn.cast_pointer_eq.GVN.diff
9971016
// EMIT_MIR gvn.cast_pointer_then_transmute.GVN.diff
1017+
// EMIT_MIR gvn.remove_casts_must_change_both_sides.GVN.diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
- // MIR for `main` before GVN
2+
+ // MIR for `main` after GVN
3+
4+
fn main() -> () {
5+
let mut _0: ();
6+
let _1: bool;
7+
let mut _2: *mut u8;
8+
scope 1 (inlined dangling_mut::<u8>) {
9+
let mut _3: usize;
10+
scope 2 (inlined align_of::<u8>) {
11+
}
12+
scope 3 (inlined without_provenance_mut::<u8>) {
13+
}
14+
}
15+
scope 4 (inlined Foo::<u8>::cmp_ptr) {
16+
let mut _4: *const u8;
17+
let mut _5: *mut u8;
18+
let mut _6: *const u8;
19+
scope 5 (inlined std::ptr::eq::<u8>) {
20+
}
21+
}
22+
23+
bb0: {
24+
StorageLive(_1);
25+
StorageLive(_2);
26+
StorageLive(_3);
27+
- _3 = AlignOf(u8);
28+
- _2 = _3 as *mut u8 (Transmute);
29+
+ _3 = const 1_usize;
30+
+ _2 = const {0x1 as *mut u8};
31+
StorageDead(_3);
32+
StorageLive(_4);
33+
StorageLive(_5);
34+
- _5 = _2;
35+
- _4 = _2 as *const u8 (PtrToPtr);
36+
+ _5 = const {0x1 as *mut u8};
37+
+ _4 = const {0x1 as *const u8};
38+
StorageDead(_5);
39+
StorageLive(_6);
40+
- _6 = const Foo::<u8>::SENTINEL as *const u8 (PtrToPtr);
41+
- _1 = Eq(_4, _6);
42+
+ _6 = const {0x1 as *const u8};
43+
+ _1 = const true;
44+
StorageDead(_6);
45+
StorageDead(_4);
46+
StorageDead(_2);
47+
StorageDead(_1);
48+
_0 = const ();
49+
return;
50+
}
51+
}
52+
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// skip-filecheck
2+
//@ test-mir-pass: GVN
3+
//@ only-64bit
4+
//@ compile-flags: -Z mir-enable-passes=+Inline
5+
6+
// Regression for <https://github.com/rust-lang/rust/issues/127089>
7+
8+
#![feature(strict_provenance)]
9+
10+
struct Foo<T>(std::marker::PhantomData<T>);
11+
12+
impl<T> Foo<T> {
13+
const SENTINEL: *mut T = std::ptr::dangling_mut();
14+
15+
fn cmp_ptr(a: *mut T) -> bool {
16+
std::ptr::eq(a, Self::SENTINEL)
17+
}
18+
}
19+
20+
// EMIT_MIR gvn_ptr_eq_with_constant.main.GVN.diff
21+
pub fn main() {
22+
Foo::<u8>::cmp_ptr(std::ptr::dangling_mut());
23+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//@ normalize-stderr-test "DefId\(.+?\)" -> "DefId(..)"
2+
#![feature(rustc_attrs)]
3+
4+
fn bar() {
5+
fn foo() {
6+
fn baz() {
7+
#[rustc_dump_def_parents]
8+
|| {
9+
//~^ ERROR: rustc_dump_def_parents: DefId
10+
qux::<
11+
{
12+
//~^ ERROR: rustc_dump_def_parents: DefId
13+
fn inhibits_dump() {
14+
qux::<
15+
{
16+
"hi";
17+
1
18+
},
19+
>();
20+
}
21+
22+
qux::<{ 1 + 1 }>();
23+
//~^ ERROR: rustc_dump_def_parents: DefId
24+
1
25+
},
26+
>();
27+
};
28+
}
29+
}
30+
}
31+
32+
const fn qux<const N: usize>() {}
33+
34+
fn main() {}

0 commit comments

Comments
 (0)