From 830a833688583f449fd30f37a9d375ec8b8aac6a Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 7 May 2016 21:43:17 +0300 Subject: [PATCH] resolve: Make `self` in value namespace unhygienic --- src/librustc/hir/lowering.rs | 8 +++++-- src/test/run-pass/self-unhygienic.rs | 31 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 src/test/run-pass/self-unhygienic.rs diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 0876e609396c5..6e232d0041604 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -53,7 +53,7 @@ use syntax::errors::Handler; use syntax::ext::mtwt; use syntax::ptr::P; use syntax::codemap::{respan, Spanned, Span}; -use syntax::parse::token; +use syntax::parse::token::{self, keywords}; use syntax::std_inject; use syntax::visit::{self, Visitor}; @@ -141,7 +141,11 @@ impl<'a, 'hir> LoweringContext<'a> { pub fn lower_ident(_lctx: &LoweringContext, ident: Ident) -> hir::Ident { hir::Ident { - name: mtwt::resolve(ident), + name: if ident.name != keywords::SelfValue.name() { + mtwt::resolve(ident) + } else { + ident.name + }, unhygienic_name: ident.name, } } diff --git a/src/test/run-pass/self-unhygienic.rs b/src/test/run-pass/self-unhygienic.rs new file mode 100644 index 0000000000000..922ece5fdf89a --- /dev/null +++ b/src/test/run-pass/self-unhygienic.rs @@ -0,0 +1,31 @@ +// Copyright 2016 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. + +// Check that `self` is unhygienic + +struct A { + pub v: i64 +} + +macro_rules! pretty { + ( $t:ty => $body:block ) => ( + impl $t { + pub fn pretty(&self) -> String $body + } + ) +} + +pretty! (A => { + format!("", self.v) +}); + +fn main() { + assert_eq!(format!("Pretty: {}", A { v: 3 }.pretty()), "Pretty: "); +}