Skip to content

Simplify PatIdent to contain an Ident rather than a Path #15327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,12 +902,10 @@ impl LintPass for NonUppercasePatternStatics {
fn check_pat(&mut self, cx: &Context, p: &ast::Pat) {
// Lint for constants that look like binding identifiers (#7526)
match (&p.node, cx.tcx.def_map.borrow().find(&p.id)) {
(&ast::PatIdent(_, ref path, _), Some(&def::DefStatic(_, false))) => {
// last identifier alone is right choice for this lint.
let ident = path.segments.last().unwrap().identifier;
let s = token::get_ident(ident);
(&ast::PatIdent(_, ref path1, _), Some(&def::DefStatic(_, false))) => {
let s = token::get_ident(path1.node);
if s.get().chars().any(|c| c.is_lowercase()) {
cx.span_lint(NON_UPPERCASE_PATTERN_STATICS, path.span,
cx.span_lint(NON_UPPERCASE_PATTERN_STATICS, path1.span,
format!("static constant in pattern `{}` should have an uppercase \
name such as `{}`",
s.get(), s.get().chars().map(|c| c.to_uppercase())
Expand All @@ -931,15 +929,13 @@ impl LintPass for UppercaseVariables {

fn check_pat(&mut self, cx: &Context, p: &ast::Pat) {
match &p.node {
&ast::PatIdent(_, ref path, _) => {
&ast::PatIdent(_, ref path1, _) => {
match cx.tcx.def_map.borrow().find(&p.id) {
Some(&def::DefLocal(_, _)) | Some(&def::DefBinding(_, _)) |
Some(&def::DefArg(_, _)) => {
// last identifier alone is right choice for this lint.
let ident = path.segments.last().unwrap().identifier;
let s = token::get_ident(ident);
let s = token::get_ident(path1.node);
if s.get().len() > 0 && s.get().char_at(0).is_uppercase() {
cx.span_lint(UPPERCASE_VARIABLES, path.span,
cx.span_lint(UPPERCASE_VARIABLES, path1.span,
"variable names should start with \
a lowercase character");
}
Expand Down Expand Up @@ -1113,15 +1109,10 @@ impl UnusedMut {
// avoid false warnings in match arms with multiple patterns
let mut mutables = HashMap::new();
for &p in pats.iter() {
pat_util::pat_bindings(&cx.tcx.def_map, &*p, |mode, id, _, path| {
pat_util::pat_bindings(&cx.tcx.def_map, &*p, |mode, id, _, path1| {
let ident = path1.node;
match mode {
ast::BindByValue(ast::MutMutable) => {
if path.segments.len() != 1 {
cx.sess().span_bug(p.span,
"mutable binding that doesn't consist \
of exactly one segment");
}
let ident = path.segments.get(0).identifier;
if !token::get_ident(ident).get().starts_with("_") {
mutables.insert_or_update_with(ident.name as uint,
vec!(id), |_, old| { old.push(id); });
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,9 +809,8 @@ fn encode_method_argument_names(ebml_w: &mut Encoder,
for arg in decl.inputs.iter() {
ebml_w.start_tag(tag_method_argument_name);
match arg.pat.node {
ast::PatIdent(_, ref name, _) => {
let name = name.segments.last().unwrap().identifier;
let name = token::get_ident(name);
ast::PatIdent(_, ref path1, _) => {
let name = token::get_ident(path1.node);
ebml_w.writer.write(name.get().as_bytes());
}
_ => {}
Expand Down Expand Up @@ -1106,8 +1105,9 @@ fn encode_info_for_item(ecx: &EncodeContext,
match ty.node {
ast::TyPath(ref path, ref bounds, _) if path.segments
.len() == 1 => {
let ident = path.segments.last().unwrap().identifier;
assert!(bounds.is_none());
encode_impl_type_basename(ebml_w, ast_util::path_to_ident(path));
encode_impl_type_basename(ebml_w, ident);
}
_ => {}
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/borrowck/gather_loans/gather_moves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ pub fn gather_move_from_pat(bccx: &BorrowckCtxt,
move_pat: &ast::Pat,
cmt: mc::cmt) {
let pat_span_path_opt = match move_pat.node {
ast::PatIdent(_, ref path, _) => {
Some(MoveSpanAndPath::with_span_and_path(move_pat.span,
(*path).clone()))
ast::PatIdent(_, ref path1, _) => {
Some(MoveSpanAndPath{span: move_pat.span,
ident: path1.node})
},
_ => None,
};
Expand Down
21 changes: 5 additions & 16 deletions src/librustc/middle/borrowck/gather_loans/move_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,8 @@ impl MoveError {

#[deriving(Clone)]
pub struct MoveSpanAndPath {
span: codemap::Span,
path: ast::Path
}

impl MoveSpanAndPath {
pub fn with_span_and_path(span: codemap::Span,
path: ast::Path)
-> MoveSpanAndPath {
MoveSpanAndPath {
span: span,
path: path,
}
}
pub span: codemap::Span,
pub ident: ast::Ident
}

pub struct GroupedMoveErrors {
Expand All @@ -83,7 +72,7 @@ fn report_move_errors(bccx: &BorrowckCtxt, errors: &Vec<MoveError>) {
let mut is_first_note = true;
for move_to in error.move_to_places.iter() {
note_move_destination(bccx, move_to.span,
&move_to.path, is_first_note);
&move_to.ident, is_first_note);
is_first_note = false;
}
}
Expand Down Expand Up @@ -154,9 +143,9 @@ fn report_cannot_move_out_of(bccx: &BorrowckCtxt, move_from: mc::cmt) {

fn note_move_destination(bccx: &BorrowckCtxt,
move_to_span: codemap::Span,
pat_ident_path: &ast::Path,
pat_ident: &ast::Ident,
is_first_note: bool) {
let pat_name = pprust::path_to_str(pat_ident_path);
let pat_name = pprust::ident_to_str(pat_ident);
if is_first_note {
bccx.span_note(
move_to_span,
Expand Down
4 changes: 4 additions & 0 deletions src/librustc/middle/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ impl<'a> Visitor<MarkSymbolVisitorContext> for MarkSymbolVisitor<'a> {
ast::PatStruct(_, ref fields, _) => {
self.handle_field_pattern_match(pat, fields.as_slice());
}
ast::PatIdent(_, _, _) => {
// it might be the only use of a static:
self.lookup_and_handle_definition(&pat.id)
}
_ => ()
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use util::ppaux::UserString;
use syntax::ast::*;
use syntax::attr;
use syntax::codemap::Span;
use syntax::print::pprust::{expr_to_str,path_to_str};
use syntax::print::pprust::{expr_to_str, ident_to_str};
use syntax::{visit};
use syntax::visit::Visitor;

Expand Down Expand Up @@ -627,7 +627,7 @@ fn check_sized(tcx: &ty::ctxt, ty: ty::t, name: String, sp: Span) {
fn check_pat(cx: &mut Context, pat: &Pat) {
let var_name = match pat.node {
PatWild => Some("_".to_string()),
PatIdent(_, ref path, _) => Some(path_to_str(path).to_string()),
PatIdent(_, ref path1, _) => Some(ident_to_str(&path1.node).to_string()),
_ => None
};

Expand Down
16 changes: 8 additions & 8 deletions src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ fn visit_fn(ir: &mut IrMaps,
for arg in decl.inputs.iter() {
pat_util::pat_bindings(&ir.tcx.def_map,
&*arg.pat,
|_bm, arg_id, _x, path| {
|_bm, arg_id, _x, path1| {
debug!("adding argument {}", arg_id);
let ident = ast_util::path_to_ident(path);
let ident = path1.node;
fn_maps.add_variable(Arg(arg_id, ident));
})
};
Expand Down Expand Up @@ -399,9 +399,9 @@ fn visit_fn(ir: &mut IrMaps,
}

fn visit_local(ir: &mut IrMaps, local: &Local) {
pat_util::pat_bindings(&ir.tcx.def_map, &*local.pat, |_, p_id, sp, path| {
pat_util::pat_bindings(&ir.tcx.def_map, &*local.pat, |_, p_id, sp, path1| {
debug!("adding local variable {}", p_id);
let name = ast_util::path_to_ident(path);
let name = path1.node;
ir.add_live_node_for_node(p_id, VarDefNode(sp));
ir.add_variable(Local(LocalInfo {
id: p_id,
Expand All @@ -413,10 +413,10 @@ fn visit_local(ir: &mut IrMaps, local: &Local) {

fn visit_arm(ir: &mut IrMaps, arm: &Arm) {
for pat in arm.pats.iter() {
pat_util::pat_bindings(&ir.tcx.def_map, &**pat, |bm, p_id, sp, path| {
pat_util::pat_bindings(&ir.tcx.def_map, &**pat, |bm, p_id, sp, path1| {
debug!("adding local variable {} from match with bm {:?}",
p_id, bm);
let name = ast_util::path_to_ident(path);
let name = path1.node;
ir.add_live_node_for_node(p_id, VarDefNode(sp));
ir.add_variable(Local(LocalInfo {
id: p_id,
Expand Down Expand Up @@ -1522,10 +1522,10 @@ impl<'a> Liveness<'a> {
for arg in decl.inputs.iter() {
pat_util::pat_bindings(&self.ir.tcx.def_map,
&*arg.pat,
|_bm, p_id, sp, path| {
|_bm, p_id, sp, path1| {
let var = self.variable(p_id, sp);
// Ignore unused self.
let ident = ast_util::path_to_ident(path);
let ident = path1.node;
if ident.name != special_idents::self_.name {
self.warn_about_unused(sp, p_id, entry_ln, var);
}
Expand Down
14 changes: 7 additions & 7 deletions src/librustc/middle/pat_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use middle::resolve;
use std::collections::HashMap;
use std::gc::{Gc, GC};
use syntax::ast::*;
use syntax::ast_util::{path_to_ident, walk_pat};
use syntax::ast_util::{walk_pat};
use syntax::codemap::{Span, DUMMY_SP};

pub type PatIdMap = HashMap<Ident, NodeId>;
Expand All @@ -23,8 +23,8 @@ pub type PatIdMap = HashMap<Ident, NodeId>;
// use the NodeId of their namesake in the first pattern.
pub fn pat_id_map(dm: &resolve::DefMap, pat: &Pat) -> PatIdMap {
let mut map = HashMap::new();
pat_bindings(dm, pat, |_bm, p_id, _s, n| {
map.insert(path_to_ident(n), p_id);
pat_bindings(dm, pat, |_bm, p_id, _s, path1| {
map.insert(path1.node, p_id);
});
map
}
Expand Down Expand Up @@ -75,7 +75,7 @@ pub fn pat_is_binding_or_wild(dm: &resolve::DefMap, pat: &Pat) -> bool {
/// `match foo() { Some(a) => (), None => () }`
pub fn pat_bindings(dm: &resolve::DefMap,
pat: &Pat,
it: |BindingMode, NodeId, Span, &Path|) {
it: |BindingMode, NodeId, Span, &SpannedIdent|) {
walk_pat(pat, |p| {
match p.node {
PatIdent(binding_mode, ref pth, _) if pat_is_binding(dm, p) => {
Expand All @@ -102,10 +102,10 @@ pub fn pat_contains_bindings(dm: &resolve::DefMap, pat: &Pat) -> bool {
contains_bindings
}

pub fn simple_identifier<'a>(pat: &'a Pat) -> Option<&'a Path> {
pub fn simple_identifier<'a>(pat: &'a Pat) -> Option<&'a Ident> {
match pat.node {
PatIdent(BindByValue(_), ref path, None) => {
Some(path)
PatIdent(BindByValue(_), ref path1, None) => {
Some(&path1.node)
}
_ => {
None
Expand Down
65 changes: 9 additions & 56 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ use util::nodemap::{NodeMap, DefIdSet, FnvHashMap};
use syntax::ast::*;
use syntax::ast;
use syntax::ast_util::{local_def};
use syntax::ast_util::{path_to_ident, walk_pat, trait_method_to_ty_method};
use syntax::ast_util::{walk_pat, trait_method_to_ty_method};
use syntax::ext::mtwt;
use syntax::parse::token::special_idents;
use syntax::parse::token;
use syntax::print::pprust::path_to_str;
use syntax::codemap::{Span, DUMMY_SP, Pos};
use syntax::owned_slice::OwnedSlice;
use syntax::visit;
Expand Down Expand Up @@ -1247,7 +1246,7 @@ impl<'a> Resolver<'a> {
// Create the module and add all methods.
match ty.node {
TyPath(ref path, _, _) if path.segments.len() == 1 => {
let name = path_to_ident(path);
let name = path.segments.last().unwrap().identifier;

let parent_opt = parent.module().children.borrow()
.find_copy(&name.name);
Expand Down Expand Up @@ -4104,8 +4103,8 @@ impl<'a> Resolver<'a> {
// user and one 'x' came from the macro.
fn binding_mode_map(&mut self, pat: &Pat) -> BindingMap {
let mut result = HashMap::new();
pat_bindings(&self.def_map, pat, |binding_mode, _id, sp, path| {
let name = mtwt::resolve(path_to_ident(path));
pat_bindings(&self.def_map, pat, |binding_mode, _id, sp, path1| {
let name = mtwt::resolve(path1.node);
result.insert(name,
binding_info {span: sp,
binding_mode: binding_mode});
Expand Down Expand Up @@ -4314,8 +4313,7 @@ impl<'a> Resolver<'a> {
let pat_id = pattern.id;
walk_pat(pattern, |pattern| {
match pattern.node {
PatIdent(binding_mode, ref path, _)
if !path.global && path.segments.len() == 1 => {
PatIdent(binding_mode, ref path1, _) => {

// The meaning of pat_ident with no type parameters
// depends on whether an enum variant or unit-like struct
Expand All @@ -4326,7 +4324,7 @@ impl<'a> Resolver<'a> {
// such a value is simply disallowed (since it's rarely
// what you want).

let ident = path.segments.get(0).identifier;
let ident = path1.node;
let renamed = mtwt::resolve(ident);

match self.resolve_bare_identifier_pattern(ident) {
Expand Down Expand Up @@ -4416,57 +4414,12 @@ impl<'a> Resolver<'a> {
format!("identifier `{}` is bound \
more than once in the same \
pattern",
path_to_str(path)).as_slice());
token::get_ident(ident)).as_slice());
}
// Else, not bound in the same pattern: do
// nothing.
}
}

// Check the types in the path pattern.
for ty in path.segments
.iter()
.flat_map(|seg| seg.types.iter()) {
self.resolve_type(&**ty);
}
}

PatIdent(binding_mode, ref path, _) => {
// This must be an enum variant, struct, or constant.
match self.resolve_path(pat_id, path, ValueNS, false) {
Some(def @ (DefVariant(..), _)) |
Some(def @ (DefStruct(..), _)) => {
self.record_def(pattern.id, def);
}
Some(def @ (DefStatic(..), _)) => {
self.enforce_default_binding_mode(
pattern,
binding_mode,
"a constant");
self.record_def(pattern.id, def);
}
Some(_) => {
self.resolve_error(
path.span,
format!("`{}` is not an enum variant or constant",
token::get_ident(
path.segments
.last()
.unwrap()
.identifier)).as_slice())
}
None => {
self.resolve_error(path.span,
"unresolved enum variant");
}
}

// Check the types in the path pattern.
for ty in path.segments
.iter()
.flat_map(|s| s.types.iter()) {
self.resolve_type(&**ty);
}
}

PatEnum(ref path, _) => {
Expand Down Expand Up @@ -5202,8 +5155,8 @@ impl<'a> Resolver<'a> {
in a static method. Maybe a \
`self` argument is missing?");
} else {
let name = path_to_ident(path).name;
let mut msg = match self.find_fallback_in_self_type(name) {
let last_name = path.segments.last().unwrap().identifier.name;
let mut msg = match self.find_fallback_in_self_type(last_name) {
NoSuggestion => {
// limit search to 5 to reduce the number
// of stupid suggestions
Expand Down
5 changes: 3 additions & 2 deletions src/librustc/middle/save/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ impl <'l> DxrVisitor<'l> {
self.collected_paths.push((p.id, path.clone(), false, recorder::VarRef));
visit::walk_pat(self, p, e);
}
ast::PatIdent(bm, ref path, ref optional_subpattern) => {
ast::PatIdent(bm, ref path1, ref optional_subpattern) => {
let immut = match bm {
// Even if the ref is mut, you can't change the ref, only
// the data pointed at, so showing the initialising expression
Expand All @@ -940,7 +940,8 @@ impl <'l> DxrVisitor<'l> {
}
};
// collect path for either visit_local or visit_arm
self.collected_paths.push((p.id, path.clone(), immut, recorder::VarRef));
let path = ast_util::ident_to_path(path1.span,path1.node);
self.collected_paths.push((p.id, path, immut, recorder::VarRef));
match *optional_subpattern {
None => {}
Some(subpattern) => self.visit_pat(&*subpattern, e),
Expand Down
Loading