Skip to content

Commit 5baee04

Browse files
committed
Auto merge of rust-lang#125456 - fmease:rollup-n8608gc, r=fmease
Rollup of 7 pull requests Successful merges: - rust-lang#122382 (Detect unused structs which implement private traits) - rust-lang#124389 (Add a warning to proc_macro::Delimiter::None that rustc currently does not respect it.) - rust-lang#125224 (Migrate `run-make/issue-53964` to `rmake`) - rust-lang#125227 (Migrate `run-make/issue-30063` to `rmake`) - rust-lang#125336 (Add dedicated definition for intrinsics) - rust-lang#125401 (Migrate `run-make/rustdoc-scrape-examples-macros` to `rmake.rs`) - rust-lang#125454 (Improve the doc of query associated_item) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 606afbb + 55a16a4 commit 5baee04

File tree

23 files changed

+322
-114
lines changed

23 files changed

+322
-114
lines changed

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ rustc_queries! {
780780
separate_provide_extern
781781
}
782782

783-
/// Maps from a trait item to the trait item "descriptor".
783+
/// Maps from a trait/impl item to the trait/impl item "descriptor".
784784
query associated_item(key: DefId) -> ty::AssocItem {
785785
desc { |tcx| "computing associated item data for `{}`", tcx.def_path_str(key) }
786786
cache_on_disk_if { key.is_local() }

compiler/rustc_passes/src/dead.rs

+33-16
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,11 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
425425
&& let ItemKind::Impl(impl_ref) =
426426
self.tcx.hir().expect_item(local_impl_id).kind
427427
{
428-
if self.tcx.visibility(trait_id).is_public()
429-
&& matches!(trait_item.kind, hir::TraitItemKind::Fn(..))
428+
if matches!(trait_item.kind, hir::TraitItemKind::Fn(..))
430429
&& !ty_ref_to_pub_struct(self.tcx, impl_ref.self_ty)
431430
{
431+
// skip methods of private ty,
432+
// they would be solved in `solve_rest_impl_items`
432433
continue;
433434
}
434435

@@ -485,32 +486,46 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
485486

486487
fn solve_rest_impl_items(&mut self, mut unsolved_impl_items: Vec<(hir::ItemId, LocalDefId)>) {
487488
let mut ready;
488-
(ready, unsolved_impl_items) = unsolved_impl_items
489-
.into_iter()
490-
.partition(|&(impl_id, _)| self.impl_item_with_used_self(impl_id));
489+
(ready, unsolved_impl_items) =
490+
unsolved_impl_items.into_iter().partition(|&(impl_id, impl_item_id)| {
491+
self.impl_item_with_used_self(impl_id, impl_item_id)
492+
});
491493

492494
while !ready.is_empty() {
493495
self.worklist =
494496
ready.into_iter().map(|(_, id)| (id, ComesFromAllowExpect::No)).collect();
495497
self.mark_live_symbols();
496498

497-
(ready, unsolved_impl_items) = unsolved_impl_items
498-
.into_iter()
499-
.partition(|&(impl_id, _)| self.impl_item_with_used_self(impl_id));
499+
(ready, unsolved_impl_items) =
500+
unsolved_impl_items.into_iter().partition(|&(impl_id, impl_item_id)| {
501+
self.impl_item_with_used_self(impl_id, impl_item_id)
502+
});
500503
}
501504
}
502505

503-
fn impl_item_with_used_self(&mut self, impl_id: hir::ItemId) -> bool {
506+
fn impl_item_with_used_self(&mut self, impl_id: hir::ItemId, impl_item_id: LocalDefId) -> bool {
504507
if let TyKind::Path(hir::QPath::Resolved(_, path)) =
505508
self.tcx.hir().item(impl_id).expect_impl().self_ty.kind
506509
&& let Res::Def(def_kind, def_id) = path.res
507510
&& let Some(local_def_id) = def_id.as_local()
508511
&& matches!(def_kind, DefKind::Struct | DefKind::Enum | DefKind::Union)
509512
{
510-
self.live_symbols.contains(&local_def_id)
511-
} else {
512-
false
513+
if self.tcx.visibility(impl_item_id).is_public() {
514+
// for the public method, we don't know the trait item is used or not,
515+
// so we mark the method live if the self is used
516+
return self.live_symbols.contains(&local_def_id);
517+
}
518+
519+
if let Some(trait_item_id) = self.tcx.associated_item(impl_item_id).trait_item_def_id
520+
&& let Some(local_id) = trait_item_id.as_local()
521+
{
522+
// for the private method, we can know the trait item is used or not,
523+
// so we mark the method live if the self is used and the trait item is used
524+
return self.live_symbols.contains(&local_id)
525+
&& self.live_symbols.contains(&local_def_id);
526+
}
513527
}
528+
false
514529
}
515530
}
516531

@@ -745,20 +760,22 @@ fn check_item<'tcx>(
745760
matches!(fn_sig.decl.implicit_self, hir::ImplicitSelfKind::None);
746761
}
747762

748-
// for impl trait blocks, mark associate functions live if the trait is public
763+
// for trait impl blocks,
764+
// mark the method live if the self_ty is public,
765+
// or the method is public and may construct self
749766
if of_trait
750767
&& (!matches!(tcx.def_kind(local_def_id), DefKind::AssocFn)
751768
|| tcx.visibility(local_def_id).is_public()
752769
&& (ty_is_pub || may_construct_self))
753770
{
754771
worklist.push((local_def_id, ComesFromAllowExpect::No));
755-
} else if of_trait && tcx.visibility(local_def_id).is_public() {
756-
// pub method && private ty & methods not construct self
757-
unsolved_impl_items.push((id, local_def_id));
758772
} else if let Some(comes_from_allow) =
759773
has_allow_dead_code_or_lang_attr(tcx, local_def_id)
760774
{
761775
worklist.push((local_def_id, comes_from_allow));
776+
} else if of_trait {
777+
// private method || public method not constructs self
778+
unsolved_impl_items.push((id, local_def_id));
762779
}
763780
}
764781
}

compiler/rustc_smir/src/rustc_smir/context.rs

+24-12
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use stable_mir::mir::{BinOp, Body, Place};
2323
use stable_mir::target::{MachineInfo, MachineSize};
2424
use stable_mir::ty::{
2525
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, ForeignDef,
26-
ForeignItemKind, GenericArgs, LineInfo, PolyFnSig, RigidTy, Span, Ty, TyKind, UintTy,
27-
VariantDef,
26+
ForeignItemKind, GenericArgs, IntrinsicDef, LineInfo, PolyFnSig, RigidTy, Span, Ty, TyKind,
27+
UintTy, VariantDef,
2828
};
2929
use stable_mir::{Crate, CrateDef, CrateItem, CrateNum, DefId, Error, Filename, ItemKind, Symbol};
3030
use std::cell::RefCell;
@@ -312,6 +312,28 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
312312
sig.stable(&mut *tables)
313313
}
314314

315+
fn intrinsic(&self, def: DefId) -> Option<IntrinsicDef> {
316+
let mut tables = self.0.borrow_mut();
317+
let tcx = tables.tcx;
318+
let def_id = def.internal(&mut *tables, tcx);
319+
let intrinsic = tcx.intrinsic_raw(def_id);
320+
intrinsic.map(|_| IntrinsicDef(def))
321+
}
322+
323+
fn intrinsic_name(&self, def: IntrinsicDef) -> Symbol {
324+
let mut tables = self.0.borrow_mut();
325+
let tcx = tables.tcx;
326+
let def_id = def.0.internal(&mut *tables, tcx);
327+
tcx.intrinsic(def_id).unwrap().name.to_string()
328+
}
329+
330+
fn intrinsic_must_be_overridden(&self, def: IntrinsicDef) -> bool {
331+
let mut tables = self.0.borrow_mut();
332+
let tcx = tables.tcx;
333+
let def_id = def.0.internal(&mut *tables, tcx);
334+
tcx.intrinsic_raw(def_id).unwrap().must_be_overridden
335+
}
336+
315337
fn closure_sig(&self, args: &GenericArgs) -> PolyFnSig {
316338
let mut tables = self.0.borrow_mut();
317339
let tcx = tables.tcx;
@@ -650,16 +672,6 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
650672
}
651673
}
652674

653-
/// Retrieve the plain intrinsic name of an instance.
654-
///
655-
/// This assumes that the instance is an intrinsic.
656-
fn intrinsic_name(&self, def: InstanceDef) -> Symbol {
657-
let tables = self.0.borrow_mut();
658-
let instance = tables.instances[def];
659-
let intrinsic = tables.tcx.intrinsic(instance.def_id()).unwrap();
660-
intrinsic.name.to_string()
661-
}
662-
663675
fn ty_layout(&self, ty: Ty) -> Result<Layout, Error> {
664676
let mut tables = self.0.borrow_mut();
665677
let tcx = tables.tcx;

compiler/stable_mir/src/compiler_interface.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use crate::target::MachineInfo;
1313
use crate::ty::{
1414
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, ForeignDef,
1515
ForeignItemKind, ForeignModule, ForeignModuleDef, GenericArgs, GenericPredicates, Generics,
16-
ImplDef, ImplTrait, LineInfo, PolyFnSig, RigidTy, Span, TraitDecl, TraitDef, Ty, TyKind,
17-
UintTy, VariantDef,
16+
ImplDef, ImplTrait, IntrinsicDef, LineInfo, PolyFnSig, RigidTy, Span, TraitDecl, TraitDef, Ty,
17+
TyKind, UintTy, VariantDef,
1818
};
1919
use crate::{
2020
mir, Crate, CrateItem, CrateItems, CrateNum, DefId, Error, Filename, ImplTraitDecls, ItemKind,
@@ -88,6 +88,16 @@ pub trait Context {
8888
/// Retrieve the function signature for the given generic arguments.
8989
fn fn_sig(&self, def: FnDef, args: &GenericArgs) -> PolyFnSig;
9090

91+
/// Retrieve the intrinsic definition if the item corresponds one.
92+
fn intrinsic(&self, item: DefId) -> Option<IntrinsicDef>;
93+
94+
/// Retrieve the plain function name of an intrinsic.
95+
fn intrinsic_name(&self, def: IntrinsicDef) -> Symbol;
96+
97+
/// Returns whether the intrinsic has no meaningful body and all backends
98+
/// need to shim all calls to it.
99+
fn intrinsic_must_be_overridden(&self, def: IntrinsicDef) -> bool;
100+
91101
/// Retrieve the closure signature for the given generic arguments.
92102
fn closure_sig(&self, args: &GenericArgs) -> PolyFnSig;
93103

@@ -198,7 +208,6 @@ pub trait Context {
198208
fn vtable_allocation(&self, global_alloc: &GlobalAlloc) -> Option<AllocId>;
199209
fn krate(&self, def_id: DefId) -> Crate;
200210
fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol;
201-
fn intrinsic_name(&self, def: InstanceDef) -> Symbol;
202211

203212
/// Return information about the target machine.
204213
fn target_info(&self) -> MachineInfo;

compiler/stable_mir/src/mir/mono.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ impl Instance {
106106
/// which is more convenient to match with intrinsic symbols.
107107
pub fn intrinsic_name(&self) -> Option<Symbol> {
108108
match self.kind {
109-
InstanceKind::Intrinsic => Some(with(|context| context.intrinsic_name(self.def))),
109+
InstanceKind::Intrinsic => {
110+
Some(with(|context| context.intrinsic(self.def.def_id()).unwrap().fn_name()))
111+
}
110112
InstanceKind::Item | InstanceKind::Virtual { .. } | InstanceKind::Shim => None,
111113
}
112114
}

compiler/stable_mir/src/ty.rs

+35
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,41 @@ impl FnDef {
621621
pub fn body(&self) -> Option<Body> {
622622
with(|ctx| ctx.has_body(self.0).then(|| ctx.mir_body(self.0)))
623623
}
624+
625+
/// Get the information of the intrinsic if this function is a definition of one.
626+
pub fn as_intrinsic(&self) -> Option<IntrinsicDef> {
627+
with(|cx| cx.intrinsic(self.def_id()))
628+
}
629+
630+
/// Check if the function is an intrinsic.
631+
#[inline]
632+
pub fn is_intrinsic(&self) -> bool {
633+
self.as_intrinsic().is_some()
634+
}
635+
}
636+
637+
crate_def! {
638+
pub IntrinsicDef;
639+
}
640+
641+
impl IntrinsicDef {
642+
/// Returns the plain name of the intrinsic.
643+
/// e.g., `transmute` for `core::intrinsics::transmute`.
644+
pub fn fn_name(&self) -> Symbol {
645+
with(|cx| cx.intrinsic_name(*self))
646+
}
647+
648+
/// Returns whether the intrinsic has no meaningful body and all backends
649+
/// need to shim all calls to it.
650+
pub fn must_be_overridden(&self) -> bool {
651+
with(|cx| cx.intrinsic_must_be_overridden(*self))
652+
}
653+
}
654+
655+
impl From<IntrinsicDef> for FnDef {
656+
fn from(def: IntrinsicDef) -> Self {
657+
FnDef(def.0)
658+
}
624659
}
625660

626661
crate_def! {

library/proc_macro/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,18 @@ pub enum Delimiter {
815815
/// "macro variable" `$var`. It is important to preserve operator priorities in cases like
816816
/// `$var * 3` where `$var` is `1 + 2`.
817817
/// Invisible delimiters might not survive roundtrip of a token stream through a string.
818+
///
819+
/// <div class="warning">
820+
///
821+
/// Note: rustc currently can ignore the grouping of tokens delimited by `None` in the output
822+
/// of a proc_macro. Only `None`-delimited groups created by a macro_rules macro in the input
823+
/// of a proc_macro macro are preserved, and only in very specific circumstances.
824+
/// Any `None`-delimited groups (re)created by a proc_macro will therefore not preserve
825+
/// operator priorities as indicated above. The other `Delimiter` variants should be used
826+
/// instead in this context. This is a rustc bug. For details, see
827+
/// [rust-lang/rust#67062](https://github.com/rust-lang/rust/issues/67062).
828+
///
829+
/// </div>
818830
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
819831
None,
820832
}

src/tools/tidy/src/allowed_run_make_makefiles.txt

-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ run-make/issue-25581/Makefile
101101
run-make/issue-26006/Makefile
102102
run-make/issue-26092/Makefile
103103
run-make/issue-28595/Makefile
104-
run-make/issue-30063/Makefile
105104
run-make/issue-33329/Makefile
106105
run-make/issue-35164/Makefile
107106
run-make/issue-36710/Makefile
@@ -111,7 +110,6 @@ run-make/issue-40535/Makefile
111110
run-make/issue-47384/Makefile
112111
run-make/issue-47551/Makefile
113112
run-make/issue-51671/Makefile
114-
run-make/issue-53964/Makefile
115113
run-make/issue-64153/Makefile
116114
run-make/issue-68794-textrel-on-minimal-lib/Makefile
117115
run-make/issue-69368/Makefile
@@ -229,7 +227,6 @@ run-make/rlib-format-packed-bundled-libs/Makefile
229227
run-make/rmeta-preferred/Makefile
230228
run-make/rustc-macro-dep-files/Makefile
231229
run-make/rustdoc-io-error/Makefile
232-
run-make/rustdoc-scrape-examples-macros/Makefile
233230
run-make/rustdoc-verify-output-files/Makefile
234231
run-make/rustdoc-with-output-option/Makefile
235232
run-make/rustdoc-with-short-out-dir-option/Makefile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Defining a crate that provides panic handling as an external crate
2+
// could uselessly trigger the "unused external crate" lint. In this test,
3+
// if the lint is triggered, it will trip #![deny(unused_extern_crates)],
4+
// and cause the test to fail.
5+
// See https://github.com/rust-lang/rust/issues/53964
6+
7+
use run_make_support::{rustc, tmp_dir};
8+
9+
fn main() {
10+
rustc().input("panic.rs").run();
11+
rustc().input("app.rs").panic("abort").emit("obj").library_search_path(tmp_dir()).run();
12+
}

tests/run-make/issue-30063/Makefile

-36
This file was deleted.

tests/run-make/issue-53964/Makefile

-5
This file was deleted.
File renamed without changes.
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// When rustc received 4 codegen-units, an output path and an emit flag all simultaneously,
2+
// this could cause an annoying recompilation issue, uselessly lengthening the build process.
3+
// A fix was delivered, which resets codegen-units to 1 when necessary,
4+
// but as it directly affected the way codegen-units are manipulated,
5+
// this test was created to check that this fix did not cause compilation failures.
6+
// See https://github.com/rust-lang/rust/issues/30063
7+
8+
//@ ignore-cross-compile
9+
10+
use run_make_support::{rustc, tmp_dir};
11+
use std::fs;
12+
13+
fn compile(output_file: &str, emit: Option<&str>) {
14+
let mut rustc = rustc();
15+
let rustc = rustc.codegen_units(4).output(tmp_dir().join(output_file)).input("foo.rs");
16+
if let Some(emit) = emit {
17+
rustc.emit(emit);
18+
}
19+
rustc.run();
20+
}
21+
22+
fn main() {
23+
let flags = [
24+
("foo-output", None),
25+
("asm-output", Some("asm")),
26+
("bc-output", Some("llvm-bc")),
27+
("ir-output", Some("llvm-ir")),
28+
("link-output", Some("link")),
29+
("obj-output", Some("obj")),
30+
("dep-output", Some("dep-info")),
31+
("multi-output", Some("asm,obj")),
32+
];
33+
for (output_file, emit) in flags {
34+
fs::remove_file(output_file).unwrap_or_default();
35+
compile(output_file, emit);
36+
fs::remove_file(output_file);
37+
}
38+
}

0 commit comments

Comments
 (0)