From e9f6f3f2ccf563b39f7484025c8272da60c24dfa Mon Sep 17 00:00:00 2001 From: Ben Blum Date: Thu, 22 Aug 2013 17:00:02 -0400 Subject: Parse and reserve typeof keyword. #3228 --- src/libsyntax/parse/parser.rs | 9 ++++++++- src/libsyntax/parse/token.rs | 7 +++++-- 2 files changed, 13 insertions(+), 3 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 9aa2bf3f063..51213512a55 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -51,7 +51,7 @@ use ast::{struct_variant_kind, subtract}; use ast::{sty_box, sty_region, sty_static, sty_uniq, sty_value}; use ast::{token_tree, trait_method, trait_ref, tt_delim, tt_seq, tt_tok}; use ast::{tt_nonterminal, tuple_variant_kind, Ty, ty_, ty_bot, ty_box}; -use ast::{TypeField, ty_fixed_length_vec, ty_closure, ty_bare_fn}; +use ast::{TypeField, ty_fixed_length_vec, ty_closure, ty_bare_fn, ty_typeof}; use ast::{ty_infer, TypeMethod}; use ast::{ty_nil, TyParam, TyParamBound, ty_path, ty_ptr, ty_rptr}; use ast::{ty_tup, ty_u32, ty_uniq, ty_vec, uniq}; @@ -1105,6 +1105,13 @@ impl Parser { let result = self.parse_ty_closure(ast::BorrowedSigil, None); self.obsolete(*self.last_span, ObsoleteBareFnType); result + } else if self.eat_keyword(keywords::Typeof) { + // TYPEOF + // In order to not be ambiguous, the type must be surrounded by parens. + self.expect(&token::LPAREN); + let e = self.parse_expr(); + self.expect(&token::RPAREN); + ty_typeof(e) } else if *self.token == token::MOD_SEP || is_ident_or_path(self.token) { // NAMED TYPE diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 0d7def84003..8128a4e905c 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -478,6 +478,7 @@ fn mk_fresh_ident_interner() -> @ident_interner { "be", // 64 "pure", // 65 "yield", // 66 + "typeof", // 67 ]; @ident_interner { @@ -595,6 +596,7 @@ pub mod keywords { True, Trait, Type, + Typeof, Unsafe, Use, While, @@ -639,6 +641,7 @@ pub mod keywords { True => ident { name: 57, ctxt: 0 }, Trait => ident { name: 58, ctxt: 0 }, Type => ident { name: 59, ctxt: 0 }, + Typeof => ident { name: 67, ctxt: 0 }, Unsafe => ident { name: 60, ctxt: 0 }, Use => ident { name: 61, ctxt: 0 }, While => ident { name: 62, ctxt: 0 }, @@ -660,7 +663,7 @@ pub fn is_keyword(kw: keywords::Keyword, tok: &Token) -> bool { pub fn is_any_keyword(tok: &Token) -> bool { match *tok { token::IDENT(sid, false) => match sid.name { - 8 | 27 | 32 .. 66 => true, + 8 | 27 | 32 .. 67 => true, _ => false, }, _ => false @@ -680,7 +683,7 @@ pub fn is_strict_keyword(tok: &Token) -> bool { pub fn is_reserved_keyword(tok: &Token) -> bool { match *tok { token::IDENT(sid, false) => match sid.name { - 64 .. 66 => true, + 64 .. 67 => true, _ => false, }, _ => false, -- cgit 1.4.1-3-g733a5 From 02f93ca32481d3b1a6a307d28223ca061348eb0c Mon Sep 17 00:00:00 2001 From: Ben Blum Date: Thu, 22 Aug 2013 20:15:09 -0400 Subject: Emit a better error for attempted unsafe-pointer-self. Close #8306. --- src/libsyntax/parse/parser.rs | 13 +++++++++++++ src/test/compile-fail/no-unsafe-self.rs | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/test/compile-fail/no-unsafe-self.rs (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 51213512a55..6db9828fa25 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3571,6 +3571,19 @@ impl Parser { self.bump(); sty_value } + token::BINOP(token::STAR) => { + // Possibly "*self" or "*mut self" -- not supported. Try to avoid + // emitting cryptic "unexpected token" errors. + self.bump(); + if self.token_is_mutability(self.token) { + self.bump(); + } + if self.is_self_ident() { + self.span_err(*self.span, "cannot pass self by unsafe pointer"); + self.bump(); + } + sty_value + } _ => { sty_static } diff --git a/src/test/compile-fail/no-unsafe-self.rs b/src/test/compile-fail/no-unsafe-self.rs new file mode 100644 index 00000000000..0bf73bbdfe3 --- /dev/null +++ b/src/test/compile-fail/no-unsafe-self.rs @@ -0,0 +1,22 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +trait A { + fn foo(*mut self); //~ ERROR cannot pass self by unsafe pointer + fn bar(*self); //~ ERROR cannot pass self by unsafe pointer +} + +struct X; +impl A for X { + fn foo(*mut self) { } //~ ERROR cannot pass self by unsafe pointer + fn bar(*self) { } //~ ERROR cannot pass self by unsafe pointer +} + +fn main() { } -- cgit 1.4.1-3-g733a5