Skip to content

Commit 6c44bea

Browse files
committed
syntax: Check paths in visibilities for type parameters
syntax: Merge PathParsingMode::NoTypesAllowed and PathParsingMode::ImportPrefix syntax: Rename PathParsingMode and its variants to better express their purpose syntax: Remove obsolete error message about 'self lifetime syntax: Remove ALLOW_MODULE_PATHS workaround syntax/resolve: Adjust some error messages resolve: Compare unhygienic (not renamed) names with keywords::Invalid, invalid identifiers may appear to be valid after renaming
1 parent b32d7b5 commit 6c44bea

File tree

13 files changed

+110
-105
lines changed

13 files changed

+110
-105
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,14 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
112112
!segment.parameters.bindings().is_empty()
113113
});
114114
if found_param {
115-
self.session.span_err(path.span,
116-
"type or lifetime parameter is found in import path");
115+
self.session.span_err(path.span, "type or lifetime parameters in import path");
117116
}
118117

119118
// Checking for special identifiers in path
120119
// prevent `self` or `super` at beginning of global path
121120
if path.global && path.segments.len() > 0 {
122121
let first = path.segments[0].identifier.name;
123-
if first == keywords::Super.ident.name || first == keywords::SelfValue.ident.name {
122+
if first == keywords::Super.name() || first == keywords::SelfValue.name() {
124123
self.session.add_lint(
125124
lint::builtin::SUPER_OR_SELF_IN_GLOBAL_PATH, id, path.span,
126125
format!("expected identifier, found keyword `{}`", first)

src/librustc_resolve/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use syntax::ast::{CRATE_NODE_ID, Name, NodeId, CrateNum, IntTy, UintTy};
6262
use syntax::attr::AttrMetaMethods;
6363
use syntax::codemap::{self, Span, Pos};
6464
use syntax::errors::DiagnosticBuilder;
65-
use syntax::parse::token::{self, keywords, special_idents};
65+
use syntax::parse::token::{self, keywords};
6666
use syntax::util::lev_distance::find_best_match_for_name;
6767

6868
use rustc::hir::intravisit::{self, FnKind, Visitor};
@@ -1954,7 +1954,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
19541954
let mut self_type_rib = Rib::new(NormalRibKind);
19551955

19561956
// plain insert (no renaming, types are not currently hygienic....)
1957-
self_type_rib.bindings.insert(keywords::SelfType.ident.name, self_def);
1957+
self_type_rib.bindings.insert(keywords::SelfType.name(), self_def);
19581958
self.type_ribs.push(self_type_rib);
19591959
f(self);
19601960
if !self.resolved {
@@ -2197,7 +2197,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
21972197
let is_invalid_self_type_name = path.segments.len() > 0 &&
21982198
maybe_qself.is_none() &&
21992199
path.segments[0].identifier.name ==
2200-
keywords::SelfType.ident.name;
2200+
keywords::SelfType.name();
22012201
if is_invalid_self_type_name {
22022202
resolve_error(self,
22032203
ty.span,
@@ -2641,7 +2641,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
26412641
namespace: Namespace,
26422642
record_used: bool)
26432643
-> Option<LocalDef> {
2644-
if identifier.name == special_idents::Invalid.name {
2644+
if identifier.unhygienic_name == keywords::Invalid.name() {
26452645
return Some(LocalDef::from_def(Def::Err));
26462646
}
26472647

@@ -3073,7 +3073,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
30733073
});
30743074

30753075
if method_scope &&
3076-
&path_name[..] == keywords::SelfValue.ident.name.as_str() {
3076+
&path_name[..] == keywords::SelfValue.name().as_str() {
30773077
resolve_error(self,
30783078
expr.span,
30793079
ResolutionError::SelfNotAvailableInStaticMethod);

src/libsyntax/ext/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ mod tests {
14861486
use ext::mtwt;
14871487
use fold::Folder;
14881488
use parse;
1489-
use parse::token;
1489+
use parse::token::{self, keywords};
14901490
use util::parser_testing::{string_to_parser};
14911491
use util::parser_testing::{string_to_pat, string_to_crate, strs_to_idents};
14921492
use visit;

src/libsyntax/ext/quote.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use codemap::Span;
1313
use ext::base::ExtCtxt;
1414
use ext::base;
1515
use ext::build::AstBuilder;
16-
use parse::parser::{Parser, PathParsingMode};
16+
use parse::parser::{Parser, PathStyle};
1717
use parse::token::*;
1818
use parse::token;
1919
use ptr::P;
@@ -401,7 +401,7 @@ pub fn parse_meta_item_panic(parser: &mut Parser) -> P<ast::MetaItem> {
401401
panictry!(parser.parse_meta_item())
402402
}
403403

404-
pub fn parse_path_panic(parser: &mut Parser, mode: PathParsingMode) -> ast::Path {
404+
pub fn parse_path_panic(parser: &mut Parser, mode: PathStyle) -> ast::Path {
405405
panictry!(parser.parse_path(mode))
406406
}
407407

@@ -500,7 +500,7 @@ pub fn expand_quote_path(cx: &mut ExtCtxt,
500500
sp: Span,
501501
tts: &[TokenTree])
502502
-> Box<base::MacResult+'static> {
503-
let mode = mk_parser_path(cx, sp, "LifetimeAndTypesWithoutColons");
503+
let mode = mk_parser_path(cx, sp, &["PathStyle", "Type"]);
504504
let expanded = expand_parse_call(cx, sp, "parse_path_panic", vec!(mode), tts);
505505
base::MacEager::expr(expanded)
506506
}
@@ -557,8 +557,9 @@ fn mk_token_path(cx: &ExtCtxt, sp: Span, name: &str) -> P<ast::Expr> {
557557
cx.expr_path(cx.path_global(sp, idents))
558558
}
559559

560-
fn mk_parser_path(cx: &ExtCtxt, sp: Span, name: &str) -> P<ast::Expr> {
561-
let idents = vec!(id_ext("syntax"), id_ext("parse"), id_ext("parser"), id_ext(name));
560+
fn mk_parser_path(cx: &ExtCtxt, sp: Span, names: &[&str]) -> P<ast::Expr> {
561+
let mut idents = vec![id_ext("syntax"), id_ext("parse"), id_ext("parser")];
562+
idents.extend(names.iter().cloned().map(id_ext));
562563
cx.expr_path(cx.path_global(sp, idents))
563564
}
564565

src/libsyntax/ext/tt/macro_parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ use codemap;
8585
use errors::FatalError;
8686
use parse::lexer::*; //resolve bug?
8787
use parse::ParseSess;
88-
use parse::parser::{LifetimeAndTypesWithoutColons, Parser};
88+
use parse::parser::{PathStyle, Parser};
8989
use parse::token::{DocComment, MatchNt, SubstNt};
9090
use parse::token::{Token, Nonterminal};
9191
use parse::token;
@@ -546,7 +546,7 @@ pub fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
546546
}
547547
},
548548
"path" => {
549-
token::NtPath(Box::new(panictry!(p.parse_path(LifetimeAndTypesWithoutColons))))
549+
token::NtPath(Box::new(panictry!(p.parse_path(PathStyle::Type))))
550550
},
551551
"meta" => token::NtMeta(panictry!(p.parse_meta_item())),
552552
_ => {

src/libsyntax/feature_gate.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,19 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
11681168
fn visit_vis(&mut self, vis: &'v ast::Visibility) {
11691169
let span = match *vis {
11701170
ast::Visibility::Crate(span) => span,
1171-
ast::Visibility::Restricted { ref path, .. } => path.span,
1171+
ast::Visibility::Restricted { ref path, .. } => {
1172+
// Check for type parameters
1173+
let found_param = path.segments.iter().any(|segment| {
1174+
!segment.parameters.types().is_empty() ||
1175+
!segment.parameters.lifetimes().is_empty() ||
1176+
!segment.parameters.bindings().is_empty()
1177+
});
1178+
if found_param {
1179+
self.context.span_handler.span_err(path.span, "type or lifetime parameters \
1180+
in visibility path");
1181+
}
1182+
path.span
1183+
}
11721184
_ => return,
11731185
};
11741186
self.gate_feature("pub_restricted", span, "`pub(restricted)` syntax is experimental");

src/libsyntax/parse/lexer/mod.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use codemap::{BytePos, CharPos, CodeMap, Pos, Span};
1313
use codemap;
1414
use errors::{FatalError, Handler, DiagnosticBuilder};
1515
use ext::tt::transcribe::tt_next_token;
16-
use parse::token::str_to_ident;
17-
use parse::token;
16+
use parse::token::{self, keywords, str_to_ident};
1817
use str::char_at;
1918
use rustc_unicode::property::Pattern_White_Space;
2019

@@ -1229,14 +1228,9 @@ impl<'a> StringReader<'a> {
12291228
});
12301229
let keyword_checking_token = &token::Ident(keyword_checking_ident);
12311230
let last_bpos = self.last_pos;
1232-
if keyword_checking_token.is_keyword(token::keywords::SelfValue) {
1233-
self.err_span_(start,
1234-
last_bpos,
1235-
"invalid lifetime name: 'self is no longer a special \
1236-
lifetime");
1237-
} else if keyword_checking_token.is_any_keyword() &&
1238-
!keyword_checking_token.is_keyword(token::keywords::Static) {
1239-
self.err_span_(start, last_bpos, "invalid lifetime name");
1231+
if keyword_checking_token.is_any_keyword() &&
1232+
!keyword_checking_token.is_keyword(keywords::Static) {
1233+
self.err_span_(start, last_bpos, "lifetimes cannot use keyword names");
12401234
}
12411235

12421236
return token::Lifetime(ident);

0 commit comments

Comments
 (0)