Skip to content

Commit 7378fd9

Browse files
committed
Auto merge of #22158 - Kimundi:the_lonely_uppercase_keyword, r=pnkfelix
It is only allowed in paths now, where it will either work inside a `trait` or `impl` item, or not resolve outside of it. [breaking-change] Closes #22137
2 parents 39b463f + 07d00de commit 7378fd9

File tree

12 files changed

+124
-41
lines changed

12 files changed

+124
-41
lines changed

src/librustc_trans/save/span_utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl<'a> SpanUtils<'a> {
101101
return self.make_sub_span(span, result)
102102
}
103103
if bracket_count == 0 &&
104-
(ts.tok.is_ident() || ts.tok.is_keyword(keywords::Self)) {
104+
(ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) {
105105
result = Some(ts.sp);
106106
}
107107

@@ -124,7 +124,7 @@ impl<'a> SpanUtils<'a> {
124124
return None;
125125
}
126126
if bracket_count == 0 &&
127-
(ts.tok.is_ident() || ts.tok.is_keyword(keywords::Self)) {
127+
(ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) {
128128
return self.make_sub_span(span, Some(ts.sp));
129129
}
130130

src/libsyntax/ext/deriving/clone.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn expand_deriving_clone<F>(cx: &mut ExtCtxt,
3838
generics: LifetimeBounds::empty(),
3939
explicit_self: borrowed_explicit_self(),
4040
args: Vec::new(),
41-
ret_ty: Self,
41+
ret_ty: Self_,
4242
attributes: attrs,
4343
combine_substructure: combine_substructure(box |c, s, sub| {
4444
cs_clone("Clone", c, s, sub)

src/libsyntax/ext/deriving/decodable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn expand_deriving_decodable_imp<F>(cx: &mut ExtCtxt,
7676
ret_ty: Literal(Path::new_(
7777
pathvec_std!(cx, core::result::Result),
7878
None,
79-
vec!(box Self, box Literal(Path::new_(
79+
vec!(box Self_, box Literal(Path::new_(
8080
vec!["__D", "Error"], None, vec![], false
8181
))),
8282
true

src/libsyntax/ext/deriving/default.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn expand_deriving_default<F>(cx: &mut ExtCtxt,
3838
generics: LifetimeBounds::empty(),
3939
explicit_self: None,
4040
args: Vec::new(),
41-
ret_ty: Self,
41+
ret_ty: Self_,
4242
attributes: attrs,
4343
combine_substructure: combine_substructure(box |a, b, c| {
4444
default_substructure(a, b, c)

src/libsyntax/ext/deriving/generic/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ use parse::token::InternedString;
207207
use parse::token::special_idents;
208208
use ptr::P;
209209

210-
use self::ty::{LifetimeBounds, Path, Ptr, PtrTy, Self, Ty};
210+
use self::ty::{LifetimeBounds, Path, Ptr, PtrTy, Self_, Ty};
211211

212212
pub mod ty;
213213

@@ -261,7 +261,7 @@ pub struct Substructure<'a> {
261261
pub type_ident: Ident,
262262
/// ident of the method
263263
pub method_ident: Ident,
264-
/// dereferenced access to any `Self` or `Ptr(Self, _)` arguments
264+
/// dereferenced access to any `Self_` or `Ptr(Self_, _)` arguments
265265
pub self_args: &'a [P<Expr>],
266266
/// verbatim access to any other arguments
267267
pub nonself_args: &'a [P<Expr>],
@@ -679,10 +679,10 @@ impl<'a> MethodDef<'a> {
679679
match *ty {
680680
// for static methods, just treat any Self
681681
// arguments as a normal arg
682-
Self if nonstatic => {
682+
Self_ if nonstatic => {
683683
self_args.push(arg_expr);
684684
}
685-
Ptr(box Self, _) if nonstatic => {
685+
Ptr(box Self_, _) if nonstatic => {
686686
self_args.push(cx.expr_deref(trait_.span, arg_expr))
687687
}
688688
_ => {

src/libsyntax/ext/deriving/generic/ty.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'a> Path<'a> {
8787
/// A type. Supports pointers, Self, and literals
8888
#[derive(Clone)]
8989
pub enum Ty<'a> {
90-
Self,
90+
Self_,
9191
/// &/Box/ Ty
9292
Ptr(Box<Ty<'a>>, PtrTy<'a>),
9393
/// mod::mod::Type<[lifetime], [Params...]>, including a plain type
@@ -109,7 +109,7 @@ pub fn borrowed_explicit_self<'r>() -> Option<Option<PtrTy<'r>>> {
109109
}
110110

111111
pub fn borrowed_self<'r>() -> Ty<'r> {
112-
borrowed(box Self)
112+
borrowed(box Self_)
113113
}
114114

115115
pub fn nil_ty<'r>() -> Ty<'r> {
@@ -149,7 +149,7 @@ impl<'a> Ty<'a> {
149149
}
150150
}
151151
Literal(ref p) => { p.to_ty(cx, span, self_ty, self_generics) }
152-
Self => {
152+
Self_ => {
153153
cx.ty_path(self.to_path(cx, span, self_ty, self_generics))
154154
}
155155
Tuple(ref fields) => {
@@ -168,7 +168,7 @@ impl<'a> Ty<'a> {
168168
self_generics: &Generics)
169169
-> ast::Path {
170170
match *self {
171-
Self => {
171+
Self_ => {
172172
let self_params = self_generics.ty_params.map(|ty_param| {
173173
cx.ty_ident(span, ty_param.ident)
174174
});

src/libsyntax/ext/deriving/primitive.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn expand_deriving_from_primitive<F>(cx: &mut ExtCtxt,
4141
args: vec!(Literal(path!(i64))),
4242
ret_ty: Literal(Path::new_(pathvec_std!(cx, core::option::Option),
4343
None,
44-
vec!(box Self),
44+
vec!(box Self_),
4545
true)),
4646
// #[inline] liable to cause code-bloat
4747
attributes: attrs.clone(),
@@ -56,7 +56,7 @@ pub fn expand_deriving_from_primitive<F>(cx: &mut ExtCtxt,
5656
args: vec!(Literal(path!(u64))),
5757
ret_ty: Literal(Path::new_(pathvec_std!(cx, core::option::Option),
5858
None,
59-
vec!(box Self),
59+
vec!(box Self_),
6060
true)),
6161
// #[inline] liable to cause code-bloat
6262
attributes: attrs,

src/libsyntax/ext/deriving/rand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn expand_deriving_rand<F>(cx: &mut ExtCtxt,
5353
Ptr(box Literal(Path::new_local("R")),
5454
Borrowed(None, ast::MutMutable))
5555
),
56-
ret_ty: Self,
56+
ret_ty: Self_,
5757
attributes: Vec::new(),
5858
combine_substructure: combine_substructure(box |a, b, c| {
5959
rand_substructure(a, b, c)

src/libsyntax/parse/lexer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ impl<'a> StringReader<'a> {
11221122
let keyword_checking_token =
11231123
&token::Ident(keyword_checking_ident, token::Plain);
11241124
let last_bpos = self.last_pos;
1125-
if keyword_checking_token.is_keyword(token::keywords::Self) {
1125+
if keyword_checking_token.is_keyword(token::keywords::SelfValue) {
11261126
self.err_span_(start,
11271127
last_bpos,
11281128
"invalid lifetime name: 'self \

src/libsyntax/parse/parser.rs

+51-22
Original file line numberDiff line numberDiff line change
@@ -516,13 +516,21 @@ impl<'a> Parser<'a> {
516516
}
517517
}
518518

519+
pub fn parse_ident_or_self_type(&mut self) -> ast::Ident {
520+
if self.is_self_type_ident() {
521+
self.expect_self_type_ident()
522+
} else {
523+
self.parse_ident()
524+
}
525+
}
526+
519527
pub fn parse_path_list_item(&mut self) -> ast::PathListItem {
520528
let lo = self.span.lo;
521529
let node = if self.eat_keyword_noexpect(keywords::Mod) {
522530
let span = self.last_span;
523531
self.span_warn(span, "deprecated syntax; use the `self` keyword now");
524532
ast::PathListMod { id: ast::DUMMY_NODE_ID }
525-
} else if self.eat_keyword(keywords::Self) {
533+
} else if self.eat_keyword(keywords::SelfValue) {
526534
ast::PathListMod { id: ast::DUMMY_NODE_ID }
527535
} else {
528536
let ident = self.parse_ident();
@@ -1797,7 +1805,7 @@ impl<'a> Parser<'a> {
17971805
let mut segments = Vec::new();
17981806
loop {
17991807
// First, parse an identifier.
1800-
let identifier = self.parse_ident();
1808+
let identifier = self.parse_ident_or_self_type();
18011809

18021810
// Parse types, optionally.
18031811
let parameters = if self.eat_lt() {
@@ -1850,7 +1858,7 @@ impl<'a> Parser<'a> {
18501858
let mut segments = Vec::new();
18511859
loop {
18521860
// First, parse an identifier.
1853-
let identifier = self.parse_ident();
1861+
let identifier = self.parse_ident_or_self_type();
18541862

18551863
// If we do not see a `::`, stop.
18561864
if !self.eat(&token::ModSep) {
@@ -1895,7 +1903,7 @@ impl<'a> Parser<'a> {
18951903
let mut segments = Vec::new();
18961904
loop {
18971905
// First, parse an identifier.
1898-
let identifier = self.parse_ident();
1906+
let identifier = self.parse_ident_or_self_type();
18991907

19001908
// Assemble and push the result.
19011909
segments.push(ast::PathSegment {
@@ -2166,10 +2174,8 @@ impl<'a> Parser<'a> {
21662174
token::BinOp(token::Or) | token::OrOr => {
21672175
return self.parse_lambda_expr(CaptureByRef);
21682176
},
2169-
// FIXME #13626: Should be able to stick in
2170-
// token::SELF_KEYWORD_NAME
21712177
token::Ident(id @ ast::Ident {
2172-
name: ast::Name(token::SELF_KEYWORD_NAME_NUM),
2178+
name: token::SELF_KEYWORD_NAME,
21732179
ctxt: _
21742180
}, token::Plain) => {
21752181
self.bump();
@@ -3411,7 +3417,7 @@ impl<'a> Parser<'a> {
34113417
&& self.token != token::ModSep)
34123418
|| self.token.is_keyword(keywords::True)
34133419
|| self.token.is_keyword(keywords::False) {
3414-
// Parse an expression pattern or exp .. exp.
3420+
// Parse an expression pattern or exp ... exp.
34153421
//
34163422
// These expressions are limited to literals (possibly
34173423
// preceded by unary-minus) or identifiers.
@@ -3532,15 +3538,17 @@ impl<'a> Parser<'a> {
35323538
enum_path.segments.len() == 1 &&
35333539
enum_path.segments[0].parameters.is_empty()
35343540
{
3535-
// it could still be either an enum
3536-
// or an identifier pattern, resolve
3537-
// will sort it out:
3538-
pat = PatIdent(BindByValue(MutImmutable),
3539-
codemap::Spanned{
3540-
span: enum_path.span,
3541-
node: enum_path.segments[0]
3542-
.identifier},
3543-
None);
3541+
// NB: If enum_path is a single identifier,
3542+
// this should not be reachable due to special
3543+
// handling further above.
3544+
//
3545+
// However, previously a PatIdent got emitted
3546+
// here, so we preserve the branch just in case.
3547+
//
3548+
// A rewrite of the logic in this function
3549+
// would probably make this obvious.
3550+
self.span_bug(enum_path.span,
3551+
"ident only path should have been covered already");
35443552
} else {
35453553
pat = PatEnum(enum_path, Some(args));
35463554
}
@@ -4380,6 +4388,27 @@ impl<'a> Parser<'a> {
43804388
}
43814389
}
43824390

4391+
fn is_self_type_ident(&mut self) -> bool {
4392+
match self.token {
4393+
token::Ident(id, token::Plain) => id.name == special_idents::type_self.name,
4394+
_ => false
4395+
}
4396+
}
4397+
4398+
fn expect_self_type_ident(&mut self) -> ast::Ident {
4399+
match self.token {
4400+
token::Ident(id, token::Plain) if id.name == special_idents::type_self.name => {
4401+
self.bump();
4402+
id
4403+
},
4404+
_ => {
4405+
let token_str = self.this_token_to_string();
4406+
self.fatal(&format!("expected `Self`, found `{}`",
4407+
token_str)[])
4408+
}
4409+
}
4410+
}
4411+
43834412
/// Parse the argument list and result type of a function
43844413
/// that may have a self type.
43854414
fn parse_fn_decl_with_self<F>(&mut self, parse_arg_fn: F) -> (ExplicitSelf, P<FnDecl>) where
@@ -4396,22 +4425,22 @@ impl<'a> Parser<'a> {
43964425
//
43974426
// We already know that the current token is `&`.
43984427

4399-
if this.look_ahead(1, |t| t.is_keyword(keywords::Self)) {
4428+
if this.look_ahead(1, |t| t.is_keyword(keywords::SelfValue)) {
44004429
this.bump();
44014430
SelfRegion(None, MutImmutable, this.expect_self_ident())
44024431
} else if this.look_ahead(1, |t| t.is_mutability()) &&
4403-
this.look_ahead(2, |t| t.is_keyword(keywords::Self)) {
4432+
this.look_ahead(2, |t| t.is_keyword(keywords::SelfValue)) {
44044433
this.bump();
44054434
let mutability = this.parse_mutability();
44064435
SelfRegion(None, mutability, this.expect_self_ident())
44074436
} else if this.look_ahead(1, |t| t.is_lifetime()) &&
4408-
this.look_ahead(2, |t| t.is_keyword(keywords::Self)) {
4437+
this.look_ahead(2, |t| t.is_keyword(keywords::SelfValue)) {
44094438
this.bump();
44104439
let lifetime = this.parse_lifetime();
44114440
SelfRegion(Some(lifetime), MutImmutable, this.expect_self_ident())
44124441
} else if this.look_ahead(1, |t| t.is_lifetime()) &&
44134442
this.look_ahead(2, |t| t.is_mutability()) &&
4414-
this.look_ahead(3, |t| t.is_keyword(keywords::Self)) {
4443+
this.look_ahead(3, |t| t.is_keyword(keywords::SelfValue)) {
44154444
this.bump();
44164445
let lifetime = this.parse_lifetime();
44174446
let mutability = this.parse_mutability();
@@ -4466,7 +4495,7 @@ impl<'a> Parser<'a> {
44664495
SelfValue(self_ident)
44674496
}
44684497
} else if self.token.is_mutability() &&
4469-
self.look_ahead(1, |t| t.is_keyword(keywords::Self)) {
4498+
self.look_ahead(1, |t| t.is_keyword(keywords::SelfValue)) {
44704499
mutbl_self = self.parse_mutability();
44714500
let self_ident = self.expect_self_ident();
44724501

src/libsyntax/parse/token.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ impl Token {
300300
n == SELF_KEYWORD_NAME
301301
|| n == STATIC_KEYWORD_NAME
302302
|| n == SUPER_KEYWORD_NAME
303+
|| n == SELF_TYPE_KEYWORD_NAME
303304
|| STRICT_KEYWORD_START <= n
304305
&& n <= RESERVED_KEYWORD_FINAL
305306
},
@@ -317,6 +318,7 @@ impl Token {
317318
n == SELF_KEYWORD_NAME
318319
|| n == STATIC_KEYWORD_NAME
319320
|| n == SUPER_KEYWORD_NAME
321+
|| n == SELF_TYPE_KEYWORD_NAME
320322
|| STRICT_KEYWORD_START <= n
321323
&& n <= STRICT_KEYWORD_FINAL
322324
},
@@ -488,10 +490,12 @@ macro_rules! declare_special_idents_and_keywords {(
488490
pub const SELF_KEYWORD_NAME: ast::Name = ast::Name(SELF_KEYWORD_NAME_NUM);
489491
const STATIC_KEYWORD_NAME: ast::Name = ast::Name(STATIC_KEYWORD_NAME_NUM);
490492
const SUPER_KEYWORD_NAME: ast::Name = ast::Name(SUPER_KEYWORD_NAME_NUM);
493+
const SELF_TYPE_KEYWORD_NAME: ast::Name = ast::Name(SELF_TYPE_KEYWORD_NAME_NUM);
491494

492495
pub const SELF_KEYWORD_NAME_NUM: u32 = 1;
493496
const STATIC_KEYWORD_NAME_NUM: u32 = 2;
494497
const SUPER_KEYWORD_NAME_NUM: u32 = 3;
498+
const SELF_TYPE_KEYWORD_NAME_NUM: u32 = 10;
495499

496500
// NB: leaving holes in the ident table is bad! a different ident will get
497501
// interned with the id from the hole, but it will be between the min and max
@@ -514,7 +518,7 @@ declare_special_idents_and_keywords! {
514518
(7, clownshoe_abi, "__rust_abi");
515519
(8, opaque, "<opaque>");
516520
(9, unnamed_field, "<unnamed_field>");
517-
(10, type_self, "Self");
521+
(super::SELF_TYPE_KEYWORD_NAME_NUM, type_self, "Self");
518522
(11, prelude_import, "prelude_import");
519523
}
520524

@@ -545,7 +549,8 @@ declare_special_idents_and_keywords! {
545549
(32, Return, "return");
546550
// Static and Self are also special idents (prefill de-dupes)
547551
(super::STATIC_KEYWORD_NAME_NUM, Static, "static");
548-
(super::SELF_KEYWORD_NAME_NUM, Self, "self");
552+
(super::SELF_KEYWORD_NAME_NUM, SelfValue, "self");
553+
(super::SELF_TYPE_KEYWORD_NAME_NUM, SelfType, "Self");
549554
(33, Struct, "struct");
550555
(super::SUPER_KEYWORD_NAME_NUM, Super, "super");
551556
(34, True, "true");

0 commit comments

Comments
 (0)