Skip to content

Commit 7971c46

Browse files
committed
auto merge of #8718 : bblum/rust/typeof, r=pcwalton
r? anybody
2 parents da96b3e + 02f93ca commit 7971c46

File tree

11 files changed

+78
-4
lines changed

11 files changed

+78
-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
@@ -517,6 +517,9 @@ pub fn ast_ty_to_ty<AC:AstConv, RS:region_scope + Clone + 'static>(
517517
}
518518
}
519519
}
520+
ast::ty_typeof(_e) => {
521+
tcx.sess.span_bug(ast_ty.span, "typeof is reserved but unimplemented");
522+
}
520523
ast::ty_infer => {
521524
// ty_infer should only appear as the type of arguments or return
522525
// 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
@@ -791,6 +791,7 @@ pub enum ty_ {
791791
ty_tup(~[Ty]),
792792
ty_path(Path, Option<OptVec<TyParamBound>>, NodeId), // for #7264; see above
793793
ty_mac(mac),
794+
ty_typeof(@expr),
794795
// ty_infer means the type should be inferred instead of it having been
795796
// specified. This should only appear at the "top level" of a type and not
796797
// 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: 21 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};
@@ -1136,6 +1136,13 @@ impl Parser {
11361136
let result = self.parse_ty_closure(ast::BorrowedSigil, None);
11371137
self.obsolete(*self.last_span, ObsoleteBareFnType);
11381138
result
1139+
} else if self.eat_keyword(keywords::Typeof) {
1140+
// TYPEOF
1141+
// In order to not be ambiguous, the type must be surrounded by parens.
1142+
self.expect(&token::LPAREN);
1143+
let e = self.parse_expr();
1144+
self.expect(&token::RPAREN);
1145+
ty_typeof(e)
11391146
} else if *self.token == token::MOD_SEP
11401147
|| is_ident_or_path(self.token) {
11411148
// NAMED TYPE
@@ -3610,6 +3617,19 @@ impl Parser {
36103617
self.bump();
36113618
sty_value
36123619
}
3620+
token::BINOP(token::STAR) => {
3621+
// Possibly "*self" or "*mut self" -- not supported. Try to avoid
3622+
// emitting cryptic "unexpected token" errors.
3623+
self.bump();
3624+
if self.token_is_mutability(self.token) {
3625+
self.bump();
3626+
}
3627+
if self.is_self_ident() {
3628+
self.span_err(*self.span, "cannot pass self by unsafe pointer");
3629+
self.bump();
3630+
}
3631+
sty_value
3632+
}
36133633
_ => {
36143634
sty_static
36153635
}

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
@@ -435,6 +435,11 @@ pub fn print_type(s: @ps, ty: &ast::Ty) {
435435
print_expr(s, v);
436436
word(s.s, "]");
437437
}
438+
ast::ty_typeof(e) => {
439+
word(s.s, "typeof(");
440+
print_expr(s, e);
441+
word(s.s, ")");
442+
}
438443
ast::ty_mac(_) => {
439444
fail!("print_type doesn't know how to print a ty_mac");
440445
}

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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
trait A {
12+
fn foo(*mut self); //~ ERROR cannot pass self by unsafe pointer
13+
fn bar(*self); //~ ERROR cannot pass self by unsafe pointer
14+
}
15+
16+
struct X;
17+
impl A for X {
18+
fn foo(*mut self) { } //~ ERROR cannot pass self by unsafe pointer
19+
fn bar(*self) { } //~ ERROR cannot pass self by unsafe pointer
20+
}
21+
22+
fn main() { }

0 commit comments

Comments
 (0)