Skip to content

Commit

Permalink
syntax: Check paths in visibilities for type parameters
Browse files Browse the repository at this point in the history
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
  • Loading branch information
petrochenkov committed Apr 24, 2016
1 parent b32d7b5 commit 6c44bea
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 105 deletions.
5 changes: 2 additions & 3 deletions src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -112,15 +112,14 @@ impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
!segment.parameters.bindings().is_empty()
});
if found_param {
self.session.span_err(path.span,
"type or lifetime parameter is found in import path");
self.session.span_err(path.span, "type or lifetime parameters in import path");
}

// Checking for special identifiers in path
// prevent `self` or `super` at beginning of global path
if path.global && path.segments.len() > 0 {
let first = path.segments[0].identifier.name;
if first == keywords::Super.ident.name || first == keywords::SelfValue.ident.name {
if first == keywords::Super.name() || first == keywords::SelfValue.name() {
self.session.add_lint(
lint::builtin::SUPER_OR_SELF_IN_GLOBAL_PATH, id, path.span,
format!("expected identifier, found keyword `{}`", first)
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_resolve/lib.rs
Expand Up @@ -62,7 +62,7 @@ use syntax::ast::{CRATE_NODE_ID, Name, NodeId, CrateNum, IntTy, UintTy};
use syntax::attr::AttrMetaMethods;
use syntax::codemap::{self, Span, Pos};
use syntax::errors::DiagnosticBuilder;
use syntax::parse::token::{self, keywords, special_idents};
use syntax::parse::token::{self, keywords};
use syntax::util::lev_distance::find_best_match_for_name;

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

// plain insert (no renaming, types are not currently hygienic....)
self_type_rib.bindings.insert(keywords::SelfType.ident.name, self_def);
self_type_rib.bindings.insert(keywords::SelfType.name(), self_def);
self.type_ribs.push(self_type_rib);
f(self);
if !self.resolved {
Expand Down Expand Up @@ -2197,7 +2197,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
let is_invalid_self_type_name = path.segments.len() > 0 &&
maybe_qself.is_none() &&
path.segments[0].identifier.name ==
keywords::SelfType.ident.name;
keywords::SelfType.name();
if is_invalid_self_type_name {
resolve_error(self,
ty.span,
Expand Down Expand Up @@ -2641,7 +2641,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
namespace: Namespace,
record_used: bool)
-> Option<LocalDef> {
if identifier.name == special_idents::Invalid.name {
if identifier.unhygienic_name == keywords::Invalid.name() {
return Some(LocalDef::from_def(Def::Err));
}

Expand Down Expand Up @@ -3073,7 +3073,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
});

if method_scope &&
&path_name[..] == keywords::SelfValue.ident.name.as_str() {
&path_name[..] == keywords::SelfValue.name().as_str() {
resolve_error(self,
expr.span,
ResolutionError::SelfNotAvailableInStaticMethod);
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/expand.rs
Expand Up @@ -1486,7 +1486,7 @@ mod tests {
use ext::mtwt;
use fold::Folder;
use parse;
use parse::token;
use parse::token::{self, keywords};
use util::parser_testing::{string_to_parser};
use util::parser_testing::{string_to_pat, string_to_crate, strs_to_idents};
use visit;
Expand Down
11 changes: 6 additions & 5 deletions src/libsyntax/ext/quote.rs
Expand Up @@ -13,7 +13,7 @@ use codemap::Span;
use ext::base::ExtCtxt;
use ext::base;
use ext::build::AstBuilder;
use parse::parser::{Parser, PathParsingMode};
use parse::parser::{Parser, PathStyle};
use parse::token::*;
use parse::token;
use ptr::P;
Expand Down Expand Up @@ -401,7 +401,7 @@ pub fn parse_meta_item_panic(parser: &mut Parser) -> P<ast::MetaItem> {
panictry!(parser.parse_meta_item())
}

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

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ext/tt/macro_parser.rs
Expand Up @@ -85,7 +85,7 @@ use codemap;
use errors::FatalError;
use parse::lexer::*; //resolve bug?
use parse::ParseSess;
use parse::parser::{LifetimeAndTypesWithoutColons, Parser};
use parse::parser::{PathStyle, Parser};
use parse::token::{DocComment, MatchNt, SubstNt};
use parse::token::{Token, Nonterminal};
use parse::token;
Expand Down Expand Up @@ -546,7 +546,7 @@ pub fn parse_nt<'a>(p: &mut Parser<'a>, sp: Span, name: &str) -> Nonterminal {
}
},
"path" => {
token::NtPath(Box::new(panictry!(p.parse_path(LifetimeAndTypesWithoutColons))))
token::NtPath(Box::new(panictry!(p.parse_path(PathStyle::Type))))
},
"meta" => token::NtMeta(panictry!(p.parse_meta_item())),
_ => {
Expand Down
14 changes: 13 additions & 1 deletion src/libsyntax/feature_gate.rs
Expand Up @@ -1168,7 +1168,19 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
fn visit_vis(&mut self, vis: &'v ast::Visibility) {
let span = match *vis {
ast::Visibility::Crate(span) => span,
ast::Visibility::Restricted { ref path, .. } => path.span,
ast::Visibility::Restricted { ref path, .. } => {
// Check for type parameters
let found_param = path.segments.iter().any(|segment| {
!segment.parameters.types().is_empty() ||
!segment.parameters.lifetimes().is_empty() ||
!segment.parameters.bindings().is_empty()
});
if found_param {
self.context.span_handler.span_err(path.span, "type or lifetime parameters \
in visibility path");
}
path.span
}
_ => return,
};
self.gate_feature("pub_restricted", span, "`pub(restricted)` syntax is experimental");
Expand Down
14 changes: 4 additions & 10 deletions src/libsyntax/parse/lexer/mod.rs
Expand Up @@ -13,8 +13,7 @@ use codemap::{BytePos, CharPos, CodeMap, Pos, Span};
use codemap;
use errors::{FatalError, Handler, DiagnosticBuilder};
use ext::tt::transcribe::tt_next_token;
use parse::token::str_to_ident;
use parse::token;
use parse::token::{self, keywords, str_to_ident};
use str::char_at;
use rustc_unicode::property::Pattern_White_Space;

Expand Down Expand Up @@ -1229,14 +1228,9 @@ impl<'a> StringReader<'a> {
});
let keyword_checking_token = &token::Ident(keyword_checking_ident);
let last_bpos = self.last_pos;
if keyword_checking_token.is_keyword(token::keywords::SelfValue) {
self.err_span_(start,
last_bpos,
"invalid lifetime name: 'self is no longer a special \
lifetime");
} else if keyword_checking_token.is_any_keyword() &&
!keyword_checking_token.is_keyword(token::keywords::Static) {
self.err_span_(start, last_bpos, "invalid lifetime name");
if keyword_checking_token.is_any_keyword() &&
!keyword_checking_token.is_keyword(keywords::Static) {
self.err_span_(start, last_bpos, "lifetimes cannot use keyword names");
}

return token::Lifetime(ident);
Expand Down

0 comments on commit 6c44bea

Please sign in to comment.