Skip to content

Commit 2947be7

Browse files
committed
Auto merge of #138714 - matthiaskrgr:rollup-8uwbpwv, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #135394 (`MaybeUninit` inherent slice methods part 2) - #137051 (Implement default methods for `io::Empty` and `io::Sink`) - #138001 (mir_build: consider privacy when checking for irrefutable patterns) - #138540 (core/slice: Mark some `split_off` variants unstably const) - #138589 (If a label is placed on the block of a loop instead of the header, suggest moving it to the header.) - #138594 (Fix next solver handling of shallow trait impl check) - #138613 (Remove E0773 "A builtin-macro was defined more than once.") Failed merges: - #138602 (Slim `rustc_parse_format` dependencies down) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 1aeb99d + 966021d commit 2947be7

Some content is hidden

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

49 files changed

+988
-435
lines changed

compiler/rustc_builtin_macros/src/lib.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
extern crate proc_macro;
2525

26+
use std::sync::Arc;
27+
2628
use rustc_expand::base::{MacroExpanderFn, ResolverExpand, SyntaxExtensionKind};
2729
use rustc_expand::proc_macro::BangProcMacro;
2830
use rustc_span::sym;
@@ -67,13 +69,13 @@ rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
6769
pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
6870
let mut register = |name, kind| resolver.register_builtin_macro(name, kind);
6971
macro register_bang($($name:ident: $f:expr,)*) {
70-
$(register(sym::$name, SyntaxExtensionKind::LegacyBang(Box::new($f as MacroExpanderFn)));)*
72+
$(register(sym::$name, SyntaxExtensionKind::LegacyBang(Arc::new($f as MacroExpanderFn)));)*
7173
}
7274
macro register_attr($($name:ident: $f:expr,)*) {
73-
$(register(sym::$name, SyntaxExtensionKind::LegacyAttr(Box::new($f)));)*
75+
$(register(sym::$name, SyntaxExtensionKind::LegacyAttr(Arc::new($f)));)*
7476
}
7577
macro register_derive($($name:ident: $f:expr,)*) {
76-
$(register(sym::$name, SyntaxExtensionKind::LegacyDerive(Box::new(BuiltinDerive($f))));)*
78+
$(register(sym::$name, SyntaxExtensionKind::LegacyDerive(Arc::new(BuiltinDerive($f))));)*
7779
}
7880

7981
register_bang! {
@@ -139,9 +141,9 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
139141
}
140142

141143
let client = proc_macro::bridge::client::Client::expand1(proc_macro::quote);
142-
register(sym::quote, SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client })));
143-
let requires = SyntaxExtensionKind::Attr(Box::new(contracts::ExpandRequires));
144+
register(sym::quote, SyntaxExtensionKind::Bang(Arc::new(BangProcMacro { client })));
145+
let requires = SyntaxExtensionKind::Attr(Arc::new(contracts::ExpandRequires));
144146
register(sym::contracts_requires, requires);
145-
let ensures = SyntaxExtensionKind::Attr(Box::new(contracts::ExpandEnsures));
147+
let ensures = SyntaxExtensionKind::Attr(Arc::new(contracts::ExpandEnsures));
146148
register(sym::contracts_ensures, ensures);
147149
}
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,4 @@
1-
A builtin-macro was defined more than once.
1+
#### this error code is no longer emitted by the compiler.
22

3-
Erroneous code example:
4-
5-
```compile_fail,E0773
6-
#![feature(decl_macro)]
7-
#![feature(rustc_attrs)]
8-
#![allow(internal_features)]
9-
10-
#[rustc_builtin_macro]
11-
pub macro test($item:item) {
12-
/* compiler built-in */
13-
}
14-
15-
mod inner {
16-
#[rustc_builtin_macro]
17-
pub macro test($item:item) {
18-
/* compiler built-in */
19-
}
20-
}
21-
```
22-
23-
To fix the issue, remove the duplicate declaration:
24-
25-
```
26-
#![feature(decl_macro)]
27-
#![feature(rustc_attrs)]
28-
#![allow(internal_features)]
29-
30-
#[rustc_builtin_macro]
31-
pub macro test($item:item) {
32-
/* compiler built-in */
33-
}
34-
```
35-
36-
In very rare edge cases, this may happen when loading `core` or `std` twice,
37-
once with `check` metadata and once with `build` metadata.
38-
For more information, see [#75176].
39-
40-
[#75176]: https://github.com/rust-lang/rust/pull/75176#issuecomment-683234468
3+
This was triggered when multiple macro definitions used the same
4+
`#[rustc_builtin_macro(..)]`. This is no longer an error.

compiler/rustc_expand/src/base.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -681,33 +681,34 @@ impl MacResult for DummyResult {
681681
}
682682

683683
/// A syntax extension kind.
684+
#[derive(Clone)]
684685
pub enum SyntaxExtensionKind {
685686
/// A token-based function-like macro.
686687
Bang(
687688
/// An expander with signature TokenStream -> TokenStream.
688-
Box<dyn BangProcMacro + sync::DynSync + sync::DynSend>,
689+
Arc<dyn BangProcMacro + sync::DynSync + sync::DynSend>,
689690
),
690691

691692
/// An AST-based function-like macro.
692693
LegacyBang(
693694
/// An expander with signature TokenStream -> AST.
694-
Box<dyn TTMacroExpander + sync::DynSync + sync::DynSend>,
695+
Arc<dyn TTMacroExpander + sync::DynSync + sync::DynSend>,
695696
),
696697

697698
/// A token-based attribute macro.
698699
Attr(
699700
/// An expander with signature (TokenStream, TokenStream) -> TokenStream.
700701
/// The first TokenStream is the attribute itself, the second is the annotated item.
701702
/// The produced TokenStream replaces the input TokenStream.
702-
Box<dyn AttrProcMacro + sync::DynSync + sync::DynSend>,
703+
Arc<dyn AttrProcMacro + sync::DynSync + sync::DynSend>,
703704
),
704705

705706
/// An AST-based attribute macro.
706707
LegacyAttr(
707708
/// An expander with signature (AST, AST) -> AST.
708709
/// The first AST fragment is the attribute itself, the second is the annotated item.
709710
/// The produced AST fragment replaces the input AST fragment.
710-
Box<dyn MultiItemModifier + sync::DynSync + sync::DynSend>,
711+
Arc<dyn MultiItemModifier + sync::DynSync + sync::DynSend>,
711712
),
712713

713714
/// A trivial attribute "macro" that does nothing,
@@ -724,18 +725,18 @@ pub enum SyntaxExtensionKind {
724725
/// is handled identically to `LegacyDerive`. It should be migrated to
725726
/// a token-based representation like `Bang` and `Attr`, instead of
726727
/// using `MultiItemModifier`.
727-
Box<dyn MultiItemModifier + sync::DynSync + sync::DynSend>,
728+
Arc<dyn MultiItemModifier + sync::DynSync + sync::DynSend>,
728729
),
729730

730731
/// An AST-based derive macro.
731732
LegacyDerive(
732733
/// An expander with signature AST -> AST.
733734
/// The produced AST fragment is appended to the input AST fragment.
734-
Box<dyn MultiItemModifier + sync::DynSync + sync::DynSend>,
735+
Arc<dyn MultiItemModifier + sync::DynSync + sync::DynSend>,
735736
),
736737

737738
/// A glob delegation.
738-
GlobDelegation(Box<dyn GlobDelegationExpander + sync::DynSync + sync::DynSend>),
739+
GlobDelegation(Arc<dyn GlobDelegationExpander + sync::DynSync + sync::DynSend>),
739740
}
740741

741742
/// A struct representing a macro definition in "lowered" form ready for expansion.
@@ -937,7 +938,7 @@ impl SyntaxExtension {
937938
cx.dcx().span_delayed_bug(span, "expanded a dummy bang macro"),
938939
))
939940
}
940-
SyntaxExtension::default(SyntaxExtensionKind::LegacyBang(Box::new(expander)), edition)
941+
SyntaxExtension::default(SyntaxExtensionKind::LegacyBang(Arc::new(expander)), edition)
941942
}
942943

943944
/// A dummy derive macro `#[derive(Foo)]`.
@@ -950,7 +951,7 @@ impl SyntaxExtension {
950951
) -> Vec<Annotatable> {
951952
Vec::new()
952953
}
953-
SyntaxExtension::default(SyntaxExtensionKind::Derive(Box::new(expander)), edition)
954+
SyntaxExtension::default(SyntaxExtensionKind::Derive(Arc::new(expander)), edition)
954955
}
955956

956957
pub fn non_macro_attr(edition: Edition) -> SyntaxExtension {
@@ -980,7 +981,7 @@ impl SyntaxExtension {
980981
}
981982

982983
let expander = GlobDelegationExpanderImpl { trait_def_id, impl_def_id };
983-
SyntaxExtension::default(SyntaxExtensionKind::GlobDelegation(Box::new(expander)), edition)
984+
SyntaxExtension::default(SyntaxExtensionKind::GlobDelegation(Arc::new(expander)), edition)
984985
}
985986

986987
pub fn expn_data(

compiler/rustc_expand/src/mbe/macro_rules.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::borrow::Cow;
22
use std::collections::hash_map::Entry;
3+
use std::sync::Arc;
34
use std::{mem, slice};
45

56
use ast::token::IdentIsRaw;
@@ -388,7 +389,7 @@ pub fn compile_declarative_macro(
388389
node_id != DUMMY_NODE_ID,
389390
)
390391
};
391-
let dummy_syn_ext = |guar| (mk_syn_ext(Box::new(DummyExpander(guar))), Vec::new());
392+
let dummy_syn_ext = |guar| (mk_syn_ext(Arc::new(DummyExpander(guar))), Vec::new());
392393

393394
let lhs_nm = Ident::new(sym::lhs, span);
394395
let rhs_nm = Ident::new(sym::rhs, span);
@@ -582,7 +583,7 @@ pub fn compile_declarative_macro(
582583
})
583584
.collect();
584585

585-
let expander = Box::new(MacroRulesMacroExpander {
586+
let expander = Arc::new(MacroRulesMacroExpander {
586587
name: ident,
587588
span,
588589
node_id,

compiler/rustc_hir_typeck/src/cast.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ use rustc_middle::ty::error::TypeError;
4343
use rustc_middle::ty::{self, Ty, TyCtxt, TypeAndMut, TypeVisitableExt, VariantDef, elaborate};
4444
use rustc_middle::{bug, span_bug};
4545
use rustc_session::lint;
46-
use rustc_span::def_id::LOCAL_CRATE;
4746
use rustc_span::{DUMMY_SP, Span, sym};
4847
use rustc_trait_selection::infer::InferCtxtExt;
4948
use tracing::{debug, instrument};
@@ -805,7 +804,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
805804
_ => return Err(CastError::NonScalar),
806805
};
807806
if let ty::Adt(adt_def, _) = *self.expr_ty.kind()
808-
&& adt_def.did().krate != LOCAL_CRATE
807+
&& !adt_def.did().is_local()
809808
&& adt_def.variants().iter().any(VariantDef::is_field_list_non_exhaustive)
810809
{
811810
return Err(CastError::ForeignNonExhaustiveAdt);

compiler/rustc_hir_typeck/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1977,7 +1977,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19771977

19781978
// Prohibit struct expressions when non-exhaustive flag is set.
19791979
let adt = adt_ty.ty_adt_def().expect("`check_struct_path` returned non-ADT type");
1980-
if !adt.did().is_local() && variant.is_field_list_non_exhaustive() {
1980+
if variant.field_list_has_applicable_non_exhaustive() {
19811981
self.dcx()
19821982
.emit_err(StructExprNonExhaustive { span: expr.span, what: adt.variant_descr() });
19831983
}

compiler/rustc_hir_typeck/src/expr_use_visitor.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_middle::hir::place::ProjectionKind;
2020
pub use rustc_middle::hir::place::{Place, PlaceBase, PlaceWithHirId, Projection};
2121
use rustc_middle::mir::FakeReadCause;
2222
use rustc_middle::ty::{
23-
self, AdtKind, BorrowKind, Ty, TyCtxt, TypeFoldable, TypeVisitableExt as _, adjustment,
23+
self, BorrowKind, Ty, TyCtxt, TypeFoldable, TypeVisitableExt as _, adjustment,
2424
};
2525
use rustc_middle::{bug, span_bug};
2626
use rustc_span::{ErrorGuaranteed, Span};
@@ -1899,14 +1899,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
18991899
// to assume that more cases will be added to the variant in the future. This mean
19001900
// that we should handle non-exhaustive SingleVariant the same way we would handle
19011901
// a MultiVariant.
1902-
// If the variant is not local it must be defined in another crate.
1903-
let is_non_exhaustive = match def.adt_kind() {
1904-
AdtKind::Struct | AdtKind::Union => {
1905-
def.non_enum_variant().is_field_list_non_exhaustive()
1906-
}
1907-
AdtKind::Enum => def.is_variant_list_non_exhaustive(),
1908-
};
1909-
def.variants().len() > 1 || (!def.did().is_local() && is_non_exhaustive)
1902+
def.variants().len() > 1 || def.variant_list_has_applicable_non_exhaustive()
19101903
} else {
19111904
false
19121905
}

compiler/rustc_hir_typeck/src/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1724,7 +1724,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17241724
};
17251725

17261726
// Require `..` if struct has non_exhaustive attribute.
1727-
let non_exhaustive = variant.is_field_list_non_exhaustive() && !adt.did().is_local();
1727+
let non_exhaustive = variant.field_list_has_applicable_non_exhaustive();
17281728
if non_exhaustive && !has_rest_pat {
17291729
self.error_foreign_non_exhaustive_spat(pat, adt.variant_descr(), fields.is_empty());
17301730
}

compiler/rustc_lint/src/types.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1193,9 +1193,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
11931193
};
11941194
}
11951195

1196-
let is_non_exhaustive =
1197-
def.non_enum_variant().is_field_list_non_exhaustive();
1198-
if is_non_exhaustive && !def.did().is_local() {
1196+
if def.non_enum_variant().field_list_has_applicable_non_exhaustive() {
11991197
return FfiUnsafe {
12001198
ty,
12011199
reason: if def.is_struct() {
@@ -1248,14 +1246,12 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
12481246
};
12491247
}
12501248

1251-
use improper_ctypes::{
1252-
check_non_exhaustive_variant, non_local_and_non_exhaustive,
1253-
};
1249+
use improper_ctypes::check_non_exhaustive_variant;
12541250

1255-
let non_local_def = non_local_and_non_exhaustive(def);
1251+
let non_exhaustive = def.variant_list_has_applicable_non_exhaustive();
12561252
// Check the contained variants.
12571253
let ret = def.variants().iter().try_for_each(|variant| {
1258-
check_non_exhaustive_variant(non_local_def, variant)
1254+
check_non_exhaustive_variant(non_exhaustive, variant)
12591255
.map_break(|reason| FfiUnsafe { ty, reason, help: None })?;
12601256

12611257
match self.check_variant_for_ffi(acc, ty, def, variant, args) {

compiler/rustc_lint/src/types/improper_ctypes.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ use crate::fluent_generated as fluent;
1515
/// so we don't need the lint to account for it.
1616
/// e.g. going from enum Foo { A, B, C } to enum Foo { A, B, C, D(u32) }.
1717
pub(crate) fn check_non_exhaustive_variant(
18-
non_local_def: bool,
18+
non_exhaustive_variant_list: bool,
1919
variant: &ty::VariantDef,
2020
) -> ControlFlow<DiagMessage, ()> {
2121
// non_exhaustive suggests it is possible that someone might break ABI
2222
// see: https://github.com/rust-lang/rust/issues/44109#issuecomment-537583344
2323
// so warn on complex enums being used outside their crate
24-
if non_local_def {
24+
if non_exhaustive_variant_list {
2525
// which is why we only warn about really_tagged_union reprs from https://rust.tf/rfc2195
2626
// with an enum like `#[repr(u8)] enum Enum { A(DataA), B(DataB), }`
2727
// but exempt enums with unit ctors like C's (e.g. from rust-bindgen)
@@ -30,8 +30,7 @@ pub(crate) fn check_non_exhaustive_variant(
3030
}
3131
}
3232

33-
let non_exhaustive_variant_fields = variant.is_field_list_non_exhaustive();
34-
if non_exhaustive_variant_fields && !variant.def_id.is_local() {
33+
if variant.field_list_has_applicable_non_exhaustive() {
3534
return ControlFlow::Break(fluent::lint_improper_ctypes_non_exhaustive_variant);
3635
}
3736

@@ -42,10 +41,3 @@ fn variant_has_complex_ctor(variant: &ty::VariantDef) -> bool {
4241
// CtorKind::Const means a "unit" ctor
4342
!matches!(variant.ctor_kind(), Some(CtorKind::Const))
4443
}
45-
46-
// non_exhaustive suggests it is possible that someone might break ABI
47-
// see: https://github.com/rust-lang/rust/issues/44109#issuecomment-537583344
48-
// so warn on complex enums being used outside their crate
49-
pub(crate) fn non_local_and_non_exhaustive(def: ty::AdtDef<'_>) -> bool {
50-
def.is_variant_list_non_exhaustive() && !def.did().is_local()
51-
}

compiler/rustc_metadata/src/rmeta/decoder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1053,15 +1053,15 @@ impl<'a> CrateMetadataRef<'a> {
10531053
attributes.iter().cloned().map(Symbol::intern).collect::<Vec<_>>();
10541054
(
10551055
trait_name,
1056-
SyntaxExtensionKind::Derive(Box::new(DeriveProcMacro { client })),
1056+
SyntaxExtensionKind::Derive(Arc::new(DeriveProcMacro { client })),
10571057
helper_attrs,
10581058
)
10591059
}
10601060
ProcMacro::Attr { name, client } => {
1061-
(name, SyntaxExtensionKind::Attr(Box::new(AttrProcMacro { client })), Vec::new())
1061+
(name, SyntaxExtensionKind::Attr(Arc::new(AttrProcMacro { client })), Vec::new())
10621062
}
10631063
ProcMacro::Bang { name, client } => {
1064-
(name, SyntaxExtensionKind::Bang(Box::new(BangProcMacro { client })), Vec::new())
1064+
(name, SyntaxExtensionKind::Bang(Arc::new(BangProcMacro { client })), Vec::new())
10651065
}
10661066
};
10671067

compiler/rustc_middle/src/ty/adt.rs

+11
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,22 @@ impl<'tcx> AdtDef<'tcx> {
327327
}
328328

329329
/// Returns `true` if the variant list of this ADT is `#[non_exhaustive]`.
330+
///
331+
/// Note that this function will return `true` even if the ADT has been
332+
/// defined in the crate currently being compiled. If that's not what you
333+
/// want, see [`Self::variant_list_has_applicable_non_exhaustive`].
330334
#[inline]
331335
pub fn is_variant_list_non_exhaustive(self) -> bool {
332336
self.flags().contains(AdtFlags::IS_VARIANT_LIST_NON_EXHAUSTIVE)
333337
}
334338

339+
/// Returns `true` if the variant list of this ADT is `#[non_exhaustive]`
340+
/// and has been defined in another crate.
341+
#[inline]
342+
pub fn variant_list_has_applicable_non_exhaustive(self) -> bool {
343+
self.is_variant_list_non_exhaustive() && !self.did().is_local()
344+
}
345+
335346
/// Returns the kind of the ADT.
336347
#[inline]
337348
pub fn adt_kind(self) -> AdtKind {

compiler/rustc_middle/src/ty/inhabitedness/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<'tcx> Ty<'tcx> {
107107
// For now, unions are always considered inhabited
108108
Adt(adt, _) if adt.is_union() => InhabitedPredicate::True,
109109
// Non-exhaustive ADTs from other crates are always considered inhabited
110-
Adt(adt, _) if adt.is_variant_list_non_exhaustive() && !adt.did().is_local() => {
110+
Adt(adt, _) if adt.variant_list_has_applicable_non_exhaustive() => {
111111
InhabitedPredicate::True
112112
}
113113
Never => InhabitedPredicate::False,

compiler/rustc_middle/src/ty/mod.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1208,12 +1208,23 @@ impl VariantDef {
12081208
}
12091209
}
12101210

1211-
/// Is this field list non-exhaustive?
1211+
/// Returns `true` if the field list of this variant is `#[non_exhaustive]`.
1212+
///
1213+
/// Note that this function will return `true` even if the type has been
1214+
/// defined in the crate currently being compiled. If that's not what you
1215+
/// want, see [`Self::field_list_has_applicable_non_exhaustive`].
12121216
#[inline]
12131217
pub fn is_field_list_non_exhaustive(&self) -> bool {
12141218
self.flags.intersects(VariantFlags::IS_FIELD_LIST_NON_EXHAUSTIVE)
12151219
}
12161220

1221+
/// Returns `true` if the field list of this variant is `#[non_exhaustive]`
1222+
/// and the type has been defined in another crate.
1223+
#[inline]
1224+
pub fn field_list_has_applicable_non_exhaustive(&self) -> bool {
1225+
self.is_field_list_non_exhaustive() && !self.def_id.is_local()
1226+
}
1227+
12171228
/// Computes the `Ident` of this variant by looking up the `Span`
12181229
pub fn ident(&self, tcx: TyCtxt<'_>) -> Ident {
12191230
Ident::new(self.name, tcx.def_ident_span(self.def_id).unwrap())

0 commit comments

Comments
 (0)