Skip to content

Commit acf2315

Browse files
authored
Rollup merge of rust-lang#63462 - matthewjasper:hygienic-builtin-derives, r=petrochenkov
Opaque builtin derive macros * Buiilt-in derives are now opaque macros * This required limiting the visibility of some previously unexposed functions in `core`. * This also required the change to `Ident` serialization. * All gensyms are replaced with hygienic identifiers * Use hygiene to avoid most other name-resolution issues with buiilt-in derives. * As far as I know the only remaining case that breaks is an ADT that has the same name as one of its parameters. Fixing this completely seemed to be more effort than it's worth. * Remove gensym in `Ident::decode`, which lead to linker errors due to `inline` being gensymmed. * `Ident`now panics if incremental compilation tries to serialize it (it currently doesn't). * `Ident` no longer uses `gensym` to emulate cross-crate hygiene. It only applied to reexports. * `SyntaxContext` is no longer serializable. * The long-term fix for this is to properly implement cross-crate hygiene, but this seemed to be acceptable for now. * Move type/const parameter shadowing checks to `resolve` * This was previously split between resolve and type checking. The type checking pass compared `InternedString`s, not Identifiers. * Removed the `SyntaxContext` from `{ast, hir}::{InlineAsm, GlobalAsm}` cc rust-lang#60869 r? @petrochenkov
2 parents f7af19c + 01587b1 commit acf2315

Some content is hidden

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

48 files changed

+361
-272
lines changed

src/libcore/clone.rs

-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ pub trait Clone : Sized {
135135

136136
/// Derive macro generating an impl of the trait `Clone`.
137137
#[rustc_builtin_macro]
138-
#[rustc_macro_transparency = "semitransparent"]
139138
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
140139
#[allow_internal_unstable(core_intrinsics, derive_clone_copy)]
141140
pub macro Clone($item:item) { /* compiler built-in */ }

src/libcore/cmp.rs

-4
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ pub trait PartialEq<Rhs: ?Sized = Self> {
202202

203203
/// Derive macro generating an impl of the trait `PartialEq`.
204204
#[rustc_builtin_macro]
205-
#[rustc_macro_transparency = "semitransparent"]
206205
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
207206
#[allow_internal_unstable(core_intrinsics)]
208207
pub macro PartialEq($item:item) { /* compiler built-in */ }
@@ -265,7 +264,6 @@ pub trait Eq: PartialEq<Self> {
265264

266265
/// Derive macro generating an impl of the trait `Eq`.
267266
#[rustc_builtin_macro]
268-
#[rustc_macro_transparency = "semitransparent"]
269267
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
270268
#[allow_internal_unstable(core_intrinsics, derive_eq)]
271269
pub macro Eq($item:item) { /* compiler built-in */ }
@@ -616,7 +614,6 @@ pub trait Ord: Eq + PartialOrd<Self> {
616614

617615
/// Derive macro generating an impl of the trait `Ord`.
618616
#[rustc_builtin_macro]
619-
#[rustc_macro_transparency = "semitransparent"]
620617
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
621618
#[allow_internal_unstable(core_intrinsics)]
622619
pub macro Ord($item:item) { /* compiler built-in */ }
@@ -865,7 +862,6 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
865862

866863
/// Derive macro generating an impl of the trait `PartialOrd`.
867864
#[rustc_builtin_macro]
868-
#[rustc_macro_transparency = "semitransparent"]
869865
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
870866
#[allow_internal_unstable(core_intrinsics)]
871867
pub macro PartialOrd($item:item) { /* compiler built-in */ }

src/libcore/default.rs

-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ pub trait Default: Sized {
117117

118118
/// Derive macro generating an impl of the trait `Default`.
119119
#[rustc_builtin_macro]
120-
#[rustc_macro_transparency = "semitransparent"]
121120
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
122121
#[allow_internal_unstable(core_intrinsics)]
123122
pub macro Default($item:item) { /* compiler built-in */ }

src/libcore/fmt/builders.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub struct DebugStruct<'a, 'b: 'a> {
9898
has_fields: bool,
9999
}
100100

101-
pub fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>,
101+
pub(super) fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>,
102102
name: &str)
103103
-> DebugStruct<'a, 'b> {
104104
let result = fmt.write_str(name);
@@ -251,7 +251,10 @@ pub struct DebugTuple<'a, 'b: 'a> {
251251
empty_name: bool,
252252
}
253253

254-
pub fn debug_tuple_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> DebugTuple<'a, 'b> {
254+
pub(super) fn debug_tuple_new<'a, 'b>(
255+
fmt: &'a mut fmt::Formatter<'b>,
256+
name: &str,
257+
) -> DebugTuple<'a, 'b> {
255258
let result = fmt.write_str(name);
256259
DebugTuple {
257260
fmt,
@@ -418,7 +421,7 @@ pub struct DebugSet<'a, 'b: 'a> {
418421
inner: DebugInner<'a, 'b>,
419422
}
420423

421-
pub fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugSet<'a, 'b> {
424+
pub(super) fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugSet<'a, 'b> {
422425
let result = fmt.write_str("{");
423426
DebugSet {
424427
inner: DebugInner {
@@ -555,7 +558,7 @@ pub struct DebugList<'a, 'b: 'a> {
555558
inner: DebugInner<'a, 'b>,
556559
}
557560

558-
pub fn debug_list_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugList<'a, 'b> {
561+
pub(super) fn debug_list_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugList<'a, 'b> {
559562
let result = fmt.write_str("[");
560563
DebugList {
561564
inner: DebugInner {
@@ -697,7 +700,7 @@ pub struct DebugMap<'a, 'b: 'a> {
697700
state: PadAdapterState,
698701
}
699702

700-
pub fn debug_map_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugMap<'a, 'b> {
703+
pub(super) fn debug_map_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugMap<'a, 'b> {
701704
let result = fmt.write_str("{");
702705
DebugMap {
703706
fmt,

src/libcore/fmt/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,6 @@ pub trait Debug {
549549
pub(crate) mod macros {
550550
/// Derive macro generating an impl of the trait `Debug`.
551551
#[rustc_builtin_macro]
552-
#[rustc_macro_transparency = "semitransparent"]
553552
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
554553
#[allow_internal_unstable(core_intrinsics)]
555554
pub macro Debug($item:item) { /* compiler built-in */ }

src/libcore/hash/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ pub trait Hash {
202202
pub(crate) mod macros {
203203
/// Derive macro generating an impl of the trait `Hash`.
204204
#[rustc_builtin_macro]
205-
#[rustc_macro_transparency = "semitransparent"]
206205
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
207206
#[allow_internal_unstable(core_intrinsics)]
208207
pub macro Hash($item:item) { /* compiler built-in */ }

src/libcore/macros.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1270,14 +1270,12 @@ pub(crate) mod builtin {
12701270

12711271
/// Unstable implementation detail of the `rustc` compiler, do not use.
12721272
#[rustc_builtin_macro]
1273-
#[rustc_macro_transparency = "semitransparent"]
12741273
#[stable(feature = "rust1", since = "1.0.0")]
12751274
#[allow_internal_unstable(core_intrinsics, libstd_sys_internals)]
12761275
pub macro RustcDecodable($item:item) { /* compiler built-in */ }
12771276

12781277
/// Unstable implementation detail of the `rustc` compiler, do not use.
12791278
#[rustc_builtin_macro]
1280-
#[rustc_macro_transparency = "semitransparent"]
12811279
#[stable(feature = "rust1", since = "1.0.0")]
12821280
#[allow_internal_unstable(core_intrinsics)]
12831281
pub macro RustcEncodable($item:item) { /* compiler built-in */ }

src/libcore/marker.rs

-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ pub trait Copy : Clone {
290290

291291
/// Derive macro generating an impl of the trait `Copy`.
292292
#[rustc_builtin_macro]
293-
#[rustc_macro_transparency = "semitransparent"]
294293
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
295294
#[allow_internal_unstable(core_intrinsics, derive_clone_copy)]
296295
pub macro Copy($item:item) { /* compiler built-in */ }

src/librustc/hir/lowering/expr.rs

-1
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,6 @@ impl LoweringContext<'_> {
984984
volatile: asm.volatile,
985985
alignstack: asm.alignstack,
986986
dialect: asm.dialect,
987-
ctxt: asm.ctxt,
988987
};
989988

990989
let outputs = asm.outputs

src/librustc/hir/lowering/item.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -750,10 +750,7 @@ impl LoweringContext<'_> {
750750
}
751751

752752
fn lower_global_asm(&mut self, ga: &GlobalAsm) -> P<hir::GlobalAsm> {
753-
P(hir::GlobalAsm {
754-
asm: ga.asm,
755-
ctxt: ga.ctxt,
756-
})
753+
P(hir::GlobalAsm { asm: ga.asm })
757754
}
758755

759756
fn lower_variant(&mut self, v: &Variant) -> hir::Variant {

src/librustc/hir/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use rustc_target::spec::abi::Abi;
2323
use syntax::ast::{self, CrateSugar, Ident, Name, NodeId, AsmDialect};
2424
use syntax::ast::{Attribute, Label, LitKind, StrStyle, FloatTy, IntTy, UintTy};
2525
use syntax::attr::{InlineAttr, OptimizeAttr};
26-
use syntax::ext::hygiene::SyntaxContext;
2726
use syntax::symbol::{Symbol, kw};
2827
use syntax::tokenstream::TokenStream;
2928
use syntax::util::parser::ExprPrecedence;
@@ -2004,8 +2003,6 @@ pub struct InlineAsm {
20042003
pub volatile: bool,
20052004
pub alignstack: bool,
20062005
pub dialect: AsmDialect,
2007-
#[stable_hasher(ignore)] // This is used for error reporting
2008-
pub ctxt: SyntaxContext,
20092006
}
20102007

20112008
/// Represents an argument in a function header.
@@ -2184,8 +2181,6 @@ pub struct ForeignMod {
21842181
#[derive(RustcEncodable, RustcDecodable, Debug, HashStable)]
21852182
pub struct GlobalAsm {
21862183
pub asm: Symbol,
2187-
#[stable_hasher(ignore)] // This is used for error reporting
2188-
pub ctxt: SyntaxContext,
21892184
}
21902185

21912186
#[derive(RustcEncodable, RustcDecodable, Debug, HashStable)]

src/librustc_codegen_llvm/asm.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use crate::value::Value;
66

77
use rustc::hir;
88
use rustc_codegen_ssa::traits::*;
9-
109
use rustc_codegen_ssa::mir::place::PlaceRef;
1110
use rustc_codegen_ssa::mir::operand::OperandValue;
11+
use syntax_pos::Span;
1212

1313
use std::ffi::{CStr, CString};
1414
use libc::{c_uint, c_char};
@@ -19,7 +19,8 @@ impl AsmBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
1919
&mut self,
2020
ia: &hir::InlineAsm,
2121
outputs: Vec<PlaceRef<'tcx, &'ll Value>>,
22-
mut inputs: Vec<&'ll Value>
22+
mut inputs: Vec<&'ll Value>,
23+
span: Span,
2324
) -> bool {
2425
let mut ext_constraints = vec![];
2526
let mut output_types = vec![];
@@ -102,7 +103,7 @@ impl AsmBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
102103
let kind = llvm::LLVMGetMDKindIDInContext(self.llcx,
103104
key.as_ptr() as *const c_char, key.len() as c_uint);
104105

105-
let val: &'ll Value = self.const_i32(ia.ctxt.outer_expn().as_u32() as i32);
106+
let val: &'ll Value = self.const_i32(span.ctxt().outer_expn().as_u32() as i32);
106107

107108
llvm::LLVMSetMetadata(r, kind,
108109
llvm::LLVMMDNodeInContext(self.llcx, &val, 1));

src/librustc_codegen_ssa/mir/statement.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
8989
});
9090

9191
if input_vals.len() == asm.inputs.len() {
92-
let res = bx.codegen_inline_asm(&asm.asm, outputs, input_vals);
92+
let res = bx.codegen_inline_asm(
93+
&asm.asm,
94+
outputs,
95+
input_vals,
96+
statement.source_info.span,
97+
);
9398
if !res {
9499
span_err!(bx.sess(), statement.source_info.span, E0668,
95100
"malformed inline assembly");

src/librustc_codegen_ssa/traits/asm.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::BackendTypes;
22
use crate::mir::place::PlaceRef;
33
use rustc::hir::{GlobalAsm, InlineAsm};
4+
use syntax_pos::Span;
45

56
pub trait AsmBuilderMethods<'tcx>: BackendTypes {
67
/// Take an inline assembly expression and splat it out via LLVM
@@ -9,6 +10,7 @@ pub trait AsmBuilderMethods<'tcx>: BackendTypes {
910
ia: &InlineAsm,
1011
outputs: Vec<PlaceRef<'tcx, Self::Value>>,
1112
inputs: Vec<Self::Value>,
13+
span: Span,
1214
) -> bool;
1315
}
1416

src/librustc_metadata/decoder.rs

+8
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,14 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
348348
}
349349
}
350350

351+
impl SpecializedDecoder<Ident> for DecodeContext<'_, '_> {
352+
fn specialized_decode(&mut self) -> Result<Ident, Self::Error> {
353+
// FIXME(jseyfried): intercrate hygiene
354+
355+
Ok(Ident::with_empty_ctxt(Symbol::decode(self)?))
356+
}
357+
}
358+
351359
impl<'a, 'tcx> SpecializedDecoder<Fingerprint> for DecodeContext<'a, 'tcx> {
352360
fn specialized_decode(&mut self) -> Result<Fingerprint, Self::Error> {
353361
Fingerprint::decode_opaque(&mut self.opaque)

src/librustc_metadata/encoder.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use std::u32;
3131
use syntax::ast;
3232
use syntax::attr;
3333
use syntax::source_map::Spanned;
34-
use syntax::symbol::{kw, sym};
34+
use syntax::symbol::{kw, sym, Ident};
3535
use syntax_pos::{self, FileName, SourceFile, Span};
3636
use log::{debug, trace};
3737

@@ -173,6 +173,13 @@ impl<'tcx> SpecializedEncoder<Span> for EncodeContext<'tcx> {
173173
}
174174
}
175175

176+
impl SpecializedEncoder<Ident> for EncodeContext<'tcx> {
177+
fn specialized_encode(&mut self, ident: &Ident) -> Result<(), Self::Error> {
178+
// FIXME(jseyfried): intercrate hygiene
179+
ident.name.encode(self)
180+
}
181+
}
182+
176183
impl<'tcx> SpecializedEncoder<LocalDefId> for EncodeContext<'tcx> {
177184
#[inline]
178185
fn specialized_encode(&mut self, def_id: &LocalDefId) -> Result<(), Self::Error> {

src/librustc_resolve/diagnostics.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,14 @@ impl<'a> Resolver<'a> {
166166
err
167167
}
168168
ResolutionError::NameAlreadyUsedInParameterList(name, first_use_span) => {
169-
let mut err = struct_span_err!(self.session,
170-
span,
171-
E0403,
172-
"the name `{}` is already used for a generic \
173-
parameter in this list of generic parameters",
174-
name);
169+
let mut err = struct_span_err!(
170+
self.session,
171+
span,
172+
E0403,
173+
"the name `{}` is already used for a generic \
174+
parameter in this item's generic parameters",
175+
name,
176+
);
175177
err.span_label(span, "already used");
176178
err.span_label(first_use_span, format!("first use of `{}`", name));
177179
err

src/librustc_resolve/error_codes.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -526,15 +526,25 @@ Some type parameters have the same name.
526526
Erroneous code example:
527527
528528
```compile_fail,E0403
529-
fn foo<T, T>(s: T, u: T) {} // error: the name `T` is already used for a type
530-
// parameter in this type parameter list
529+
fn f<T, T>(s: T, u: T) {} // error: the name `T` is already used for a generic
530+
// parameter in this item's generic parameters
531531
```
532532
533533
Please verify that none of the type parameters are misspelled, and rename any
534534
clashing parameters. Example:
535535
536536
```
537-
fn foo<T, Y>(s: T, u: Y) {} // ok!
537+
fn f<T, Y>(s: T, u: Y) {} // ok!
538+
```
539+
540+
Type parameters in an associated item also cannot shadow parameters from the
541+
containing item:
542+
543+
```compile_fail,E0403
544+
trait Foo<T> {
545+
fn do_something(&self) -> T;
546+
fn do_something_else<T: Clone>(&self, bar: T);
547+
}
538548
```
539549
"##,
540550

src/librustc_resolve/late.rs

+31
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,24 @@ crate enum RibKind<'a> {
104104
TyParamAsConstParamTy,
105105
}
106106

107+
impl RibKind<'_> {
108+
// Whether this rib kind contains generic parameters, as opposed to local
109+
// variables.
110+
crate fn contains_params(&self) -> bool {
111+
match self {
112+
NormalRibKind
113+
| FnItemRibKind
114+
| ConstantItemRibKind
115+
| ModuleRibKind(_)
116+
| MacroDefinition(_) => false,
117+
AssocItemRibKind
118+
| ItemRibKind
119+
| ForwardTyParamBanRibKind
120+
| TyParamAsConstParamTy => true,
121+
}
122+
}
123+
}
124+
107125
/// A single local scope.
108126
///
109127
/// A rib represents a scope names can live in. Note that these appear in many places, not just
@@ -792,6 +810,19 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
792810
let mut function_type_rib = Rib::new(rib_kind);
793811
let mut function_value_rib = Rib::new(rib_kind);
794812
let mut seen_bindings = FxHashMap::default();
813+
// We also can't shadow bindings from the parent item
814+
if let AssocItemRibKind = rib_kind {
815+
let mut add_bindings_for_ns = |ns| {
816+
let parent_rib = self.ribs[ns].iter()
817+
.rfind(|rib| if let ItemRibKind = rib.kind { true } else { false })
818+
.expect("associated item outside of an item");
819+
seen_bindings.extend(
820+
parent_rib.bindings.iter().map(|(ident, _)| (*ident, ident.span)),
821+
);
822+
};
823+
add_bindings_for_ns(ValueNS);
824+
add_bindings_for_ns(TypeNS);
825+
}
795826
for param in &generics.params {
796827
match param.kind {
797828
GenericParamKind::Lifetime { .. } => {}

src/librustc_resolve/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1448,7 +1448,7 @@ impl<'a> Resolver<'a> {
14481448
debug!("walk rib\n{:?}", ribs[i].bindings);
14491449
// Use the rib kind to determine whether we are resolving parameters
14501450
// (modern hygiene) or local variables (legacy hygiene).
1451-
let rib_ident = if let AssocItemRibKind | ItemRibKind = ribs[i].kind {
1451+
let rib_ident = if ribs[i].kind.contains_params() {
14521452
modern_ident
14531453
} else {
14541454
ident

0 commit comments

Comments
 (0)