Skip to content

Commit e9f6f3f

Browse files
committed
Parse and reserve typeof keyword. rust-lang#3228
1 parent 2c0f9bd commit e9f6f3f

File tree

10 files changed

+43
-4
lines changed

10 files changed

+43
-4
lines changed

src/etc/vim/syntax/rust.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ syn match rustIdentifier contains=rustIdentifierPrime "\%([^[:cntrl:][:spac
3333
syn match rustFuncName "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" display contained
3434

3535
" reserved
36-
syn keyword rustKeyword be
36+
syn keyword rustKeyword be yield typeof
3737

3838
syn keyword rustType int uint float char bool u8 u16 u32 u64 f32
3939
syn keyword rustType f64 i8 i16 i32 i64 str Self

src/librustc/middle/typeck/astconv.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,9 @@ pub fn ast_ty_to_ty<AC:AstConv, RS:region_scope + Clone + 'static>(
504504
}
505505
}
506506
}
507+
ast::ty_typeof(_e) => {
508+
tcx.sess.span_bug(ast_ty.span, "typeof is reserved but unimplemented");
509+
}
507510
ast::ty_infer => {
508511
// ty_infer should only appear as the type of arguments or return
509512
// values in a fn_expr, or as the type of local variables. Both of

src/libsyntax/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,7 @@ pub enum ty_ {
775775
ty_tup(~[Ty]),
776776
ty_path(Path, Option<OptVec<TyParamBound>>, NodeId), // for #7264; see above
777777
ty_mac(mac),
778+
ty_typeof(@expr),
778779
// ty_infer means the type should be inferred instead of it having been
779780
// specified. This should only appear at the "top level" of a type and not
780781
// nested in one.

src/libsyntax/fold.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ pub fn noop_fold_ty(t: &ty_, fld: @ast_fold) -> ty_ {
697697
fld.fold_expr(e)
698698
)
699699
}
700+
ty_typeof(e) => ty_typeof(fld.fold_expr(e)),
700701
ty_mac(ref mac) => ty_mac(fold_mac(mac))
701702
}
702703
}

src/libsyntax/oldvisit.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ pub fn visit_ty<E:Clone>(t: &Ty, (e, v): (E, vt<E>)) {
279279
(v.visit_ty)(mt.ty, (e.clone(), v));
280280
(v.visit_expr)(ex, (e.clone(), v));
281281
},
282+
ty_typeof(ex) => {
283+
(v.visit_expr)(ex, (e.clone(), v));
284+
}
282285
ty_nil | ty_bot | ty_mac(_) | ty_infer => ()
283286
}
284287
}

src/libsyntax/parse/parser.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use ast::{struct_variant_kind, subtract};
5151
use ast::{sty_box, sty_region, sty_static, sty_uniq, sty_value};
5252
use ast::{token_tree, trait_method, trait_ref, tt_delim, tt_seq, tt_tok};
5353
use ast::{tt_nonterminal, tuple_variant_kind, Ty, ty_, ty_bot, ty_box};
54-
use ast::{TypeField, ty_fixed_length_vec, ty_closure, ty_bare_fn};
54+
use ast::{TypeField, ty_fixed_length_vec, ty_closure, ty_bare_fn, ty_typeof};
5555
use ast::{ty_infer, TypeMethod};
5656
use ast::{ty_nil, TyParam, TyParamBound, ty_path, ty_ptr, ty_rptr};
5757
use ast::{ty_tup, ty_u32, ty_uniq, ty_vec, uniq};
@@ -1105,6 +1105,13 @@ impl Parser {
11051105
let result = self.parse_ty_closure(ast::BorrowedSigil, None);
11061106
self.obsolete(*self.last_span, ObsoleteBareFnType);
11071107
result
1108+
} else if self.eat_keyword(keywords::Typeof) {
1109+
// TYPEOF
1110+
// In order to not be ambiguous, the type must be surrounded by parens.
1111+
self.expect(&token::LPAREN);
1112+
let e = self.parse_expr();
1113+
self.expect(&token::RPAREN);
1114+
ty_typeof(e)
11081115
} else if *self.token == token::MOD_SEP
11091116
|| is_ident_or_path(self.token) {
11101117
// NAMED TYPE

src/libsyntax/parse/token.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ fn mk_fresh_ident_interner() -> @ident_interner {
478478
"be", // 64
479479
"pure", // 65
480480
"yield", // 66
481+
"typeof", // 67
481482
];
482483

483484
@ident_interner {
@@ -595,6 +596,7 @@ pub mod keywords {
595596
True,
596597
Trait,
597598
Type,
599+
Typeof,
598600
Unsafe,
599601
Use,
600602
While,
@@ -639,6 +641,7 @@ pub mod keywords {
639641
True => ident { name: 57, ctxt: 0 },
640642
Trait => ident { name: 58, ctxt: 0 },
641643
Type => ident { name: 59, ctxt: 0 },
644+
Typeof => ident { name: 67, ctxt: 0 },
642645
Unsafe => ident { name: 60, ctxt: 0 },
643646
Use => ident { name: 61, ctxt: 0 },
644647
While => ident { name: 62, ctxt: 0 },
@@ -660,7 +663,7 @@ pub fn is_keyword(kw: keywords::Keyword, tok: &Token) -> bool {
660663
pub fn is_any_keyword(tok: &Token) -> bool {
661664
match *tok {
662665
token::IDENT(sid, false) => match sid.name {
663-
8 | 27 | 32 .. 66 => true,
666+
8 | 27 | 32 .. 67 => true,
664667
_ => false,
665668
},
666669
_ => false
@@ -680,7 +683,7 @@ pub fn is_strict_keyword(tok: &Token) -> bool {
680683
pub fn is_reserved_keyword(tok: &Token) -> bool {
681684
match *tok {
682685
token::IDENT(sid, false) => match sid.name {
683-
64 .. 66 => true,
686+
64 .. 67 => true,
684687
_ => false,
685688
},
686689
_ => false,

src/libsyntax/print/pprust.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,11 @@ pub fn print_type(s: @ps, ty: &ast::Ty) {
437437
print_expr(s, v);
438438
word(s.s, "]");
439439
}
440+
ast::ty_typeof(e) => {
441+
word(s.s, "typeof(");
442+
print_expr(s, e);
443+
word(s.s, ")");
444+
}
440445
ast::ty_mac(_) => {
441446
fail!("print_type doesn't know how to print a ty_mac");
442447
}

src/libsyntax/visit.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ pub fn walk_ty<E:Clone, V:Visitor<E>>(visitor: &mut V, typ: &Ty, env: E) {
314314
visitor.visit_ty(mutable_type.ty, env.clone());
315315
visitor.visit_expr(expression, env)
316316
}
317+
ty_typeof(expression) => {
318+
visitor.visit_expr(expression, env)
319+
}
317320
ty_nil | ty_bot | ty_mac(_) | ty_infer => ()
318321
}
319322
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2013 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+
fn main() {
12+
let typeof = (); //~ ERROR `typeof` is a reserved keyword
13+
}

0 commit comments

Comments
 (0)