Skip to content

Commit 5f8cf48

Browse files
committed
unit-test symbol-names and item-paths
1 parent 767c789 commit 5f8cf48

File tree

6 files changed

+146
-0
lines changed

6 files changed

+146
-0
lines changed

src/librustc_trans/trans/base.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ use trans::machine::{llalign_of_min, llsize_of, llsize_of_real};
8585
use trans::meth;
8686
use trans::mir;
8787
use trans::monomorphize::{self, Instance};
88+
use trans::symbol_names_test;
8889
use trans::tvec;
8990
use trans::type_::Type;
9091
use trans::type_of;
@@ -2758,6 +2759,8 @@ pub fn trans_crate<'tcx>(tcx: &TyCtxt<'tcx>,
27582759
}
27592760

27602761
collector::print_collection_results(&ccx);
2762+
2763+
symbol_names_test::report_symbol_names(&ccx);
27612764
}
27622765

27632766
emit_link_guard_if_necessary(&shared_ccx);

src/librustc_trans/trans/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ mod meth;
6060
mod mir;
6161
mod monomorphize;
6262
mod collector;
63+
mod symbol_names_test;
6364
mod tvec;
6465
mod type_;
6566
mod type_of;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2012-2015 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+
//! Walks the crate looking for items/impl-items/trait-items that have
12+
//! either a `rustc_symbol_name` or `rustc_item_path` attribute and
13+
//! generates an error giving, respectively, the symbol name or
14+
//! item-path. This is used for unit testing the code that generates
15+
//! paths etc in all kinds of annoying scenarios.
16+
17+
use back::symbol_names;
18+
use rustc::middle::ty::TyCtxt;
19+
use rustc_front::hir;
20+
use rustc_front::intravisit::{self, Visitor};
21+
use syntax::ast;
22+
use syntax::attr::AttrMetaMethods;
23+
use trans::common::CrateContext;
24+
25+
const SYMBOL_NAME: &'static str = "rustc_symbol_name";
26+
const ITEM_PATH: &'static str = "rustc_item_path";
27+
28+
pub fn report_symbol_names(ccx: &CrateContext) {
29+
// if the `rustc_attrs` feature is not enabled, then the
30+
// attributes we are interested in cannot be present anyway, so
31+
// skip the walk.
32+
let tcx = ccx.tcx();
33+
if !tcx.sess.features.borrow().rustc_attrs {
34+
return;
35+
}
36+
37+
let _ignore = tcx.dep_graph.in_ignore();
38+
let mut visitor = SymbolNamesTest { ccx: ccx, tcx: tcx };
39+
tcx.map.krate().visit_all_items(&mut visitor);
40+
}
41+
42+
struct SymbolNamesTest<'a, 'tcx:'a> {
43+
ccx: &'a CrateContext<'a, 'tcx>,
44+
tcx: &'a TyCtxt<'tcx>,
45+
}
46+
47+
impl<'a, 'tcx> SymbolNamesTest<'a, 'tcx> {
48+
fn process_attrs(&mut self,
49+
node_id: ast::NodeId) {
50+
let def_id = self.tcx.map.local_def_id(node_id);
51+
for attr in self.tcx.get_attrs(def_id).iter() {
52+
if attr.check_name(SYMBOL_NAME) {
53+
// for now, just monomorphic names
54+
let name = symbol_names::exported_name(self.ccx, def_id, &[]);
55+
self.tcx.sess.span_err(attr.span, &format!("symbol-name({})", name));
56+
} else if attr.check_name(ITEM_PATH) {
57+
let path = self.tcx.item_path_str(def_id);
58+
self.tcx.sess.span_err(attr.span, &format!("item-path({})", path));
59+
}
60+
61+
// (*) The formatting of `tag({})` is chosen so that tests can elect
62+
// to test the entirety of the string, if they choose, or else just
63+
// some subset.
64+
}
65+
}
66+
}
67+
68+
impl<'a, 'tcx> Visitor<'tcx> for SymbolNamesTest<'a, 'tcx> {
69+
fn visit_item(&mut self, item: &'tcx hir::Item) {
70+
self.process_attrs(item.id);
71+
intravisit::walk_item(self, item);
72+
}
73+
74+
fn visit_trait_item(&mut self, ti: &'tcx hir::TraitItem) {
75+
self.process_attrs(ti.id);
76+
intravisit::walk_trait_item(self, ti)
77+
}
78+
79+
fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem) {
80+
self.process_attrs(ii.id);
81+
intravisit::walk_impl_item(self, ii)
82+
}
83+
}
84+

src/libsyntax/feature_gate.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ pub const KNOWN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeGat
346346
"the `#[rustc_if_this_changed]` attribute \
347347
is just used for rustc unit tests \
348348
and will never be stable")),
349+
("rustc_symbol_name", Whitelisted, Gated("rustc_attrs",
350+
"internal rustc attributes will never be stable")),
351+
("rustc_item_path", Whitelisted, Gated("rustc_attrs",
352+
"internal rustc attributes will never be stable")),
349353
("rustc_move_fragments", Normal, Gated("rustc_attrs",
350354
"the `#[rustc_move_fragments]` attribute \
351355
is just used for rustc unit tests \
@@ -573,6 +577,7 @@ pub struct Features {
573577
pub const_indexing: bool,
574578
pub static_recursion: bool,
575579
pub default_type_parameter_fallback: bool,
580+
pub rustc_attrs: bool,
576581
pub type_macros: bool,
577582
pub cfg_target_feature: bool,
578583
pub cfg_target_vendor: bool,
@@ -608,6 +613,7 @@ impl Features {
608613
const_indexing: false,
609614
static_recursion: false,
610615
default_type_parameter_fallback: false,
616+
rustc_attrs: false,
611617
type_macros: false,
612618
cfg_target_feature: false,
613619
cfg_target_vendor: false,
@@ -1219,6 +1225,7 @@ fn check_crate_inner<F>(cm: &CodeMap, span_handler: &Handler,
12191225
const_indexing: cx.has_feature("const_indexing"),
12201226
static_recursion: cx.has_feature("static_recursion"),
12211227
default_type_parameter_fallback: cx.has_feature("default_type_parameter_fallback"),
1228+
rustc_attrs: cx.has_feature("rustc_attrs"),
12221229
type_macros: cx.has_feature("type_macros"),
12231230
cfg_target_feature: cx.has_feature("cfg_target_feature"),
12241231
cfg_target_vendor: cx.has_feature("cfg_target_vendor"),
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2012-2015 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+
#![feature(rustc_attrs)]
12+
13+
#[rustc_symbol_name] //~ ERROR _ZN5basic4main
14+
#[rustc_item_path] //~ ERROR item-path(main)
15+
fn main() {
16+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2012-2015 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+
#![feature(rustc_attrs)]
12+
#![allow(dead_code)]
13+
14+
mod foo {
15+
pub struct Foo { x: u32 }
16+
17+
impl Foo {
18+
#[rustc_symbol_name] //~ ERROR _ZN5impl13foo3Foo3bar
19+
#[rustc_item_path] //~ ERROR item-path(foo::Foo::bar)
20+
fn bar() { }
21+
}
22+
}
23+
24+
mod bar {
25+
use foo::Foo;
26+
27+
impl Foo {
28+
#[rustc_symbol_name] //~ ERROR _ZN5impl13bar26_$LT$impl$u20$foo..Foo$GT$3baz
29+
#[rustc_item_path] //~ ERROR item-path(bar::<impl foo::Foo>::baz)
30+
fn baz() { }
31+
}
32+
}
33+
34+
fn main() {
35+
}

0 commit comments

Comments
 (0)