Skip to content

Commit

Permalink
auto merge of #10605 : huonw/rust/ascii-ident-gate, r=pcwalton
Browse files Browse the repository at this point in the history
  • Loading branch information
bors committed Nov 22, 2013
2 parents 09ed791 + ab2a99f commit fb279aa
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/librustc/front/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use syntax::attr::AttrMetaMethods;
use syntax::codemap::Span;
use syntax::visit;
use syntax::visit::Visitor;
use syntax::parse::token;

use driver::session::Session;

Expand All @@ -36,6 +37,7 @@ static KNOWN_FEATURES: &'static [(&'static str, Status)] = &[
("once_fns", Active),
("asm", Active),
("managed_boxes", Active),
("non_ascii_idents", Active),

// These are used to test this portion of the compiler, they don't actually
// mean anything
Expand Down Expand Up @@ -76,6 +78,15 @@ impl Context {
}

impl Visitor<()> for Context {
fn visit_ident(&mut self, sp: Span, id: ast::Ident, _: ()) {
let s = token::ident_to_str(&id);

if !s.is_ascii() {
self.gate_feature("non_ascii_idents", sp,
"non-ascii idents are not fully supported.");
}
}

fn visit_view_item(&mut self, i: &ast::view_item, _: ()) {
match i.node {
ast::view_item_use(ref paths) => {
Expand Down
48 changes: 45 additions & 3 deletions src/libsyntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ pub fn generics_of_fn(fk: &fn_kind) -> Generics {
}

pub trait Visitor<E:Clone> {
fn visit_ident(&mut self, _sp: Span, _ident: Ident, _e: E) {
/*! Visit the idents */
}
fn visit_mod(&mut self, m:&_mod, _s:Span, _n:NodeId, e:E) { walk_mod(self, m, e) }
fn visit_view_item(&mut self, i:&view_item, e:E) { walk_view_item(self, i, e) }
fn visit_foreign_item(&mut self, i:@foreign_item, e:E) { walk_foreign_item(self, i, e) }
Expand Down Expand Up @@ -132,8 +135,31 @@ pub fn walk_mod<E:Clone, V:Visitor<E>>(visitor: &mut V, module: &_mod, env: E) {
}
}

pub fn walk_view_item<E:Clone, V:Visitor<E>>(_: &mut V, _: &view_item, _: E) {
// Empty!
pub fn walk_view_item<E:Clone, V:Visitor<E>>(visitor: &mut V, vi: &view_item, env: E) {
match vi.node {
view_item_extern_mod(name, _, _, _) => {
visitor.visit_ident(vi.span, name, env)
}
view_item_use(ref paths) => {
for vp in paths.iter() {
let path = match vp.node {
view_path_simple(ident, ref path, _) => {
visitor.visit_ident(vp.span, ident, env.clone());
path
}
view_path_glob(ref path, _) => path,
view_path_list(ref path, ref list, _) => {
for id in list.iter() {
visitor.visit_ident(id.span, id.node.name, env.clone())
}
path
}
};

walk_path(visitor, path, env.clone());
}
}
}
}

pub fn walk_local<E:Clone, V:Visitor<E>>(visitor: &mut V, local: &Local, env: E) {
Expand Down Expand Up @@ -164,6 +190,7 @@ fn walk_trait_ref<E:Clone, V:Visitor<E>>(visitor: &mut V,
}

pub fn walk_item<E:Clone, V:Visitor<E>>(visitor: &mut V, item: &item, env: E) {
visitor.visit_ident(item.span, item.ident, env.clone());
match item.node {
item_static(ref typ, _, expr) => {
visitor.visit_ty(typ, env.clone());
Expand Down Expand Up @@ -243,6 +270,8 @@ pub fn walk_variant<E:Clone, V:Visitor<E>>(visitor:&mut V,
variant: &variant,
generics: &Generics,
env: E) {
visitor.visit_ident(variant.span, variant.node.name, env.clone());

match variant.node.kind {
tuple_variant_kind(ref variant_arguments) => {
for variant_argument in variant_arguments.iter() {
Expand Down Expand Up @@ -328,6 +357,8 @@ fn walk_lifetime_decls<E:Clone, V:Visitor<E>>(visitor: &mut V,

pub fn walk_path<E:Clone, V:Visitor<E>>(visitor: &mut V, path: &Path, env: E) {
for segment in path.segments.iter() {
visitor.visit_ident(path.span, segment.identifier, env.clone());

for typ in segment.types.iter() {
visitor.visit_ty(typ, env.clone());
}
Expand Down Expand Up @@ -393,6 +424,8 @@ pub fn walk_pat<E:Clone, V:Visitor<E>>(visitor: &mut V, pattern: &Pat, env: E) {
pub fn walk_foreign_item<E:Clone, V:Visitor<E>>(visitor: &mut V,
foreign_item: &foreign_item,
env: E) {
visitor.visit_ident(foreign_item.span, foreign_item.ident, env.clone());

match foreign_item.node {
foreign_item_fn(ref function_declaration, ref generics) => {
walk_fn_decl(visitor, function_declaration, env.clone());
Expand Down Expand Up @@ -441,6 +474,7 @@ pub fn walk_fn_decl<E:Clone, V:Visitor<E>>(visitor: &mut V,
pub fn walk_method_helper<E:Clone, V:Visitor<E>>(visitor: &mut V,
method: &method,
env: E) {
visitor.visit_ident(method.span, method.ident, env.clone());
visitor.visit_fn(&fk_method(method.ident, &method.generics, method),
&method.decl,
&method.body,
Expand Down Expand Up @@ -477,12 +511,13 @@ pub fn walk_fn<E:Clone, V:Visitor<E>>(visitor: &mut V,
pub fn walk_ty_method<E:Clone, V:Visitor<E>>(visitor: &mut V,
method_type: &TypeMethod,
env: E) {
visitor.visit_ident(method_type.span, method_type.ident, env.clone());
visitor.visit_explicit_self(&method_type.explicit_self, env.clone());
for argument_type in method_type.decl.inputs.iter() {
visitor.visit_ty(&argument_type.ty, env.clone())
}
visitor.visit_generics(&method_type.generics, env.clone());
visitor.visit_ty(&method_type.decl.output, env.clone())
visitor.visit_ty(&method_type.decl.output, env);
}

pub fn walk_trait_method<E:Clone, V:Visitor<E>>(visitor: &mut V,
Expand Down Expand Up @@ -510,6 +545,13 @@ pub fn walk_struct_def<E:Clone, V:Visitor<E>>(visitor: &mut V,
pub fn walk_struct_field<E:Clone, V:Visitor<E>>(visitor: &mut V,
struct_field: &struct_field,
env: E) {
match struct_field.node.kind {
named_field(name, _) => {
visitor.visit_ident(struct_field.span, name, env.clone())
}
_ => {}
}

visitor.visit_ty(&struct_field.node.ty, env)
}

Expand Down
47 changes: 47 additions & 0 deletions src/test/compile-fail/gated-non-ascii-idents.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// xfail-fast feature doesn't work.

#[feature(struct_variant)];

extern mod bäz; //~ ERROR non-ascii idents

use föö::bar; //~ ERROR non-ascii idents

mod föö { //~ ERROR non-ascii idents
pub fn bar() {}
}

fn bär( //~ ERROR non-ascii idents
bäz: int //~ ERROR non-ascii idents
) {
let: int; //~ ERROR non-ascii idents

match (1, 2) {
(, _) => {} //~ ERROR non-ascii idents
}
}

struct Föö { //~ ERROR non-ascii idents
föö: int //~ ERROR non-ascii idents
}

enum Bär { //~ ERROR non-ascii idents
Bäz { //~ ERROR non-ascii idents
qüx: int //~ ERROR non-ascii idents
}
}

extern {
fn qüx(); //~ ERROR non-ascii idents
}

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// xfail-fast feature doesn't work.

#[forbid(non_camel_case_types)];
#[forbid(non_uppercase_statics)];
#[feature(non_ascii_idents)];

// Some scripts (e.g. hiragana) don't have a concept of
// upper/lowercase
Expand Down
4 changes: 4 additions & 0 deletions src/test/run-pass/utf8_idents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// xfail-fast feature doesn't work.

#[feature(non_ascii_idents)];

use std::num;

pub fn main() {
Expand Down

0 comments on commit fb279aa

Please sign in to comment.