Skip to content

Commit 8821aff

Browse files
committed
rustc: Move some attr methods to queries
Otherwise we may emit double errors related to the `#[export_name]` attribute, for example, and using a query should ensure that it's only emitted at most once.
1 parent 132bde7 commit 8821aff

File tree

10 files changed

+69
-58
lines changed

10 files changed

+69
-58
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,8 @@ define_dep_nodes!( <'tcx>
577577
[] AllCrateNums,
578578
[] ExportedSymbols,
579579
[] CollectAndPartitionTranslationItems,
580+
[] ExportName(DefId),
581+
[] ContainsExternIndicator(DefId),
580582
);
581583

582584
trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {

src/librustc/middle/reachable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
233233
} else {
234234
false
235235
};
236-
let is_extern = attr::contains_extern_indicator(&self.tcx.sess.diagnostic(),
237-
&item.attrs);
236+
let def_id = self.tcx.hir.local_def_id(item.id);
237+
let is_extern = self.tcx.contains_extern_indicator(def_id);
238238
if reachable || is_extern {
239239
self.reachable_symbols.insert(search_item);
240240
}

src/librustc/middle/trans.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
// Copyright 2017 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+
111
use syntax::ast::NodeId;
212
use syntax::symbol::InternedString;
313
use ty::Instance;

src/librustc/ty/maps.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,8 @@ define_maps! { <'tcx>
13921392
[] fn collect_and_partition_translation_items:
13931393
collect_and_partition_translation_items_node(CrateNum)
13941394
-> (Arc<FxHashSet<TransItem<'tcx>>>, Vec<Arc<CodegenUnit<'tcx>>>),
1395+
[] fn export_name: ExportName(DefId) -> Option<Symbol>,
1396+
[] fn contains_extern_indicator: ContainsExternIndicator(DefId) -> bool,
13951397
}
13961398

13971399
fn type_param_predicates<'tcx>((item_id, param_id): (DefId, DefId)) -> DepConstructor<'tcx> {

src/librustc_trans/back/symbol_export.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use rustc::ty::TyCtxt;
1717
use rustc_allocator::ALLOCATOR_METHODS;
1818
use rustc::middle::exported_symbols::{ExportedSymbols, SymbolExportLevel};
1919
use rustc::middle::exported_symbols::is_below_threshold;
20-
use syntax::attr;
2120

2221
pub fn compute<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> ExportedSymbols {
2322
let export_threshold = crates_export_threshold(&tcx.sess.crate_types.borrow());
@@ -137,11 +136,8 @@ pub fn compute<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> ExportedSymbols {
137136

138137
return ExportedSymbols::new(export_threshold, exports, local_exports);
139138

140-
fn export_level(tcx: TyCtxt,
141-
sym_def_id: DefId)
142-
-> SymbolExportLevel {
143-
let attrs = tcx.get_attrs(sym_def_id);
144-
if attr::contains_extern_indicator(tcx.sess.diagnostic(), &attrs) {
139+
fn export_level(tcx: TyCtxt, sym_def_id: DefId) -> SymbolExportLevel {
140+
if tcx.contains_extern_indicator(sym_def_id) {
145141
SymbolExportLevel::C
146142
} else {
147143
SymbolExportLevel::Rust

src/librustc_trans/back/symbol_names.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,30 @@ pub fn provide(providers: &mut Providers) {
119119
*providers = Providers {
120120
def_symbol_name,
121121
symbol_name,
122+
123+
export_name: |tcx, id| {
124+
tcx.get_attrs(id).iter().fold(None, |ia, attr| {
125+
if attr.check_name("export_name") {
126+
if let s @ Some(_) = attr.value_str() {
127+
s
128+
} else {
129+
struct_span_err!(tcx.sess, attr.span, E0558,
130+
"export_name attribute has invalid format")
131+
.span_label(attr.span, "did you mean #[export_name=\"*\"]?")
132+
.emit();
133+
None
134+
}
135+
} else {
136+
ia
137+
}
138+
})
139+
},
140+
141+
contains_extern_indicator: |tcx, id| {
142+
attr::contains_name(&tcx.get_attrs(id), "no_mangle") ||
143+
tcx.export_name(id).is_some()
144+
},
145+
122146
..*providers
123147
};
124148
}
@@ -245,7 +269,7 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance
245269
return tcx.item_name(def_id).to_string();
246270
}
247271

248-
if let Some(name) = attr::find_export_name_attr(tcx.sess.diagnostic(), &attrs) {
272+
if let Some(name) = tcx.export_name(def_id) {
249273
// Use provided name
250274
return name.to_string();
251275
}

src/librustc_trans/diagnostics.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,28 @@ extern "platform-intrinsic" {
4646
unsafe { simd_add(i32x1(0), i32x1(1)); } // ok!
4747
```
4848
"##,
49+
50+
E0558: r##"
51+
The `export_name` attribute was malformed.
52+
53+
Erroneous code example:
54+
55+
```ignore (error-emitted-at-codegen-which-cannot-be-handled-by-compile_fail)
56+
#[export_name] // error: export_name attribute has invalid format
57+
pub fn something() {}
58+
59+
fn main() {}
60+
```
61+
62+
The `export_name` attribute expects a string in order to determine the name of
63+
the exported symbol. Example:
64+
65+
```
66+
#[export_name = "some_function"] // ok!
67+
pub fn something() {}
68+
69+
fn main() {}
70+
```
71+
"##,
72+
4973
}

src/librustc_trans/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ use rustc::middle::cstore::{NativeLibrary, CrateSource, LibSource};
7676
use rustc::ty::maps::Providers;
7777
use rustc::util::nodemap::{FxHashSet, FxHashMap};
7878

79+
mod diagnostics;
80+
7981
pub mod back {
8082
mod archive;
8183
mod command;
@@ -88,8 +90,6 @@ pub mod back {
8890
mod rpath;
8991
}
9092

91-
mod diagnostics;
92-
9393
mod abi;
9494
mod adt;
9595
mod allocator;

src/libsyntax/attr.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -506,30 +506,6 @@ pub fn find_crate_name(attrs: &[Attribute]) -> Option<Symbol> {
506506
first_attr_value_str_by_name(attrs, "crate_name")
507507
}
508508

509-
/// Find the value of #[export_name=*] attribute and check its validity.
510-
pub fn find_export_name_attr(diag: &Handler, attrs: &[Attribute]) -> Option<Symbol> {
511-
attrs.iter().fold(None, |ia,attr| {
512-
if attr.check_name("export_name") {
513-
if let s@Some(_) = attr.value_str() {
514-
s
515-
} else {
516-
struct_span_err!(diag, attr.span, E0558,
517-
"export_name attribute has invalid format")
518-
.span_label(attr.span, "did you mean #[export_name=\"*\"]?")
519-
.emit();
520-
None
521-
}
522-
} else {
523-
ia
524-
}
525-
})
526-
}
527-
528-
pub fn contains_extern_indicator(diag: &Handler, attrs: &[Attribute]) -> bool {
529-
contains_name(attrs, "no_mangle") ||
530-
find_export_name_attr(diag, attrs).is_some()
531-
}
532-
533509
#[derive(Copy, Clone, PartialEq)]
534510
pub enum InlineAttr {
535511
None,

src/libsyntax/diagnostic_list.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -219,29 +219,6 @@ Erroneous code example:
219219
Delete the offending feature attribute.
220220
"##,
221221

222-
E0558: r##"
223-
The `export_name` attribute was malformed.
224-
225-
Erroneous code example:
226-
227-
```compile_fail,E0558
228-
#[export_name] // error: export_name attribute has invalid format
229-
pub fn something() {}
230-
231-
fn main() {}
232-
```
233-
234-
The `export_name` attribute expects a string in order to determine the name of
235-
the exported symbol. Example:
236-
237-
```
238-
#[export_name = "some_function"] // ok!
239-
pub fn something() {}
240-
241-
fn main() {}
242-
```
243-
"##,
244-
245222
E0565: r##"
246223
A literal was used in an attribute that doesn't support literals.
247224

0 commit comments

Comments
 (0)