Skip to content

Commit 39f42ad

Browse files
committed
Auto merge of #113865 - Dylan-DPC:rollup-pt960bk, r=Dylan-DPC
Rollup of 7 pull requests Successful merges: - #113444 (add tests for alias bound preference) - #113716 (Add the `no-builtins` attribute to functions when `no_builtins` is applied at the crate level.) - #113754 (Simplify native_libs query) - #113765 (Make it clearer that edition functions are `>=`, not `==`) - #113774 (Improve error message when closing bracket interpreted as formatting fill character) - #113785 (Fix invalid display of inlined re-export when both local and foreign items are inlined) - #113803 (Fix inline_const with interpolated block) r? `@ghost` `@rustbot` modify labels: rollup
2 parents fdfcdad + 6c3cbcd commit 39f42ad

File tree

46 files changed

+425
-193
lines changed

Some content is hidden

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

46 files changed

+425
-193
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

+4
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ pub fn from_fn_attrs<'ll, 'tcx>(
335335
to_add.extend(probestack_attr(cx));
336336
to_add.extend(stackprotector_attr(cx));
337337

338+
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_BUILTINS) {
339+
to_add.push(llvm::CreateAttrString(cx.llcx, "no-builtins"));
340+
}
341+
338342
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
339343
to_add.push(AttributeKind::Cold.create_attr(cx.llcx));
340344
}

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_ast::{ast, MetaItemKind, NestedMetaItem};
1+
use rustc_ast::{ast, attr, MetaItemKind, NestedMetaItem};
22
use rustc_attr::{list_contains_name, InlineAttr, InstructionSetAttr, OptimizeAttr};
33
use rustc_errors::struct_span_err;
44
use rustc_hir as hir;
@@ -60,6 +60,14 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
6060
codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
6161
}
6262

63+
// When `no_builtins` is applied at the crate level, we should add the
64+
// `no-builtins` attribute to each function to ensure it takes effect in LTO.
65+
let crate_attrs = tcx.hir().attrs(rustc_hir::CRATE_HIR_ID);
66+
let no_builtins = attr::contains_name(crate_attrs, sym::no_builtins);
67+
if no_builtins {
68+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_BUILTINS;
69+
}
70+
6371
let supported_target_features = tcx.supported_target_features(LOCAL_CRATE);
6472

6573
let mut inline_span = None;

compiler/rustc_expand/src/mbe/macro_rules.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ fn check_matcher_core<'tt>(
12001200
err.span_label(sp, format!("not allowed after `{}` fragments", kind));
12011201

12021202
if kind == NonterminalKind::PatWithOr
1203-
&& sess.edition.rust_2021()
1203+
&& sess.edition.at_least_rust_2021()
12041204
&& next_token.is_token(&BinOp(token::BinOpToken::Or))
12051205
{
12061206
let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl(

compiler/rustc_hir_analysis/src/astconv/lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
8686
));
8787
}
8888

89-
if self_ty.span.edition().rust_2021() {
89+
if self_ty.span.edition().at_least_rust_2021() {
9090
let msg = "trait objects must include the `dyn` keyword";
9191
let label = "add `dyn` keyword before this trait";
9292
let mut diag =

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
860860
.resolve_fully_qualified_call(span, item_name, ty.normalized, qself.span, hir_id)
861861
.and_then(|r| {
862862
// lint bare trait if the method is found in the trait
863-
if span.edition().rust_2021() && let Some(mut diag) = self.tcx.sess.diagnostic().steal_diagnostic(qself.span, StashKey::TraitMissingMethod) {
863+
if span.edition().at_least_rust_2021() && let Some(mut diag) = self.tcx.sess.diagnostic().steal_diagnostic(qself.span, StashKey::TraitMissingMethod) {
864864
diag.emit();
865865
}
866866
Ok(r)
@@ -890,7 +890,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
890890
}
891891

892892
// emit or cancel the diagnostic for bare traits
893-
if span.edition().rust_2021() && let Some(mut diag) = self.tcx.sess.diagnostic().steal_diagnostic(qself.span, StashKey::TraitMissingMethod) {
893+
if span.edition().at_least_rust_2021() && let Some(mut diag) = self.tcx.sess.diagnostic().steal_diagnostic(qself.span, StashKey::TraitMissingMethod) {
894894
if trait_missing_method {
895895
// cancel the diag for bare traits when meeting `MyTrait::missing_method`
896896
diag.cancel();
@@ -908,7 +908,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
908908
error,
909909
None,
910910
Expectation::NoExpectation,
911-
trait_missing_method && span.edition().rust_2021(), // emits missing method for trait only after edition 2021
911+
trait_missing_method && span.edition().at_least_rust_2021(), // emits missing method for trait only after edition 2021
912912
) {
913913
e.emit();
914914
}

compiler/rustc_hir_typeck/src/method/prelude2021.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3232
);
3333

3434
// Rust 2021 and later is already using the new prelude
35-
if span.rust_2021() {
35+
if span.at_least_rust_2021() {
3636
return;
3737
}
3838

@@ -203,7 +203,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
203203
pick: &Pick<'tcx>,
204204
) {
205205
// Rust 2021 and later is already using the new prelude
206-
if span.rust_2021() {
206+
if span.at_least_rust_2021() {
207207
return;
208208
}
209209

compiler/rustc_hir_typeck/src/method/probe.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
437437
// this case used to be allowed by the compiler,
438438
// so we do a future-compat lint here for the 2015 edition
439439
// (see https://github.com/rust-lang/rust/issues/46906)
440-
if self.tcx.sess.rust_2018() {
440+
if self.tcx.sess.at_least_rust_2018() {
441441
self.tcx.sess.emit_err(MethodCallOnUnknownRawPointee { span });
442442
} else {
443443
self.tcx.struct_span_lint_hir(
@@ -1592,7 +1592,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
15921592
if let Some(method_name) = self.method_name {
15931593
// Some trait methods are excluded for arrays before 2021.
15941594
// (`array.into_iter()` wants a slice iterator for compatibility.)
1595-
if self_ty.is_array() && !method_name.span.rust_2021() {
1595+
if self_ty.is_array() && !method_name.span.at_least_rust_2021() {
15961596
let trait_def = self.tcx.trait_def(trait_ref.def_id);
15971597
if trait_def.skip_array_during_method_dispatch {
15981598
return ProbeResult::NoMatch;

compiler/rustc_hir_typeck/src/upvar.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2001,7 +2001,7 @@ fn should_do_rust_2021_incompatible_closure_captures_analysis(
20012001
tcx: TyCtxt<'_>,
20022002
closure_id: hir::HirId,
20032003
) -> bool {
2004-
if tcx.sess.rust_2021() {
2004+
if tcx.sess.at_least_rust_2021() {
20052005
return false;
20062006
}
20072007

@@ -2247,5 +2247,5 @@ fn truncate_capture_for_optimization(
22472247
fn enable_precise_capture(span: Span) -> bool {
22482248
// We use span here to ensure that if the closure was generated by a macro with a different
22492249
// edition.
2250-
span.rust_2021()
2250+
span.at_least_rust_2021()
22512251
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1+
use rustc_data_structures::fx::FxIndexMap;
12
use rustc_hir as hir;
23
use rustc_hir::def::DefKind;
4+
use rustc_hir::def_id::DefId;
5+
use rustc_middle::query::LocalCrate;
36
use rustc_middle::ty::TyCtxt;
47
use rustc_session::cstore::ForeignModule;
58

6-
pub(crate) fn collect(tcx: TyCtxt<'_>) -> Vec<ForeignModule> {
7-
let mut modules = Vec::new();
9+
pub(crate) fn collect(tcx: TyCtxt<'_>, LocalCrate: LocalCrate) -> FxIndexMap<DefId, ForeignModule> {
10+
let mut modules = FxIndexMap::default();
11+
12+
// We need to collect all the `ForeignMod`, even if they are empty.
813
for id in tcx.hir().items() {
914
if !matches!(tcx.def_kind(id.owner_id), DefKind::ForeignMod) {
1015
continue;
1116
}
17+
18+
let def_id = id.owner_id.to_def_id();
1219
let item = tcx.hir().item(id);
13-
if let hir::ItemKind::ForeignMod { items, .. } = item.kind {
20+
21+
if let hir::ItemKind::ForeignMod { abi, items } = item.kind {
1422
let foreign_items = items.iter().map(|it| it.id.owner_id.to_def_id()).collect();
15-
modules.push(ForeignModule { foreign_items, def_id: id.owner_id.to_def_id() });
23+
modules.insert(def_id, ForeignModule { def_id, abi, foreign_items });
1624
}
1725
}
26+
1827
modules
1928
}

compiler/rustc_metadata/src/native_libs.rs

+34-46
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
use rustc_ast::{NestedMetaItem, CRATE_NODE_ID};
22
use rustc_attr as attr;
33
use rustc_data_structures::fx::FxHashSet;
4-
use rustc_hir as hir;
5-
use rustc_hir::def::DefKind;
4+
use rustc_middle::query::LocalCrate;
65
use rustc_middle::ty::{List, ParamEnv, ParamEnvAnd, Ty, TyCtxt};
76
use rustc_session::config::CrateType;
8-
use rustc_session::cstore::{DllCallingConvention, DllImport, NativeLib, PeImportNameType};
7+
use rustc_session::cstore::{
8+
DllCallingConvention, DllImport, ForeignModule, NativeLib, PeImportNameType,
9+
};
910
use rustc_session::parse::feature_err;
1011
use rustc_session::search_paths::PathKind;
1112
use rustc_session::utils::NativeLibKind;
1213
use rustc_session::Session;
14+
use rustc_span::def_id::{DefId, LOCAL_CRATE};
1315
use rustc_span::symbol::{sym, Symbol};
1416
use rustc_target::spec::abi::Abi;
1517

@@ -66,10 +68,12 @@ fn find_bundled_library(
6668
None
6769
}
6870

69-
pub(crate) fn collect(tcx: TyCtxt<'_>) -> Vec<NativeLib> {
71+
pub(crate) fn collect(tcx: TyCtxt<'_>, LocalCrate: LocalCrate) -> Vec<NativeLib> {
7072
let mut collector = Collector { tcx, libs: Vec::new() };
71-
for id in tcx.hir().items() {
72-
collector.process_item(id);
73+
if tcx.sess.opts.unstable_opts.link_directives {
74+
for module in tcx.foreign_modules(LOCAL_CRATE).values() {
75+
collector.process_module(module);
76+
}
7377
}
7478
collector.process_command_line();
7579
collector.libs
@@ -88,29 +92,20 @@ struct Collector<'tcx> {
8892
}
8993

9094
impl<'tcx> Collector<'tcx> {
91-
fn process_item(&mut self, id: rustc_hir::ItemId) {
92-
if !matches!(self.tcx.def_kind(id.owner_id), DefKind::ForeignMod) {
93-
return;
94-
}
95+
fn process_module(&mut self, module: &ForeignModule) {
96+
let ForeignModule { def_id, abi, ref foreign_items } = *module;
97+
let def_id = def_id.expect_local();
9598

96-
let it = self.tcx.hir().item(id);
97-
let hir::ItemKind::ForeignMod { abi, items: foreign_mod_items } = it.kind else {
98-
return;
99-
};
99+
let sess = self.tcx.sess;
100100

101101
if matches!(abi, Abi::Rust | Abi::RustIntrinsic | Abi::PlatformIntrinsic) {
102102
return;
103103
}
104104

105105
// Process all of the #[link(..)]-style arguments
106-
let sess = self.tcx.sess;
107106
let features = self.tcx.features();
108107

109-
if !sess.opts.unstable_opts.link_directives {
110-
return;
111-
}
112-
113-
for m in self.tcx.hir().attrs(it.hir_id()).iter().filter(|a| a.has_name(sym::link)) {
108+
for m in self.tcx.get_attrs(def_id, sym::link) {
114109
let Some(items) = m.meta_item_list() else {
115110
continue;
116111
};
@@ -340,9 +335,9 @@ impl<'tcx> Collector<'tcx> {
340335
if name.as_str().contains('\0') {
341336
sess.emit_err(errors::RawDylibNoNul { span: name_span });
342337
}
343-
foreign_mod_items
338+
foreign_items
344339
.iter()
345-
.map(|child_item| {
340+
.map(|&child_item| {
346341
self.build_dll_import(
347342
abi,
348343
import_name_type.map(|(import_name_type, _)| import_name_type),
@@ -352,21 +347,12 @@ impl<'tcx> Collector<'tcx> {
352347
.collect()
353348
}
354349
_ => {
355-
for child_item in foreign_mod_items {
356-
if self.tcx.def_kind(child_item.id.owner_id).has_codegen_attrs()
357-
&& self
358-
.tcx
359-
.codegen_fn_attrs(child_item.id.owner_id)
360-
.link_ordinal
361-
.is_some()
350+
for &child_item in foreign_items {
351+
if self.tcx.def_kind(child_item).has_codegen_attrs()
352+
&& self.tcx.codegen_fn_attrs(child_item).link_ordinal.is_some()
362353
{
363-
let link_ordinal_attr = self
364-
.tcx
365-
.hir()
366-
.attrs(child_item.id.owner_id.into())
367-
.iter()
368-
.find(|a| a.has_name(sym::link_ordinal))
369-
.unwrap();
354+
let link_ordinal_attr =
355+
self.tcx.get_attr(child_item, sym::link_ordinal).unwrap();
370356
sess.emit_err(errors::LinkOrdinalRawDylib {
371357
span: link_ordinal_attr.span,
372358
});
@@ -384,7 +370,7 @@ impl<'tcx> Collector<'tcx> {
384370
filename,
385371
kind,
386372
cfg,
387-
foreign_module: Some(it.owner_id.to_def_id()),
373+
foreign_module: Some(def_id.to_def_id()),
388374
verbatim,
389375
dll_imports,
390376
});
@@ -476,10 +462,10 @@ impl<'tcx> Collector<'tcx> {
476462
}
477463
}
478464

479-
fn i686_arg_list_size(&self, item: &hir::ForeignItemRef) -> usize {
465+
fn i686_arg_list_size(&self, item: DefId) -> usize {
480466
let argument_types: &List<Ty<'_>> = self.tcx.erase_late_bound_regions(
481467
self.tcx
482-
.type_of(item.id.owner_id)
468+
.type_of(item)
483469
.instantiate_identity()
484470
.fn_sig(self.tcx)
485471
.inputs()
@@ -505,8 +491,10 @@ impl<'tcx> Collector<'tcx> {
505491
&self,
506492
abi: Abi,
507493
import_name_type: Option<PeImportNameType>,
508-
item: &hir::ForeignItemRef,
494+
item: DefId,
509495
) -> DllImport {
496+
let span = self.tcx.def_span(item);
497+
510498
let calling_convention = if self.tcx.sess.target.arch == "x86" {
511499
match abi {
512500
Abi::C { .. } | Abi::Cdecl { .. } => DllCallingConvention::C,
@@ -520,29 +508,29 @@ impl<'tcx> Collector<'tcx> {
520508
DllCallingConvention::Vectorcall(self.i686_arg_list_size(item))
521509
}
522510
_ => {
523-
self.tcx.sess.emit_fatal(errors::UnsupportedAbiI686 { span: item.span });
511+
self.tcx.sess.emit_fatal(errors::UnsupportedAbiI686 { span });
524512
}
525513
}
526514
} else {
527515
match abi {
528516
Abi::C { .. } | Abi::Win64 { .. } | Abi::System { .. } => DllCallingConvention::C,
529517
_ => {
530-
self.tcx.sess.emit_fatal(errors::UnsupportedAbi { span: item.span });
518+
self.tcx.sess.emit_fatal(errors::UnsupportedAbi { span });
531519
}
532520
}
533521
};
534522

535-
let codegen_fn_attrs = self.tcx.codegen_fn_attrs(item.id.owner_id);
523+
let codegen_fn_attrs = self.tcx.codegen_fn_attrs(item);
536524
let import_name_type = codegen_fn_attrs
537525
.link_ordinal
538526
.map_or(import_name_type, |ord| Some(PeImportNameType::Ordinal(ord)));
539527

540528
DllImport {
541-
name: codegen_fn_attrs.link_name.unwrap_or(item.ident.name),
529+
name: codegen_fn_attrs.link_name.unwrap_or(self.tcx.item_name(item)),
542530
import_name_type,
543531
calling_convention,
544-
span: item.span,
545-
is_fn: self.tcx.def_kind(item.id.owner_id).is_fn_like(),
532+
span,
533+
is_fn: self.tcx.def_kind(item).is_fn_like(),
546534
}
547535
}
548536
}

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,8 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
403403
.contains(&id)
404404
})
405405
},
406-
native_libraries: |tcx, LocalCrate| native_libs::collect(tcx),
407-
foreign_modules: |tcx, LocalCrate| {
408-
foreign_modules::collect(tcx).into_iter().map(|m| (m.def_id, m)).collect()
409-
},
406+
native_libraries: native_libs::collect,
407+
foreign_modules: foreign_modules::collect,
410408

411409
// Returns a map from a sufficiently visible external item (i.e., an
412410
// external item that is visible from at least one local module) to a

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ bitflags! {
100100
const REALLOCATOR = 1 << 18;
101101
/// `#[rustc_allocator_zeroed]`: a hint to LLVM that the function only allocates zeroed memory.
102102
const ALLOCATOR_ZEROED = 1 << 19;
103+
/// `#[no_builtins]`: indicates that disable implicit builtin knowledge of functions for the function.
104+
const NO_BUILTINS = 1 << 20;
103105
}
104106
}
105107

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1579,7 +1579,7 @@ rustc_queries! {
15791579
}
15801580

15811581
/// Returns a list of all `extern` blocks of a crate.
1582-
query foreign_modules(_: CrateNum) -> &'tcx FxHashMap<DefId, ForeignModule> {
1582+
query foreign_modules(_: CrateNum) -> &'tcx FxIndexMap<DefId, ForeignModule> {
15831583
arena_cache
15841584
desc { "looking up the foreign modules of a linked crate" }
15851585
separate_provide_extern

compiler/rustc_middle/src/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1932,7 +1932,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
19321932
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
19331933
self.empty_path = true;
19341934
if cnum == LOCAL_CRATE {
1935-
if self.tcx.sess.rust_2018() {
1935+
if self.tcx.sess.at_least_rust_2018() {
19361936
// We add the `crate::` keyword on Rust 2018, only when desired.
19371937
if SHOULD_PREFIX_WITH_CRATE.with(|flag| flag.get()) {
19381938
write!(self, "{}", kw::Crate)?;

compiler/rustc_mir_build/src/build/expr/as_place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -730,5 +730,5 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
730730

731731
/// Precise capture is enabled if user is using Rust Edition 2021 or higher.
732732
fn enable_precise_capture(closure_span: Span) -> bool {
733-
closure_span.rust_2021()
733+
closure_span.at_least_rust_2021()
734734
}

0 commit comments

Comments
 (0)