Skip to content

Commit 5384697

Browse files
committed
Auto merge of #131158 - matthiaskrgr:rollup-3x2vado, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #130863 (Relax a debug assertion for dyn principal *equality* in codegen) - #131016 (Apple: Do not specify an SDK version in `rlib` object files) - #131140 (Handle `rustc_hir_analysis` cases of `potential_query_instability` lint) - #131141 (mpmc doctest: make sure main thread waits for child threads) - #131150 (only query `params_in_repr` if def kind is adt) - #131151 (Replace zero-width whitespace with a visible `\` in the PR template) - #131152 (Improve const traits diagnostics for new desugaring) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 44722bd + b38f7ad commit 5384697

Some content is hidden

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

50 files changed

+538
-279
lines changed

.github/pull_request_template.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ tracking issue or there are none, feel free to ignore this.
77
This PR will get automatically assigned to a reviewer. In case you would like
88
a specific user to review your work, you can assign it to them by using
99
10-
r? <reviewer name>
10+
r\? <reviewer name> (with the `\` removed)
1111
-->
1212
<!-- homu-ignore:end -->

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3451,6 +3451,7 @@ dependencies = [
34513451
"rustc_span",
34523452
"rustc_symbol_mangling",
34533453
"rustc_target",
3454+
"rustc_trait_selection",
34543455
"rustc_type_ir",
34553456
"serde_json",
34563457
"smallvec",

compiler/rustc_codegen_cranelift/src/unsize.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! [`PointerCoercion::Unsize`]: `rustc_middle::ty::adjustment::PointerCoercion::Unsize`
44
5+
use rustc_codegen_ssa::base::validate_trivial_unsize;
56
use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
67

78
use crate::base::codegen_panic_nounwind;
@@ -34,20 +35,8 @@ pub(crate) fn unsized_info<'tcx>(
3435
let old_info =
3536
old_info.expect("unsized_info: missing old info for trait upcasting coercion");
3637
if data_a.principal_def_id() == data_b.principal_def_id() {
37-
// Codegen takes advantage of the additional assumption, where if the
38-
// principal trait def id of what's being casted doesn't change,
39-
// then we don't need to adjust the vtable at all. This
40-
// corresponds to the fact that `dyn Tr<A>: Unsize<dyn Tr<B>>`
41-
// requires that `A = B`; we don't allow *upcasting* objects
42-
// between the same trait with different args. If we, for
43-
// some reason, were to relax the `Unsize` trait, it could become
44-
// unsound, so let's assert here that the trait refs are *equal*.
45-
//
46-
// We can use `assert_eq` because the binders should have been anonymized,
47-
// and because higher-ranked equality now requires the binders are equal.
48-
debug_assert_eq!(
49-
data_a.principal(),
50-
data_b.principal(),
38+
debug_assert!(
39+
validate_trivial_unsize(fx.tcx, data_a, data_b),
5140
"NOP unsize vtable changed principal trait ref: {data_a} -> {data_b}"
5241
);
5342
return old_info;

compiler/rustc_codegen_ssa/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ rustc_session = { path = "../rustc_session" }
3434
rustc_span = { path = "../rustc_span" }
3535
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
3636
rustc_target = { path = "../rustc_target" }
37+
rustc_trait_selection = { path = "../rustc_trait_selection" }
3738
rustc_type_ir = { path = "../rustc_type_ir" }
3839
serde_json = "1.0.59"
3940
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }

compiler/rustc_codegen_ssa/src/back/link.rs

+41-7
Original file line numberDiff line numberDiff line change
@@ -2959,11 +2959,12 @@ pub(crate) fn are_upstream_rust_objects_already_included(sess: &Session) -> bool
29592959
}
29602960
}
29612961

2962-
/// We need to communicate four things to the linker on Apple/Darwin targets:
2962+
/// We need to communicate five things to the linker on Apple/Darwin targets:
29632963
/// - The architecture.
29642964
/// - The operating system (and that it's an Apple platform).
2965-
/// - The deployment target.
29662965
/// - The environment / ABI.
2966+
/// - The deployment target.
2967+
/// - The SDK version.
29672968
fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
29682969
if !sess.target.is_like_osx {
29692970
return;
@@ -3039,7 +3040,38 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo
30393040
let (major, minor, patch) = current_apple_deployment_target(&sess.target);
30403041
let min_version = format!("{major}.{minor}.{patch}");
30413042

3042-
// Lie about the SDK version, we don't know it here
3043+
// The SDK version is used at runtime when compiling with a newer SDK / version of Xcode:
3044+
// - By dyld to give extra warnings and errors, see e.g.:
3045+
// <https://github.com/apple-oss-distributions/dyld/blob/dyld-1165.3/common/MachOFile.cpp#L3029>
3046+
// <https://github.com/apple-oss-distributions/dyld/blob/dyld-1165.3/common/MachOFile.cpp#L3738-L3857>
3047+
// - By system frameworks to change certain behaviour. For example, the default value of
3048+
// `-[NSView wantsBestResolutionOpenGLSurface]` is `YES` when the SDK version is >= 10.15.
3049+
// <https://developer.apple.com/documentation/appkit/nsview/1414938-wantsbestresolutionopenglsurface?language=objc>
3050+
//
3051+
// We do not currently know the actual SDK version though, so we have a few options:
3052+
// 1. Use the minimum version supported by rustc.
3053+
// 2. Use the same as the deployment target.
3054+
// 3. Use an arbitary recent version.
3055+
// 4. Omit the version.
3056+
//
3057+
// The first option is too low / too conservative, and means that users will not get the
3058+
// same behaviour from a binary compiled with rustc as with one compiled by clang.
3059+
//
3060+
// The second option is similarly conservative, and also wrong since if the user specified a
3061+
// higher deployment target than the SDK they're compiling/linking with, the runtime might
3062+
// make invalid assumptions about the capabilities of the binary.
3063+
//
3064+
// The third option requires that `rustc` is periodically kept up to date with Apple's SDK
3065+
// version, and is also wrong for similar reasons as above.
3066+
//
3067+
// The fourth option is bad because while `ld`, `otool`, `vtool` and such understand it to
3068+
// mean "absent" or `n/a`, dyld doesn't actually understand it, and will end up interpreting
3069+
// it as 0.0, which is again too low/conservative.
3070+
//
3071+
// Currently, we lie about the SDK version, and choose the second option.
3072+
//
3073+
// FIXME(madsmtm): Parse the SDK version from the SDK root instead.
3074+
// <https://github.com/rust-lang/rust/issues/129432>
30433075
let sdk_version = &*min_version;
30443076

30453077
// From the man page for ld64 (`man ld`):
@@ -3053,11 +3085,13 @@ fn add_apple_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavo
30533085
cmd.link_args(&["-platform_version", platform_name, &*min_version, sdk_version]);
30543086
} else {
30553087
// cc == Cc::Yes
3088+
//
30563089
// We'd _like_ to use `-target` everywhere, since that can uniquely
3057-
// communicate all the required details, but that doesn't work on GCC,
3058-
// and since we don't know whether the `cc` compiler is Clang, GCC, or
3059-
// something else, we fall back to other options that also work on GCC
3060-
// when compiling for macOS.
3090+
// communicate all the required details except for the SDK version
3091+
// (which is read by Clang itself from the SDKROOT), but that doesn't
3092+
// work on GCC, and since we don't know whether the `cc` compiler is
3093+
// Clang, GCC, or something else, we fall back to other options that
3094+
// also work on GCC when compiling for macOS.
30613095
//
30623096
// Targets other than macOS are ill-supported by GCC (it doesn't even
30633097
// support e.g. `-miphoneos-version-min`), so in those cases we can

compiler/rustc_codegen_ssa/src/back/metadata.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -402,13 +402,17 @@ fn macho_object_build_version_for_target(target: &Target) -> object::write::Mach
402402
let platform =
403403
rustc_target::spec::current_apple_platform(target).expect("unknown Apple target OS");
404404
let min_os = rustc_target::spec::current_apple_deployment_target(target);
405-
let (sdk_major, sdk_minor) =
406-
rustc_target::spec::current_apple_sdk_version(platform).expect("unknown Apple target OS");
407405

408406
let mut build_version = object::write::MachOBuildVersion::default();
409407
build_version.platform = platform;
410408
build_version.minos = pack_version(min_os);
411-
build_version.sdk = pack_version((sdk_major, sdk_minor, 0));
409+
// The version here does not _really_ matter, since it is only used at runtime, and we specify
410+
// it when linking the final binary, so we will omit the version. This is also what LLVM does,
411+
// and the tooling also allows this (and shows the SDK version as `n/a`). Finally, it is the
412+
// semantically correct choice, as the SDK has not influenced the binary generated by rustc at
413+
// this point in time.
414+
build_version.sdk = 0;
415+
412416
build_version
413417
}
414418

compiler/rustc_codegen_ssa/src/base.rs

+53-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ use rustc_session::config::{self, CrateType, EntryFnType, OptLevel, OutputType};
2727
use rustc_span::symbol::sym;
2828
use rustc_span::{DUMMY_SP, Symbol};
2929
use rustc_target::abi::FIRST_VARIANT;
30+
use rustc_trait_selection::infer::at::ToTrace;
31+
use rustc_trait_selection::infer::{BoundRegionConversionTime, TyCtxtInferExt};
32+
use rustc_trait_selection::traits::{ObligationCause, ObligationCtxt};
3033
use tracing::{debug, info};
3134

3235
use crate::assert_module_sources::CguReuse;
@@ -101,6 +104,54 @@ pub fn compare_simd_types<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
101104
bx.sext(cmp, ret_ty)
102105
}
103106

107+
/// Codegen takes advantage of the additional assumption, where if the
108+
/// principal trait def id of what's being casted doesn't change,
109+
/// then we don't need to adjust the vtable at all. This
110+
/// corresponds to the fact that `dyn Tr<A>: Unsize<dyn Tr<B>>`
111+
/// requires that `A = B`; we don't allow *upcasting* objects
112+
/// between the same trait with different args. If we, for
113+
/// some reason, were to relax the `Unsize` trait, it could become
114+
/// unsound, so let's validate here that the trait refs are subtypes.
115+
pub fn validate_trivial_unsize<'tcx>(
116+
tcx: TyCtxt<'tcx>,
117+
source_data: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
118+
target_data: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
119+
) -> bool {
120+
match (source_data.principal(), target_data.principal()) {
121+
(Some(hr_source_principal), Some(hr_target_principal)) => {
122+
let infcx = tcx.infer_ctxt().build();
123+
let universe = infcx.universe();
124+
let ocx = ObligationCtxt::new(&infcx);
125+
infcx.enter_forall(hr_target_principal, |target_principal| {
126+
let source_principal = infcx.instantiate_binder_with_fresh_vars(
127+
DUMMY_SP,
128+
BoundRegionConversionTime::HigherRankedType,
129+
hr_source_principal,
130+
);
131+
let Ok(()) = ocx.eq_trace(
132+
&ObligationCause::dummy(),
133+
ty::ParamEnv::reveal_all(),
134+
ToTrace::to_trace(
135+
&ObligationCause::dummy(),
136+
hr_target_principal,
137+
hr_source_principal,
138+
),
139+
target_principal,
140+
source_principal,
141+
) else {
142+
return false;
143+
};
144+
if !ocx.select_all_or_error().is_empty() {
145+
return false;
146+
}
147+
infcx.leak_check(universe, None).is_ok()
148+
})
149+
}
150+
(None, None) => true,
151+
_ => false,
152+
}
153+
}
154+
104155
/// Retrieves the information we are losing (making dynamic) in an unsizing
105156
/// adjustment.
106157
///
@@ -133,12 +184,8 @@ fn unsized_info<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
133184
// between the same trait with different args. If we, for
134185
// some reason, were to relax the `Unsize` trait, it could become
135186
// unsound, so let's assert here that the trait refs are *equal*.
136-
//
137-
// We can use `assert_eq` because the binders should have been anonymized,
138-
// and because higher-ranked equality now requires the binders are equal.
139-
debug_assert_eq!(
140-
data_a.principal(),
141-
data_b.principal(),
187+
debug_assert!(
188+
validate_trivial_unsize(cx.tcx(), data_a, data_b),
142189
"NOP unsize vtable changed principal trait ref: {data_a} -> {data_b}"
143190
);
144191

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::cell::LazyCell;
22
use std::ops::{ControlFlow, Deref};
33

44
use hir::intravisit::{self, Visitor};
5-
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
5+
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
66
use rustc_errors::codes::*;
77
use rustc_errors::{Applicability, ErrorGuaranteed, pluralize, struct_span_code_err};
88
use rustc_hir::ItemKind;
@@ -404,7 +404,7 @@ fn check_trait_item<'tcx>(
404404
/// ```
405405
fn check_gat_where_clauses(tcx: TyCtxt<'_>, trait_def_id: LocalDefId) {
406406
// Associates every GAT's def_id to a list of possibly missing bounds detected by this lint.
407-
let mut required_bounds_by_item = FxHashMap::default();
407+
let mut required_bounds_by_item = FxIndexMap::default();
408408
let associated_items = tcx.associated_items(trait_def_id);
409409

410410
// Loop over all GATs together, because if this lint suggests adding a where-clause bound
@@ -430,7 +430,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, trait_def_id: LocalDefId) {
430430
// Gather the bounds with which all other items inside of this trait constrain the GAT.
431431
// This is calculated by taking the intersection of the bounds that each item
432432
// constrains the GAT with individually.
433-
let mut new_required_bounds: Option<FxHashSet<ty::Clause<'_>>> = None;
433+
let mut new_required_bounds: Option<FxIndexSet<ty::Clause<'_>>> = None;
434434
for item in associated_items.in_definition_order() {
435435
let item_def_id = item.def_id.expect_local();
436436
// Skip our own GAT, since it does not constrain itself at all.
@@ -589,7 +589,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, trait_def_id: LocalDefId) {
589589
fn augment_param_env<'tcx>(
590590
tcx: TyCtxt<'tcx>,
591591
param_env: ty::ParamEnv<'tcx>,
592-
new_predicates: Option<&FxHashSet<ty::Clause<'tcx>>>,
592+
new_predicates: Option<&FxIndexSet<ty::Clause<'tcx>>>,
593593
) -> ty::ParamEnv<'tcx> {
594594
let Some(new_predicates) = new_predicates else {
595595
return param_env;
@@ -625,9 +625,9 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
625625
wf_tys: &FxIndexSet<Ty<'tcx>>,
626626
gat_def_id: LocalDefId,
627627
gat_generics: &'tcx ty::Generics,
628-
) -> Option<FxHashSet<ty::Clause<'tcx>>> {
628+
) -> Option<FxIndexSet<ty::Clause<'tcx>>> {
629629
// The bounds we that we would require from `to_check`
630-
let mut bounds = FxHashSet::default();
630+
let mut bounds = FxIndexSet::default();
631631

632632
let (regions, types) = GATArgsCollector::visit(gat_def_id.to_def_id(), to_check);
633633

@@ -789,18 +789,18 @@ fn test_region_obligations<'tcx>(
789789
struct GATArgsCollector<'tcx> {
790790
gat: DefId,
791791
// Which region appears and which parameter index its instantiated with
792-
regions: FxHashSet<(ty::Region<'tcx>, usize)>,
792+
regions: FxIndexSet<(ty::Region<'tcx>, usize)>,
793793
// Which params appears and which parameter index its instantiated with
794-
types: FxHashSet<(Ty<'tcx>, usize)>,
794+
types: FxIndexSet<(Ty<'tcx>, usize)>,
795795
}
796796

797797
impl<'tcx> GATArgsCollector<'tcx> {
798798
fn visit<T: TypeFoldable<TyCtxt<'tcx>>>(
799799
gat: DefId,
800800
t: T,
801-
) -> (FxHashSet<(ty::Region<'tcx>, usize)>, FxHashSet<(Ty<'tcx>, usize)>) {
801+
) -> (FxIndexSet<(ty::Region<'tcx>, usize)>, FxIndexSet<(Ty<'tcx>, usize)>) {
802802
let mut visitor =
803-
GATArgsCollector { gat, regions: FxHashSet::default(), types: FxHashSet::default() };
803+
GATArgsCollector { gat, regions: FxIndexSet::default(), types: FxIndexSet::default() };
804804
t.visit_with(&mut visitor);
805805
(visitor.regions, visitor.types)
806806
}

compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, IndexEntry};
1+
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet, IndexEntry};
22
use rustc_errors::codes::*;
33
use rustc_errors::struct_span_code_err;
44
use rustc_hir as hir;
@@ -215,7 +215,7 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
215215

216216
struct ConnectedRegion {
217217
idents: SmallVec<[Symbol; 8]>,
218-
impl_blocks: FxHashSet<usize>,
218+
impl_blocks: FxIndexSet<usize>,
219219
}
220220
let mut connected_regions: IndexVec<RegionId, _> = Default::default();
221221
// Reverse map from the Symbol to the connected region id.

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ mod lint;
2323
use std::slice;
2424

2525
use rustc_ast::TraitObjectSyntax;
26-
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
26+
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
2727
use rustc_errors::codes::*;
2828
use rustc_errors::{
2929
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, FatalError, struct_span_code_err,
@@ -2394,8 +2394,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23942394
#[instrument(level = "trace", skip(self, generate_err))]
23952395
fn validate_late_bound_regions<'cx>(
23962396
&'cx self,
2397-
constrained_regions: FxHashSet<ty::BoundRegionKind>,
2398-
referenced_regions: FxHashSet<ty::BoundRegionKind>,
2397+
constrained_regions: FxIndexSet<ty::BoundRegionKind>,
2398+
referenced_regions: FxIndexSet<ty::BoundRegionKind>,
23992399
generate_err: impl Fn(&str) -> Diag<'cx>,
24002400
) {
24012401
for br in referenced_regions.difference(&constrained_regions) {

compiler/rustc_hir_analysis/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ This API is completely unstable and subject to change.
5858
// tidy-alphabetical-start
5959
#![allow(internal_features)]
6060
#![allow(rustc::diagnostic_outside_of_impl)]
61-
#![allow(rustc::potential_query_instability)]
6261
#![allow(rustc::untranslatable_diagnostic)]
6362
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
6463
#![doc(rust_logo)]

compiler/rustc_middle/src/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ macro_rules! TrivialLiftImpls {
6767
};
6868
}
6969

70-
/// Used for types that are `Copy` and which **do not care arena
70+
/// Used for types that are `Copy` and which **do not care about arena
7171
/// allocated data** (i.e., don't need to be folded).
7272
#[macro_export]
7373
macro_rules! TrivialTypeTraversalImpls {

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ pub struct ResolverGlobalCtxt {
186186
pub proc_macros: Vec<LocalDefId>,
187187
/// Mapping from ident span to path span for paths that don't exist as written, but that
188188
/// exist under `std`. For example, wrote `str::from_utf8` instead of `std::str::from_utf8`.
189-
pub confused_type_with_std_module: FxHashMap<Span, Span>,
189+
pub confused_type_with_std_module: FxIndexMap<Span, Span>,
190190
pub doc_link_resolutions: FxHashMap<LocalDefId, DocLinkResMap>,
191191
pub doc_link_traits_in_scope: FxHashMap<LocalDefId, Vec<DefId>>,
192192
pub all_macro_rules: FxHashMap<Symbol, Res<ast::NodeId>>,

0 commit comments

Comments
 (0)