Skip to content

Commit c572ece

Browse files
committed
Auto merge of #158503 - JonathanBrouwer:rollup-bBcH9ML, r=JonathanBrouwer
Rollup of 10 pull requests Successful merges: - #158502 (Revert "LLVM 23: Adapt codegen test to moved assume") - #152225 (Add supertrait item shadowing for type-level path resolution) - #158194 (Adds RmetaLinkCache a per-link cache that uses path as the key of dec…) - #158466 (rustdoc: show impl Trait<Box<Local>> for Foreign, etc on Local's docs) - #158501 (miri subtree update) - #153097 (Expand `OptionFlatten`'s iterator methods) - #158163 (Fix too-short variance slice in `variances_of` cycle recovery) - #158233 (Allow the unstable attribute on foreign type) - #158470 (Upgrade `jsonsocck` and `jsondoclint` to edition 2024.) - #158488 (Upgrade `rustdoc-json-types` to 2024 edition.)
2 parents 13f1859 + 8696dfa commit c572ece

130 files changed

Lines changed: 2801 additions & 792 deletions

File tree

Some content is hidden

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

compiler/rustc_attr_parsing/src/attributes/stability.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
4040
Allow(Target::Static),
4141
Allow(Target::ForeignFn),
4242
Allow(Target::ForeignStatic),
43+
Allow(Target::ForeignTy),
4344
Allow(Target::ExternCrate),
4445
]);
4546

compiler/rustc_codegen_ssa/src/back/archive.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_target::spec::Arch;
2222
use tracing::trace;
2323

2424
use super::metadata::{create_compressed_metadata_file, search_for_section};
25-
use super::rmeta_link;
25+
use super::rmeta_link::{self, RmetaLinkCache};
2626
use super::symbol_edit::{apply_edits, collect_internal_names};
2727
use crate::common;
2828
// Public for ArchiveBuilderBuilder::extract_bundled_libs
@@ -311,7 +311,7 @@ fn find_binutils_dlltool(sess: &Session) -> OsString {
311311
}
312312

313313
pub enum AddArchiveKind<'a> {
314-
Rlib(/*skip*/ &'a dyn Fn(&str, ArchiveEntryKind) -> bool),
314+
Rlib(&'a mut RmetaLinkCache, /*skip*/ &'a dyn Fn(&str, ArchiveEntryKind) -> bool),
315315
Other,
316316
}
317317

@@ -466,7 +466,11 @@ pub fn try_extract_macho_fat_archive(
466466
}
467467

468468
impl<'a> ArchiveBuilder for ArArchiveBuilder<'a> {
469-
fn add_archive(&mut self, archive_path: &Path, ar_kind: AddArchiveKind<'_>) -> io::Result<()> {
469+
fn add_archive(
470+
&mut self,
471+
archive_path: &Path,
472+
mut ar_kind: AddArchiveKind<'_>,
473+
) -> io::Result<()> {
470474
let mut archive_path = archive_path.to_path_buf();
471475
if self.sess.target.llvm_target.contains("-apple-macosx")
472476
&& let Some(new_archive_path) = try_extract_macho_fat_archive(self.sess, &archive_path)?
@@ -481,8 +485,14 @@ impl<'a> ArchiveBuilder for ArArchiveBuilder<'a> {
481485
let archive_map = unsafe { Mmap::map(File::open(&archive_path)?)? };
482486
let archive = ArchiveFile::parse(&*archive_map)
483487
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
484-
let metadata_link = match ar_kind {
485-
AddArchiveKind::Rlib(..) => rmeta_link::read(&archive, &archive_map, &archive_path),
488+
let skip = match &ar_kind {
489+
AddArchiveKind::Rlib(_, skip) => Some(*skip),
490+
AddArchiveKind::Other => None,
491+
};
492+
let metadata_link = match &mut ar_kind {
493+
AddArchiveKind::Rlib(cache, _) => cache.get_or_insert_with(&archive_path, || {
494+
rmeta_link::read(&archive, &archive_map, &archive_path)
495+
}),
486496
AddArchiveKind::Other => None,
487497
};
488498
let archive_index = self.src_archives.len();
@@ -512,9 +522,9 @@ impl<'a> ArchiveBuilder for ArArchiveBuilder<'a> {
512522
} else {
513523
ArchiveEntryKind::Other
514524
};
515-
let drop = match ar_kind {
516-
AddArchiveKind::Rlib(skip) => skip(&file_name, kind),
517-
AddArchiveKind::Other => false,
525+
let drop = match skip {
526+
Some(skip) => skip(&file_name, kind),
527+
None => false,
518528
};
519529
if !drop {
520530
let source = if entry.is_thin() {

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ use super::archive::{
5959
use super::command::Command;
6060
use super::linker::{self, Linker};
6161
use super::metadata::{MetadataPosition, create_wrapper_file};
62+
use super::rmeta_link::RmetaLinkCache;
6263
use super::rpath::{self, RPathConfig};
6364
use super::{apple, rmeta_link, versioned_llvm_target};
6465
use crate::base::needs_allocator_shim_for_linking;
@@ -86,6 +87,7 @@ pub fn link_binary(
8687
let _timer = sess.timer("link_binary");
8788
let output_metadata = sess.opts.output_types.contains_key(&OutputType::Metadata);
8889
let mut tempfiles_for_stdout_output: Vec<PathBuf> = Vec::new();
90+
let mut rmeta_link_cache = RmetaLinkCache::default();
8991
for &crate_type in &crate_info.crate_types {
9092
// Ignore executable crates if we have -Z no-codegen, as they will error.
9193
if (sess.opts.unstable_opts.no_codegen || !sess.opts.output_types.should_codegen())
@@ -139,6 +141,7 @@ pub fn link_binary(
139141
link_staticlib(
140142
sess,
141143
archive_builder_builder,
144+
&mut rmeta_link_cache,
142145
&compiled_modules,
143146
&crate_info,
144147
&metadata,
@@ -150,6 +153,7 @@ pub fn link_binary(
150153
link_natively(
151154
sess,
152155
archive_builder_builder,
156+
&mut rmeta_link_cache,
153157
crate_type,
154158
&out_filename,
155159
&compiled_modules,
@@ -502,6 +506,7 @@ fn link_rlib<'a>(
502506
fn link_staticlib(
503507
sess: &Session,
504508
archive_builder_builder: &dyn ArchiveBuilderBuilder,
509+
rmeta_link_cache: &mut RmetaLinkCache,
505510
compiled_modules: &CompiledModules,
506511
crate_info: &CrateInfo,
507512
metadata: &EncodedMetadata,
@@ -531,7 +536,7 @@ fn link_staticlib(
531536
let bundled_libs: FxIndexSet<_> = native_libs.filter_map(|lib| lib.filename).collect();
532537
ab.add_archive(
533538
path,
534-
AddArchiveKind::Rlib(&|fname: &str, entry_kind| {
539+
AddArchiveKind::Rlib(rmeta_link_cache, &|fname: &str, entry_kind| {
535540
// Ignore metadata and rmeta-link files.
536541
if fname == METADATA_FILENAME || fname == rmeta_link::FILENAME {
537542
return true;
@@ -939,6 +944,7 @@ fn report_linker_output(
939944
fn link_natively(
940945
sess: &Session,
941946
archive_builder_builder: &dyn ArchiveBuilderBuilder,
947+
rmeta_link_cache: &mut RmetaLinkCache,
942948
crate_type: CrateType,
943949
out_filename: &Path,
944950
compiled_modules: &CompiledModules,
@@ -965,6 +971,7 @@ fn link_natively(
965971
flavor,
966972
sess,
967973
archive_builder_builder,
974+
rmeta_link_cache,
968975
crate_type,
969976
tmpdir,
970977
temp_filename,
@@ -2562,6 +2569,7 @@ fn linker_with_args(
25622569
flavor: LinkerFlavor,
25632570
sess: &Session,
25642571
archive_builder_builder: &dyn ArchiveBuilderBuilder,
2572+
rmeta_link_cache: &mut RmetaLinkCache,
25652573
crate_type: CrateType,
25662574
tmpdir: &Path,
25672575
out_filename: &Path,
@@ -2690,6 +2698,7 @@ fn linker_with_args(
26902698
cmd,
26912699
sess,
26922700
archive_builder_builder,
2701+
rmeta_link_cache,
26932702
crate_info,
26942703
crate_type,
26952704
tmpdir,
@@ -3126,6 +3135,7 @@ fn add_upstream_rust_crates(
31263135
cmd: &mut dyn Linker,
31273136
sess: &Session,
31283137
archive_builder_builder: &dyn ArchiveBuilderBuilder,
3138+
rmeta_link_cache: &mut RmetaLinkCache,
31293139
crate_info: &CrateInfo,
31303140
crate_type: CrateType,
31313141
tmpdir: &Path,
@@ -3178,6 +3188,7 @@ fn add_upstream_rust_crates(
31783188
cmd,
31793189
sess,
31803190
archive_builder_builder,
3191+
rmeta_link_cache,
31813192
crate_info,
31823193
tmpdir,
31833194
cnum,
@@ -3309,6 +3320,7 @@ fn add_static_crate(
33093320
cmd: &mut dyn Linker,
33103321
sess: &Session,
33113322
archive_builder_builder: &dyn ArchiveBuilderBuilder,
3323+
rmeta_link_cache: &mut RmetaLinkCache,
33123324
crate_info: &CrateInfo,
33133325
tmpdir: &Path,
33143326
cnum: CrateNum,
@@ -3339,7 +3351,7 @@ fn add_static_crate(
33393351
let mut archive = archive_builder_builder.new_archive_builder(sess);
33403352
if let Err(error) = archive.add_archive(
33413353
cratepath,
3342-
AddArchiveKind::Rlib(&|f, entry_kind| {
3354+
AddArchiveKind::Rlib(rmeta_link_cache, &|f, entry_kind| {
33433355
if f == METADATA_FILENAME || f == rmeta_link::FILENAME {
33443356
return true;
33453357
}

compiler/rustc_codegen_ssa/src/back/rmeta_link.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
//! and potentially other data collected and used when building or linking a rlib.
33
//! See <https://github.com/rust-lang/rust/issues/138243>.
44
5-
use std::path::Path;
5+
use std::path::{Path, PathBuf};
66

77
use object::read::archive::ArchiveFile;
8+
use rustc_data_structures::fx::FxHashMap;
89
use rustc_serialize::opaque::mem_encoder::MemEncoder;
910
use rustc_serialize::opaque::{MAGIC_END_BYTES, MemDecoder};
1011
use rustc_serialize::{Decodable, Encodable};
@@ -54,3 +55,18 @@ pub fn read_from_data(archive_data: &[u8], rlib_path: &Path) -> Option<RmetaLink
5455
let archive = ArchiveFile::parse(archive_data).ok()?;
5556
read(&archive, archive_data, rlib_path)
5657
}
58+
59+
#[derive(Default)]
60+
pub struct RmetaLinkCache {
61+
cache: FxHashMap<PathBuf, Option<RmetaLink>>,
62+
}
63+
64+
impl RmetaLinkCache {
65+
pub fn get_or_insert_with(
66+
&mut self,
67+
rlib_path: &Path,
68+
load: impl FnOnce() -> Option<RmetaLink>,
69+
) -> Option<&RmetaLink> {
70+
self.cache.entry(rlib_path.to_path_buf()).or_insert_with(load).as_ref()
71+
}
72+
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
409409

410410
pub(super) fn report_ambiguous_assoc_item(
411411
&self,
412-
bound1: ty::PolyTraitRef<'tcx>,
413-
bound2: ty::PolyTraitRef<'tcx>,
414-
matching_candidates: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
412+
matching_candidates: &[ty::PolyTraitRef<'tcx>],
415413
qself: AssocItemQSelf,
416414
assoc_tag: ty::AssocTag,
417415
assoc_ident: Ident,
@@ -443,7 +441,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
443441
// predicates!).
444442
// FIXME: Turn this into a structured, translatable & more actionable suggestion.
445443
let mut where_bounds = vec![];
446-
for bound in [bound1, bound2].into_iter().chain(matching_candidates) {
444+
for &bound in matching_candidates {
447445
let bound_id = bound.def_id();
448446
let assoc_item = tcx.associated_items(bound_id).find_by_ident_and_kind(
449447
tcx,

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use std::{assert_matches, slice};
2424
use rustc_abi::FIRST_VARIANT;
2525
use rustc_ast::LitKind;
2626
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
27+
use rustc_data_structures::sso::SsoHashSet;
2728
use rustc_errors::codes::*;
2829
use rustc_errors::{
2930
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, FatalError, StashKey,
@@ -1262,6 +1263,74 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
12621263
)
12631264
}
12641265

1266+
/// When there are multiple traits which contain an identically named
1267+
/// associated item, this function eliminates any traits which are a
1268+
/// supertrait of another candidate trait.
1269+
///
1270+
/// This is the type-level analogue of
1271+
/// `rustc_hir_typeck::method::probe::ProbeContext::collapse_candidates_to_subtrait_pick`;
1272+
/// keep both implementations in sync.
1273+
///
1274+
/// This implements RFC #3624.
1275+
fn collapse_candidates_to_subtrait_pick(
1276+
&self,
1277+
matching_candidates: &[ty::PolyTraitRef<'tcx>],
1278+
) -> Option<ty::PolyTraitRef<'tcx>> {
1279+
if !self.tcx().features().supertrait_item_shadowing() {
1280+
return None;
1281+
}
1282+
1283+
let mut child_trait = matching_candidates[0];
1284+
let mut supertraits: SsoHashSet<_> =
1285+
traits::supertrait_def_ids(self.tcx(), child_trait.def_id()).collect();
1286+
1287+
let mut remaining_candidates: Vec<_> = matching_candidates[1..].iter().copied().collect();
1288+
while !remaining_candidates.is_empty() {
1289+
let mut made_progress = false;
1290+
let mut next_round = vec![];
1291+
1292+
for remaining_trait in remaining_candidates {
1293+
if supertraits.contains(&remaining_trait.def_id()) {
1294+
made_progress = true;
1295+
continue;
1296+
}
1297+
1298+
// This candidate is not a supertrait of the `child_trait`.
1299+
// Check if it's a subtrait of the `child_trait`, instead.
1300+
// If it is, then it must have been a subtrait of every
1301+
// other pick we've eliminated at this point. It will
1302+
// take over at this point.
1303+
let remaining_trait_supertraits: SsoHashSet<_> =
1304+
traits::supertrait_def_ids(self.tcx(), remaining_trait.def_id()).collect();
1305+
if remaining_trait_supertraits.contains(&child_trait.def_id()) {
1306+
child_trait = remaining_trait;
1307+
supertraits = remaining_trait_supertraits;
1308+
made_progress = true;
1309+
continue;
1310+
}
1311+
1312+
// Neither `child_trait` or the current candidate are
1313+
// supertraits of each other.
1314+
// Don't bail here, since we may be comparing two supertraits
1315+
// of a common subtrait. These two supertraits won't be related
1316+
// at all, but we will pick them up next round when we find their
1317+
// child as we continue iterating in this round.
1318+
next_round.push(remaining_trait);
1319+
}
1320+
1321+
if made_progress {
1322+
// If we've made progress, iterate again.
1323+
remaining_candidates = next_round;
1324+
} else {
1325+
// Otherwise, we must have at least two candidates which
1326+
// are not related to each other at all.
1327+
return None;
1328+
}
1329+
}
1330+
1331+
Some(child_trait)
1332+
}
1333+
12651334
/// Search for a single trait bound whose trait defines the associated item given by
12661335
/// `assoc_ident`.
12671336
///
@@ -1296,10 +1365,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
12961365
};
12971366

12981367
if let Some(bound2) = matching_candidates.next() {
1368+
let all_matching_candidates: Vec<_> =
1369+
[bound1, bound2].into_iter().chain(matching_candidates).collect();
1370+
if let Some(bound) = self.collapse_candidates_to_subtrait_pick(&all_matching_candidates)
1371+
{
1372+
return Ok(bound);
1373+
}
1374+
12991375
return Err(self.report_ambiguous_assoc_item(
1300-
bound1,
1301-
bound2,
1302-
matching_candidates,
1376+
&all_matching_candidates,
13031377
qself,
13041378
assoc_tag,
13051379
assoc_ident,

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,6 +2359,10 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
23592359
/// multiple conflicting picks if there is one pick whose trait container is a subtrait
23602360
/// of the trait containers of all of the other picks.
23612361
///
2362+
/// This is the method-probe analogue of
2363+
/// `rustc_hir_analysis::hir_ty_lowering::HirTyLowerer::collapse_candidates_to_subtrait_pick`;
2364+
/// keep both implementations in sync.
2365+
///
23622366
/// This implements RFC #3624.
23632367
fn collapse_candidates_to_subtrait_pick(
23642368
&self,

compiler/rustc_query_impl/src/handle_cycle_error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub(crate) fn variances_of<'tcx>(
105105
err: Diag<'_>,
106106
) -> &'tcx [ty::Variance] {
107107
let _guar = err.delay_as_bug();
108-
let n = tcx.generics_of(def_id).own_params.len();
108+
let n = tcx.generics_of(def_id).count();
109109
tcx.arena.alloc_from_iter(iter::repeat_n(ty::Bivariant, n))
110110
}
111111

0 commit comments

Comments
 (0)