Skip to content

Commit 6f567e0

Browse files
committed
rollup merge of #20425: sanxiyn/opt-local-ty
This avoids having ast::Ty nodes which have no counterpart in the source.
2 parents 735c308 + 4ae7c38 commit 6f567e0

File tree

12 files changed

+50
-28
lines changed

12 files changed

+50
-28
lines changed

src/librustc/middle/region.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,10 @@ fn resolve_local(visitor: &mut RegionResolutionVisitor, local: &ast::Local) {
649649
Some(ref expr) => {
650650
record_rvalue_scope_if_borrow_expr(visitor, &**expr, blk_scope);
651651

652-
if is_binding_pat(&*local.pat) || is_borrowed_ty(&*local.ty) {
652+
let is_borrow =
653+
if let Some(ref ty) = local.ty { is_borrowed_ty(&**ty) } else { false };
654+
655+
if is_binding_pat(&*local.pat) || is_borrow {
653656
record_rvalue_scope(visitor, &**expr, blk_scope);
654657
}
655658
}

src/librustc_resolve/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3401,7 +3401,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
34013401

34023402
fn resolve_local(&mut self, local: &Local) {
34033403
// Resolve the type.
3404-
self.resolve_type(&*local.ty);
3404+
if let Some(ref ty) = local.ty {
3405+
self.resolve_type(&**ty);
3406+
}
34053407

34063408
// Resolve the initializer, if necessary.
34073409
match local.init {

src/librustc_trans/save/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1495,7 +1495,7 @@ impl<'l, 'tcx, 'v> Visitor<'v> for DxrVisitor<'l, 'tcx> {
14951495
self.collected_paths.clear();
14961496

14971497
// Just walk the initialiser and type (don't want to walk the pattern again).
1498-
self.visit_ty(&*l.ty);
1498+
visit::walk_ty_opt(self, &l.ty);
14991499
visit::walk_expr_opt(self, &l.init);
15001500
}
15011501
}

src/librustc_typeck/check/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,9 @@ impl<'a, 'tcx> GatherLocalsVisitor<'a, 'tcx> {
507507
impl<'a, 'tcx, 'v> Visitor<'v> for GatherLocalsVisitor<'a, 'tcx> {
508508
// Add explicitly-declared locals.
509509
fn visit_local(&mut self, local: &ast::Local) {
510-
let o_ty = match local.ty.node {
511-
ast::TyInfer => None,
512-
_ => Some(self.fcx.to_ty(&*local.ty))
510+
let o_ty = match local.ty {
511+
Some(ref ty) => Some(self.fcx.to_ty(&**ty)),
512+
None => None
513513
};
514514
self.assign(local.span, local.id, o_ty);
515515
debug!("Local variable {} is assigned type {}",

src/libsyntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,8 @@ pub enum LocalSource {
643643
/// Local represents a `let` statement, e.g., `let <pat>:<ty> = <expr>;`
644644
#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)]
645645
pub struct Local {
646-
pub ty: P<Ty>,
647646
pub pat: P<Pat>,
647+
pub ty: Option<P<Ty>>,
648648
pub init: Option<P<Expr>>,
649649
pub id: NodeId,
650650
pub span: Span,

src/libsyntax/ext/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,8 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
482482
self.pat_ident(sp, ident)
483483
};
484484
let local = P(ast::Local {
485-
ty: self.ty_infer(sp),
486485
pat: pat,
486+
ty: None,
487487
init: Some(ex),
488488
id: ast::DUMMY_NODE_ID,
489489
span: sp,
@@ -506,8 +506,8 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
506506
self.pat_ident(sp, ident)
507507
};
508508
let local = P(ast::Local {
509-
ty: typ,
510509
pat: pat,
510+
ty: Some(typ),
511511
init: Some(ex),
512512
id: ast::DUMMY_NODE_ID,
513513
span: sp,

src/libsyntax/ext/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ fn expand_non_macro_stmt(Spanned {node, span: stmt_span}: Stmt, fld: &mut MacroE
712712
let rewritten_local = local.map(|Local {id, pat, ty, init, source, span}| {
713713
// expand the ty since TyFixedLengthVec contains an Expr
714714
// and thus may have a macro use
715-
let expanded_ty = fld.fold_ty(ty);
715+
let expanded_ty = ty.map(|t| fld.fold_ty(t));
716716
// expand the pat (it might contain macro uses):
717717
let expanded_pat = fld.fold_pat(pat);
718718
// find the PatIdents in the pattern:

src/libsyntax/fold.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ pub fn noop_fold_parenthesized_parameter_data<T: Folder>(data: ParenthesizedPara
553553
pub fn noop_fold_local<T: Folder>(l: P<Local>, fld: &mut T) -> P<Local> {
554554
l.map(|Local {id, pat, ty, init, source, span}| Local {
555555
id: fld.new_id(id),
556-
ty: fld.fold_ty(ty),
556+
ty: ty.map(|t| fld.fold_ty(t)),
557557
pat: fld.fold_pat(pat),
558558
init: init.map(|e| fld.fold_expr(e)),
559559
source: source,

src/libsyntax/parse/parser.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3628,13 +3628,9 @@ impl<'a> Parser<'a> {
36283628
let lo = self.span.lo;
36293629
let pat = self.parse_pat();
36303630

3631-
let mut ty = P(Ty {
3632-
id: ast::DUMMY_NODE_ID,
3633-
node: TyInfer,
3634-
span: mk_sp(lo, lo),
3635-
});
3631+
let mut ty = None;
36363632
if self.eat(&token::Colon) {
3637-
ty = self.parse_ty_sum();
3633+
ty = Some(self.parse_ty_sum());
36383634
}
36393635
let init = self.parse_initializer();
36403636
P(ast::Local {

src/libsyntax/print/pprust.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,13 +1858,11 @@ impl<'a> State<'a> {
18581858

18591859
pub fn print_local_decl(&mut self, loc: &ast::Local) -> IoResult<()> {
18601860
try!(self.print_pat(&*loc.pat));
1861-
match loc.ty.node {
1862-
ast::TyInfer => Ok(()),
1863-
_ => {
1864-
try!(self.word_space(":"));
1865-
self.print_type(&*loc.ty)
1866-
}
1861+
if let Some(ref ty) = loc.ty {
1862+
try!(self.word_space(":"));
1863+
try!(self.print_type(&**ty));
18671864
}
1865+
Ok(())
18681866
}
18691867

18701868
pub fn print_decl(&mut self, decl: &ast::Decl) -> IoResult<()> {

src/libsyntax/visit.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ pub fn walk_view_item<'v, V: Visitor<'v>>(visitor: &mut V, vi: &'v ViewItem) {
211211

212212
pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
213213
visitor.visit_pat(&*local.pat);
214-
visitor.visit_ty(&*local.ty);
214+
walk_ty_opt(visitor, &local.ty);
215215
walk_expr_opt(visitor, &local.init);
216216
}
217217

@@ -381,6 +381,13 @@ pub fn skip_ty<'v, V: Visitor<'v>>(_: &mut V, _: &'v Ty) {
381381
// Empty!
382382
}
383383

384+
pub fn walk_ty_opt<'v, V: Visitor<'v>>(visitor: &mut V, optional_type: &'v Option<P<Ty>>) {
385+
match *optional_type {
386+
Some(ref ty) => visitor.visit_ty(&**ty),
387+
None => ()
388+
}
389+
}
390+
384391
pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
385392
match typ.node {
386393
TyVec(ref ty) | TyParen(ref ty) => {
@@ -583,10 +590,7 @@ pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V,
583590
pub fn walk_ty_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v TyParam) {
584591
visitor.visit_ident(param.span, param.ident);
585592
walk_ty_param_bounds_helper(visitor, &param.bounds);
586-
match param.default {
587-
Some(ref ty) => visitor.visit_ty(&**ty),
588-
None => {}
589-
}
593+
walk_ty_opt(visitor, &param.default);
590594
}
591595

592596
pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics) {

src/test/pretty/let.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// pp-exact
12+
13+
// Check that `let x: _ = 0;` does not print as `let x = 0;`.
14+
15+
fn main() {
16+
let x: _ = 0;
17+
18+
let _ = x;
19+
}

0 commit comments

Comments
 (0)