From 5754848f8cd06bd3fc2bb084b5ca7bd41974e1b5 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Sun, 20 Oct 2013 01:55:23 -0400 Subject: libsyntax/librustc: Allow specifying mut on by-value self. --- src/libsyntax/parse/parser.rs | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'src/libsyntax/parse/parser.rs') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index ed6019e1a55..133934a746a 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3438,15 +3438,11 @@ impl Parser { // parse the argument list and result type of a function // that may have a self type. - fn parse_fn_decl_with_self( - &self, - parse_arg_fn: - &fn(&Parser) -> arg - ) -> (explicit_self, fn_decl) { - fn maybe_parse_explicit_self( - cnstr: &fn(v: Mutability) -> ast::explicit_self_, - p: &Parser - ) -> ast::explicit_self_ { + fn parse_fn_decl_with_self(&self, parse_arg_fn: &fn(&Parser) -> arg) + -> (explicit_self, fn_decl) { + + fn maybe_parse_explicit_self(cnstr: &fn(v: Mutability) -> ast::explicit_self_, + p: &Parser) -> ast::explicit_self_ { // We need to make sure it isn't a type if p.look_ahead(1, |t| token::is_keyword(keywords::Self, t)) || ((p.look_ahead(1, |t| token::is_keyword(keywords::Const, t)) || @@ -3529,20 +3525,26 @@ impl Parser { } token::IDENT(*) if self.is_self_ident() => { self.bump(); - sty_value + sty_value(MutImmutable) } 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(); - } + let mutability = if self.token_is_mutability(self.token) { + self.parse_mutability() + } else { MutImmutable }; if self.is_self_ident() { self.span_err(*self.span, "cannot pass self by unsafe pointer"); self.bump(); } - sty_value + sty_value(mutability) + } + _ if self.token_is_mutability(self.token) && + self.look_ahead(1, |t| token::is_keyword(keywords::Self, t)) => { + let mutability = self.parse_mutability(); + self.expect_self_ident(); + sty_value(mutability) } _ => { sty_static -- cgit 1.4.1-3-g733a5 From af163579ed674c4b1a37f5c8d50bf348722db439 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Sun, 20 Oct 2013 02:34:01 -0400 Subject: libsyntax/librustc: Allow specifying mut on ~self. --- src/librustc/metadata/decoder.rs | 2 +- src/librustc/metadata/encoder.rs | 3 ++- src/librustc/middle/liveness.rs | 2 +- src/librustc/middle/resolve.rs | 2 +- src/librustc/middle/typeck/astconv.rs | 2 +- src/librustc/middle/typeck/check/method.rs | 2 +- src/librustdoc/clean.rs | 2 +- src/libsyntax/ast.rs | 2 +- src/libsyntax/ext/deriving/ty.rs | 2 +- src/libsyntax/parse/parser.rs | 10 +++++++++- src/libsyntax/print/pprust.rs | 5 ++++- 11 files changed, 23 insertions(+), 11 deletions(-) (limited to 'src/libsyntax/parse/parser.rs') diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index ca1912c8347..10232730329 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -977,7 +977,7 @@ fn get_explicit_self(item: ebml::Doc) -> ast::explicit_self_ { 's' => { return ast::sty_static; } 'v' => { return ast::sty_value(get_mutability(string[1])); } '@' => { return ast::sty_box(get_mutability(string[1])); } - '~' => { return ast::sty_uniq; } + '~' => { return ast::sty_uniq(get_mutability(string[1])); } '&' => { // FIXME(#4846) expl. region return ast::sty_region(None, get_mutability(string[1])); diff --git a/src/librustc/metadata/encoder.rs b/src/librustc/metadata/encoder.rs index 0f5ce06a7da..bae0dcc2a52 100644 --- a/src/librustc/metadata/encoder.rs +++ b/src/librustc/metadata/encoder.rs @@ -675,8 +675,9 @@ fn encode_explicit_self(ebml_w: &mut writer::Encoder, explicit_self: ast::explic ebml_w.writer.write(&[ '@' as u8 ]); encode_mutability(ebml_w, m); } - sty_uniq => { + sty_uniq(m) => { ebml_w.writer.write(&[ '~' as u8 ]); + encode_mutability(ebml_w, m); } } diff --git a/src/librustc/middle/liveness.rs b/src/librustc/middle/liveness.rs index a235d7395a6..71934c9f2a7 100644 --- a/src/librustc/middle/liveness.rs +++ b/src/librustc/middle/liveness.rs @@ -392,7 +392,7 @@ fn visit_fn(v: &mut LivenessVisitor, match *fk { visit::fk_method(_, _, method) => { match method.explicit_self.node { - sty_value(_) | sty_region(*) | sty_box(_) | sty_uniq => { + sty_value(_) | sty_region(*) | sty_box(_) | sty_uniq(_) => { fn_maps.add_variable(Arg(method.self_id, special_idents::self_)); } diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index 4c5d2f86a1b..ee36b807ac7 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -3801,7 +3801,7 @@ impl Resolver { } HasSelfBinding(self_node_id, explicit_self) => { let mutable = match explicit_self.node { - sty_value(m) if m == MutMutable => true, + sty_uniq(m) | sty_value(m) if m == MutMutable => true, _ => false }; let def_like = DlDef(DefSelf(self_node_id, mutable)); diff --git a/src/librustc/middle/typeck/astconv.rs b/src/librustc/middle/typeck/astconv.rs index e703f6da416..dd0c6c12a69 100644 --- a/src/librustc/middle/typeck/astconv.rs +++ b/src/librustc/middle/typeck/astconv.rs @@ -689,7 +689,7 @@ fn ty_of_method_or_bare_fn( ty::mt {ty: self_info.untransformed_self_ty, mutbl: mutability})) } - ast::sty_uniq => { + ast::sty_uniq(_) => { Some(ty::mk_uniq(this.tcx(), ty::mt {ty: self_info.untransformed_self_ty, mutbl: ast::MutImmutable})) diff --git a/src/librustc/middle/typeck/check/method.rs b/src/librustc/middle/typeck/check/method.rs index b48e1f7fcf1..af1d5ce3cc6 100644 --- a/src/librustc/middle/typeck/check/method.rs +++ b/src/librustc/middle/typeck/check/method.rs @@ -1236,7 +1236,7 @@ impl<'self> LookupContext<'self> { } } - sty_uniq => { + sty_uniq(_) => { debug!("(is relevant?) explicit self is a unique pointer"); match ty::get(rcvr_ty).sty { ty::ty_uniq(mt) => { diff --git a/src/librustdoc/clean.rs b/src/librustdoc/clean.rs index 90fc71aa863..dd1ad8263da 100644 --- a/src/librustdoc/clean.rs +++ b/src/librustdoc/clean.rs @@ -389,7 +389,7 @@ impl Clean for ast::explicit_self { match self.node { ast::sty_static => SelfStatic, ast::sty_value(_) => SelfValue, - ast::sty_uniq => SelfOwned, + ast::sty_uniq(_) => SelfOwned, ast::sty_region(lt, mt) => SelfBorrowed(lt.clean(), mt.clean()), ast::sty_box(mt) => SelfManaged(mt.clean()), } diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 6631924239d..372f1950c1d 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -924,7 +924,7 @@ pub enum explicit_self_ { sty_value(Mutability), // `self` sty_region(Option, Mutability), // `&'lt self` sty_box(Mutability), // `@self` - sty_uniq // `~self` + sty_uniq(Mutability) // `~self` } pub type explicit_self = Spanned; diff --git a/src/libsyntax/ext/deriving/ty.rs b/src/libsyntax/ext/deriving/ty.rs index a9fdafc8014..c60259304ae 100644 --- a/src/libsyntax/ext/deriving/ty.rs +++ b/src/libsyntax/ext/deriving/ty.rs @@ -246,7 +246,7 @@ pub fn get_explicit_self(cx: @ExtCtxt, span: Span, self_ptr: &Option) let self_ty = respan( span, match *ptr { - Send => ast::sty_uniq, + Send => ast::sty_uniq(ast::MutImmutable), Managed(mutbl) => ast::sty_box(mutbl), Borrowed(ref lt, mutbl) => { let lt = lt.map(|s| cx.lifetime(span, cx.ident_of(s))); diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 133934a746a..605e259cf0c 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -3520,7 +3520,7 @@ impl Parser { self.span_err(*self.last_span, "mutability declaration not allowed here"); } - sty_uniq + sty_uniq(MutImmutable) }, self) } token::IDENT(*) if self.is_self_ident() => { @@ -3546,6 +3546,14 @@ impl Parser { self.expect_self_ident(); sty_value(mutability) } + _ if self.token_is_mutability(self.token) && + self.look_ahead(1, |t| *t == token::TILDE) && + self.look_ahead(2, |t| token::is_keyword(keywords::Self, t)) => { + let mutability = self.parse_mutability(); + self.bump(); + self.expect_self_ident(); + sty_uniq(mutability) + } _ => { sty_static } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 9ce28e4d55a..0e330da31e6 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1690,7 +1690,10 @@ pub fn print_explicit_self(s: @ps, explicit_self: ast::explicit_self_) -> bool { print_mutability(s, m); word(s.s, "self"); } - ast::sty_uniq => { word(s.s, "~self"); } + ast::sty_uniq(m) => { + print_mutability(s, m); + word(s.s, "~self"); + } ast::sty_region(ref lt, m) => { word(s.s, "&"); print_opt_lifetime(s, lt); -- cgit 1.4.1-3-g733a5