Skip to content

Commit d8ea2a2

Browse files
committed
change smir attributes getters to only support tool attributes
1 parent 95b52d5 commit d8ea2a2

File tree

4 files changed

+45
-68
lines changed

4 files changed

+45
-68
lines changed

Diff for: compiler/rustc_smir/src/rustc_smir/context.rs

+25-14
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::cell::RefCell;
99
use std::iter;
1010

1111
use rustc_abi::HasDataLayout;
12-
use rustc_hir::LangItem;
12+
use rustc_hir::{Attribute, LangItem};
1313
use rustc_middle::ty::layout::{
1414
FnAbiOf, FnAbiOfHelpers, HasTyCtxt, HasTypingEnv, LayoutOf, LayoutOfHelpers,
1515
};
@@ -243,7 +243,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
243243
}
244244
}
245245

246-
fn get_attrs_by_path(
246+
fn tool_attrs(
247247
&self,
248248
def_id: stable_mir::DefId,
249249
attr: &[stable_mir::Symbol],
@@ -253,29 +253,40 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
253253
let did = tables[def_id];
254254
let attr_name: Vec<_> = attr.iter().map(|seg| rustc_span::Symbol::intern(&seg)).collect();
255255
tcx.get_attrs_by_path(did, &attr_name)
256-
.map(|attribute| {
257-
let attr_str = rustc_hir_pretty::attribute_to_string(&tcx, attribute);
258-
let span = attribute.span();
259-
stable_mir::crate_def::Attribute::new(attr_str, span.stable(&mut *tables))
256+
.filter_map(|attribute| {
257+
if let Attribute::Unparsed(u) = attribute {
258+
let attr_str = rustc_hir_pretty::attribute_to_string(&tcx, attribute);
259+
Some(stable_mir::crate_def::Attribute::new(
260+
attr_str,
261+
u.span.stable(&mut *tables),
262+
))
263+
} else {
264+
None
265+
}
260266
})
261267
.collect()
262268
}
263269

264-
fn get_all_attrs(&self, def_id: stable_mir::DefId) -> Vec<stable_mir::crate_def::Attribute> {
270+
fn all_tool_attrs(&self, def_id: stable_mir::DefId) -> Vec<stable_mir::crate_def::Attribute> {
265271
let mut tables = self.0.borrow_mut();
266272
let tcx = tables.tcx;
267273
let did = tables[def_id];
268-
let filter_fn = move |a: &&rustc_hir::Attribute| !a.is_doc_comment();
269274
let attrs_iter = if let Some(did) = did.as_local() {
270-
tcx.hir().attrs(tcx.local_def_id_to_hir_id(did)).iter().filter(filter_fn)
275+
tcx.hir().attrs(tcx.local_def_id_to_hir_id(did)).iter()
271276
} else {
272-
tcx.attrs_for_def(did).iter().filter(filter_fn)
277+
tcx.attrs_for_def(did).iter()
273278
};
274279
attrs_iter
275-
.map(|attribute| {
276-
let attr_str = rustc_hir_pretty::attribute_to_string(&tcx, attribute);
277-
let span = attribute.span();
278-
stable_mir::crate_def::Attribute::new(attr_str, span.stable(&mut *tables))
280+
.filter_map(|attribute| {
281+
if let Attribute::Unparsed(u) = attribute {
282+
let attr_str = rustc_hir_pretty::attribute_to_string(&tcx, attribute);
283+
Some(stable_mir::crate_def::Attribute::new(
284+
attr_str,
285+
u.span.stable(&mut *tables),
286+
))
287+
} else {
288+
None
289+
}
279290
})
280291
.collect()
281292
}

Diff for: compiler/stable_mir/src/compiler_interface.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,17 @@ pub trait Context {
6262
/// Returns the name of given `DefId`
6363
fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol;
6464

65-
/// Return attributes with the given attribute name.
65+
/// Return registered tool attributes with the given attribute name.
6666
///
67-
/// Single segmented name like `#[inline]` is specified as `&["inline".to_string()]`.
67+
/// FIXME(jdonszelmann): may panic on non-tool attributes. After more attribute work, non-tool
68+
/// attributes will simply return an empty list.
69+
///
70+
/// Single segmented name like `#[clippy]` is specified as `&["clippy".to_string()]`.
6871
/// Multi-segmented name like `#[rustfmt::skip]` is specified as `&["rustfmt".to_string(), "skip".to_string()]`.
69-
fn get_attrs_by_path(&self, def_id: DefId, attr: &[Symbol]) -> Vec<Attribute>;
72+
fn tool_attrs(&self, def_id: DefId, attr: &[Symbol]) -> Vec<Attribute>;
7073

71-
/// Get all attributes of a definition.
72-
fn get_all_attrs(&self, def_id: DefId) -> Vec<Attribute>;
74+
/// Get all tool attributes of a definition.
75+
fn all_tool_attrs(&self, def_id: DefId) -> Vec<Attribute>;
7376

7477
/// Returns printable, human readable form of `Span`
7578
fn span_to_string(&self, span: Span) -> String;

Diff for: compiler/stable_mir/src/crate_def.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,22 @@ pub trait CrateDef {
5353
with(|cx| cx.span_of_an_item(def_id))
5454
}
5555

56-
/// Return attributes with the given attribute name.
56+
/// Return registered tool attributes with the given attribute name.
5757
///
58-
/// Single segmented name like `#[inline]` is specified as `&["inline".to_string()]`.
58+
/// FIXME(jdonszelmann): may panic on non-tool attributes. After more attribute work, non-tool
59+
/// attributes will simply return an empty list.
60+
///
61+
/// Single segmented name like `#[clippy]` is specified as `&["clippy".to_string()]`.
5962
/// Multi-segmented name like `#[rustfmt::skip]` is specified as `&["rustfmt".to_string(), "skip".to_string()]`.
60-
fn attrs_by_path(&self, attr: &[Symbol]) -> Vec<Attribute> {
63+
fn tool_attrs(&self, attr: &[Symbol]) -> Vec<Attribute> {
6164
let def_id = self.def_id();
62-
with(|cx| cx.get_attrs_by_path(def_id, attr))
65+
with(|cx| cx.tool_attrs(def_id, attr))
6366
}
6467

65-
/// Return all attributes of this definition.
66-
fn all_attrs(&self) -> Vec<Attribute> {
68+
/// Return all tool attributes of this definition.
69+
fn all_tool_attrs(&self) -> Vec<Attribute> {
6770
let def_id = self.def_id();
68-
with(|cx| cx.get_all_attrs(def_id))
71+
with(|cx| cx.all_tool_attrs(def_id))
6972
}
7073
}
7174

Diff for: tests/ui-fulldeps/stable-mir/check_attribute.rs

+2-42
Original file line numberDiff line numberDiff line change
@@ -27,63 +27,23 @@ fn test_stable_mir() -> ControlFlow<()> {
2727
// Find items in the local crate.
2828
let items = stable_mir::all_local_items();
2929

30-
test_builtins(&items);
31-
test_derive(&items);
3230
test_tool(&items);
33-
test_all_attrs(&items);
3431

3532
ControlFlow::Continue(())
3633
}
3734

38-
// Test built-in attributes.
39-
fn test_builtins(items: &CrateItems) {
40-
let target_fn = *get_item(&items, "builtins_fn").unwrap();
41-
let allow_attrs = target_fn.attrs_by_path(&["allow".to_string()]);
42-
assert_eq!(allow_attrs[0].as_str(), "#[allow(unused_variables)]");
43-
44-
let inline_attrs = target_fn.attrs_by_path(&["inline".to_string()]);
45-
assert_eq!(inline_attrs[0].as_str(), "#[inline]");
46-
47-
let deprecated_attrs = target_fn.attrs_by_path(&["deprecated".to_string()]);
48-
assert_eq!(deprecated_attrs[0].as_str(), "#[deprecated(since = \"5.2.0\")]");
49-
}
50-
51-
// Test derive attribute.
52-
fn test_derive(items: &CrateItems) {
53-
let target_struct = *get_item(&items, "Foo").unwrap();
54-
let attrs = target_struct.attrs_by_path(&["derive".to_string()]);
55-
// No `derive` attribute since it's expanded before MIR.
56-
assert_eq!(attrs.len(), 0);
57-
58-
// Check derived trait method's attributes.
59-
let derived_fmt = *get_item(&items, "<Foo as std::fmt::Debug>::fmt").unwrap();
60-
// The Rust reference lies about this attribute. It doesn't show up in `clone` or `fmt` impl.
61-
let _fmt_attrs = derived_fmt.attrs_by_path(&["automatically_derived".to_string()]);
62-
}
63-
6435
// Test tool attributes.
6536
fn test_tool(items: &CrateItems) {
6637
let rustfmt_fn = *get_item(&items, "do_not_format").unwrap();
67-
let rustfmt_attrs = rustfmt_fn.attrs_by_path(&["rustfmt".to_string(), "skip".to_string()]);
38+
let rustfmt_attrs = rustfmt_fn.tool_attrs(&["rustfmt".to_string(), "skip".to_string()]);
6839
assert_eq!(rustfmt_attrs[0].as_str(), "#[rustfmt::skip]");
6940

7041
let clippy_fn = *get_item(&items, "complex_fn").unwrap();
71-
let clippy_attrs = clippy_fn.attrs_by_path(&["clippy".to_string(),
42+
let clippy_attrs = clippy_fn.tool_attrs(&["clippy".to_string(),
7243
"cyclomatic_complexity".to_string()]);
7344
assert_eq!(clippy_attrs[0].as_str(), "#[clippy::cyclomatic_complexity = \"100\"]");
7445
}
7546

76-
fn test_all_attrs(items: &CrateItems) {
77-
let target_fn = *get_item(&items, "many_attrs").unwrap();
78-
let all_attrs = target_fn.all_attrs();
79-
assert_eq!(all_attrs[0].as_str(), "#[inline]");
80-
assert_eq!(all_attrs[1].as_str(), "#[allow(unused_variables)]");
81-
assert_eq!(all_attrs[2].as_str(), "#[allow(dead_code)]");
82-
assert_eq!(all_attrs[3].as_str(), "#[allow(unused_imports)]");
83-
assert_eq!(all_attrs[4].as_str(), "#![allow(clippy::filter_map)]");
84-
}
85-
86-
8747
fn get_item<'a>(
8848
items: &'a CrateItems,
8949
name: &str,

0 commit comments

Comments
 (0)