Skip to content

Commit 7d30472

Browse files
committed
Remove mk_name_value_item{,_str}.
There are better ways to create the meta items. - In the rustdoc tests, the commit adds `dummy_meta_item_name_value`, which matches the existing `dummy_meta_item_word` function and `dummy_meta_item_list` macro. - In `types.rs` the commit clones the existing meta item and then modifies the clone.
1 parent d5526ff commit 7d30472

File tree

3 files changed

+20
-29
lines changed

3 files changed

+20
-29
lines changed

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Functions dealing with attributes and meta items.
22
3-
use crate::ast;
43
use crate::ast::{AttrArgs, AttrArgsEq, AttrId, AttrItem, AttrKind, AttrStyle, AttrVec, Attribute};
54
use crate::ast::{DelimArgs, Expr, ExprKind, LitKind, MetaItemLit};
65
use crate::ast::{MacDelimiter, MetaItem, MetaItemKind, NestedMetaItem, NormalAttr};
@@ -321,20 +320,6 @@ impl Attribute {
321320
}
322321
}
323322

324-
/* Constructors */
325-
326-
pub fn mk_name_value_item_str(ident: Ident, str: Symbol, str_span: Span) -> MetaItem {
327-
mk_name_value_item(ident, LitKind::Str(str, ast::StrStyle::Cooked), str_span)
328-
}
329-
330-
pub fn mk_name_value_item(ident: Ident, kind: LitKind, lit_span: Span) -> MetaItem {
331-
let token_lit = kind.synthesize_token_lit();
332-
let lit =
333-
MetaItemLit { symbol: token_lit.symbol, suffix: token_lit.suffix, kind, span: lit_span };
334-
let span = ident.span.to(lit_span);
335-
MetaItem { path: Path::from_ident(ident), kind: MetaItemKind::NameValue(lit), span }
336-
}
337-
338323
pub struct AttrIdGenerator(WorkerLocal<Cell<u32>>);
339324

340325
#[cfg(debug_assertions)]

src/librustdoc/clean/cfg/tests.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use super::*;
22

3-
use rustc_ast::attr;
4-
use rustc_ast::Path;
3+
use rustc_ast::{LitKind, MetaItemLit, Path, StrStyle};
54
use rustc_span::create_default_session_globals_then;
6-
use rustc_span::symbol::{Ident, Symbol};
5+
use rustc_span::symbol::{kw, Ident, Symbol};
76
use rustc_span::DUMMY_SP;
87

98
fn word_cfg(s: &str) -> Cfg {
@@ -22,6 +21,15 @@ fn dummy_meta_item_word(name: &str) -> MetaItem {
2221
}
2322
}
2423

24+
fn dummy_meta_item_name_value(name: &str, symbol: Symbol, kind: LitKind) -> MetaItem {
25+
let lit = MetaItemLit { symbol, suffix: None, kind, span: DUMMY_SP };
26+
MetaItem {
27+
path: Path::from_ident(Ident::from_str(name)),
28+
kind: MetaItemKind::NameValue(lit),
29+
span: DUMMY_SP,
30+
}
31+
}
32+
2533
macro_rules! dummy_meta_item_list {
2634
($name:ident, [$($list:ident),* $(,)?]) => {
2735
MetaItem {
@@ -242,8 +250,8 @@ fn test_parse_ok() {
242250
let mi = dummy_meta_item_word("all");
243251
assert_eq!(Cfg::parse(&mi), Ok(word_cfg("all")));
244252

245-
let mi =
246-
attr::mk_name_value_item_str(Ident::from_str("all"), Symbol::intern("done"), DUMMY_SP);
253+
let done = Symbol::intern("done");
254+
let mi = dummy_meta_item_name_value("all", done, LitKind::Str(done, StrStyle::Cooked));
247255
assert_eq!(Cfg::parse(&mi), Ok(name_value_cfg("all", "done")));
248256

249257
let mi = dummy_meta_item_list!(all, [a, b]);
@@ -272,7 +280,7 @@ fn test_parse_ok() {
272280
#[test]
273281
fn test_parse_err() {
274282
create_default_session_globals_then(|| {
275-
let mi = attr::mk_name_value_item(Ident::from_str("foo"), LitKind::Bool(false), DUMMY_SP);
283+
let mi = dummy_meta_item_name_value("foo", kw::False, LitKind::Bool(false));
276284
assert!(Cfg::parse(&mi).is_err());
277285

278286
let mi = dummy_meta_item_list!(not, [a, b]);

src/librustdoc/clean/types.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use std::{cmp, fmt, iter};
1010
use arrayvec::ArrayVec;
1111
use thin_vec::ThinVec;
1212

13-
use rustc_ast::attr;
1413
use rustc_ast::util::comments::beautify_doc_string;
1514
use rustc_ast::{self as ast, AttrStyle};
1615
use rustc_attr::{ConstStability, Deprecation, Stability, StabilityLevel};
@@ -27,7 +26,6 @@ use rustc_middle::ty::fast_reject::SimplifiedType;
2726
use rustc_middle::ty::{self, DefIdTree, TyCtxt, Visibility};
2827
use rustc_session::Session;
2928
use rustc_span::hygiene::MacroKind;
30-
use rustc_span::source_map::DUMMY_SP;
3129
use rustc_span::symbol::{kw, sym, Ident, Symbol};
3230
use rustc_span::{self, FileName, Loc};
3331
use rustc_target::abi::VariantIdx;
@@ -979,12 +977,12 @@ impl AttributesExt for [ast::Attribute] {
979977
// #[doc(cfg(target_feature = "feat"))] attributes as well
980978
for attr in self.lists(sym::target_feature) {
981979
if attr.has_name(sym::enable) {
982-
if let Some(feat) = attr.value_str() {
983-
let meta = attr::mk_name_value_item_str(
984-
Ident::with_dummy_span(sym::target_feature),
985-
feat,
986-
DUMMY_SP,
987-
);
980+
if attr.value_str().is_some() {
981+
// Clone `enable = "feat"`, change to `target_feature = "feat"`.
982+
// Unwrap is safe because `value_str` succeeded above.
983+
let mut meta = attr.meta_item().unwrap().clone();
984+
meta.path = ast::Path::from_ident(Ident::with_dummy_span(sym::target_feature));
985+
988986
if let Ok(feat_cfg) = Cfg::parse(&meta) {
989987
cfg &= feat_cfg;
990988
}

0 commit comments

Comments
 (0)