Skip to content

Commit d91b32b

Browse files
committed
Auto merge of #59256 - petrochenkov:derval2, r=Zoxc
Make meta-item API compatible with `LocalInternedString::get` soundness fix r? @Zoxc
2 parents 3f36ac4 + db74efc commit d91b32b

File tree

17 files changed

+84
-97
lines changed

17 files changed

+84
-97
lines changed

src/librustc/hir/check_attr.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,8 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
177177
let mut is_transparent = false;
178178

179179
for hint in &hints {
180-
let name = if let Some(name) = hint.ident_str() {
181-
name
182-
} else {
183-
// Invalid repr hint like repr(42). We don't check for unrecognized hints here
184-
// (libsyntax does that), so just ignore it.
185-
continue;
186-
};
187-
188-
let (article, allowed_targets) = match name {
189-
"C" | "align" => {
180+
let (article, allowed_targets) = match hint.name_or_empty().get() {
181+
name @ "C" | name @ "align" => {
190182
is_c |= name == "C";
191183
if target != Target::Struct &&
192184
target != Target::Union &&

src/librustc/lint/levels.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl<'a> LintLevelsBuilder<'a> {
194194
struct_span_err!(sess, span, E0452, "malformed lint attribute")
195195
};
196196
for attr in attrs {
197-
let level = match attr.ident_str().and_then(|name| Level::from_str(name)) {
197+
let level = match Level::from_str(&attr.name_or_empty()) {
198198
None => continue,
199199
Some(lvl) => lvl,
200200
};

src/librustc/lint/mod.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -723,12 +723,7 @@ pub fn struct_lint_level<'a>(sess: &'a Session,
723723

724724
pub fn maybe_lint_level_root(tcx: TyCtxt<'_, '_, '_>, id: hir::HirId) -> bool {
725725
let attrs = tcx.hir().attrs_by_hir_id(id);
726-
for attr in attrs {
727-
if attr.ident_str().and_then(Level::from_str).is_some() {
728-
return true;
729-
}
730-
}
731-
false
726+
attrs.iter().any(|attr| Level::from_str(&attr.name_or_empty()).is_some())
732727
}
733728

734729
fn lint_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, cnum: CrateNum)

src/librustc/middle/lib_features.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ impl<'a, 'tcx> LibFeatureCollector<'a, 'tcx> {
6565
for meta in metas {
6666
if let Some(mi) = meta.meta_item() {
6767
// Find the `feature = ".."` meta-item.
68-
match (mi.ident_str(), mi.value_str()) {
69-
(Some("feature"), val) => feature = val,
70-
(Some("since"), val) => since = val,
68+
match (mi.name_or_empty().get(), mi.value_str()) {
69+
("feature", val) => feature = val,
70+
("since", val) => since = val,
7171
_ => {}
7272
}
7373
}

src/librustc/middle/stability.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,11 @@ impl<'a, 'tcx: 'a> Annotator<'a, 'tcx> {
194194
} else {
195195
// Emit errors for non-staged-api crates.
196196
for attr in attrs {
197-
if let Some(tag) = attr.ident_str() {
198-
if tag == "unstable" || tag == "stable" || tag == "rustc_deprecated" {
199-
attr::mark_used(attr);
200-
self.tcx.sess.span_err(attr.span, "stability attributes may not be used \
201-
outside of the standard library");
202-
}
197+
let name = attr.name_or_empty();
198+
if ["unstable", "stable", "rustc_deprecated"].contains(&name.get()) {
199+
attr::mark_used(attr);
200+
self.tcx.sess.span_err(attr.span, "stability attributes may not be used \
201+
outside of the standard library");
203202
}
204203
}
205204

src/librustc/traits/on_unimplemented.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedDirective {
177177
for command in self.subcommands.iter().chain(Some(self)).rev() {
178178
if let Some(ref condition) = command.condition {
179179
if !attr::eval_condition(condition, &tcx.sess.parse_sess, &mut |c| {
180-
c.ident_str().map_or(false, |name| {
180+
c.ident().map_or(false, |ident| {
181181
options.contains(&(
182-
name.to_string(),
182+
ident.to_string(),
183183
c.value_str().map(|s| s.as_str().to_string())
184184
))
185185
})

src/librustc_incremental/persist/dirty_clean.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,8 @@ fn expect_associated_value(tcx: TyCtxt<'_, '_, '_>, item: &NestedMetaItem) -> as
576576
if let Some(value) = item.value_str() {
577577
value
578578
} else {
579-
let msg = if let Some(name) = item.ident_str() {
580-
format!("associated value expected for `{}`", name)
579+
let msg = if let Some(ident) = item.ident() {
580+
format!("associated value expected for `{}`", ident)
581581
} else {
582582
"expected an associated value".to_string()
583583
};

src/librustc_lint/builtin.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -756,8 +756,9 @@ impl LintPass for DeprecatedAttr {
756756

757757
impl EarlyLintPass for DeprecatedAttr {
758758
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
759+
let name = attr.name_or_empty();
759760
for &&(n, _, _, ref g) in &self.depr_attrs {
760-
if attr.ident_str() == Some(n) {
761+
if name == n {
761762
if let &AttributeGate::Gated(Stability::Deprecated(link, suggestion),
762763
ref name,
763764
ref reason,

src/librustc_lint/unused.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -267,21 +267,21 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAttributes {
267267
}
268268
}
269269

270-
let name = attr.ident_str();
270+
let name = attr.name_or_empty();
271271
if !attr::is_used(attr) {
272272
debug!("Emitting warning for: {:?}", attr);
273273
cx.span_lint(UNUSED_ATTRIBUTES, attr.span, "unused attribute");
274274
// Is it a builtin attribute that must be used at the crate level?
275275
let known_crate = BUILTIN_ATTRIBUTES.iter()
276276
.find(|&&(builtin, ty, ..)| {
277-
name == Some(builtin) && ty == AttributeType::CrateLevel
277+
name == builtin && ty == AttributeType::CrateLevel
278278
})
279279
.is_some();
280280

281281
// Has a plugin registered this attribute as one that must be used at
282282
// the crate level?
283283
let plugin_crate = plugin_attributes.iter()
284-
.find(|&&(ref x, t)| name == Some(x) && AttributeType::CrateLevel == t)
284+
.find(|&&(ref x, t)| name == x.as_str() && AttributeType::CrateLevel == t)
285285
.is_some();
286286
if known_crate || plugin_crate {
287287
let msg = match attr.style {

src/librustc_passes/layout_test.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ impl<'a, 'tcx> VarianceTest<'a, 'tcx> {
5353
// The `..` are the names of fields to dump.
5454
let meta_items = attr.meta_item_list().unwrap_or_default();
5555
for meta_item in meta_items {
56-
let name = meta_item.ident_str().unwrap_or("");
57-
match name {
56+
match meta_item.name_or_empty().get() {
5857
"abi" => {
5958
self.tcx
6059
.sess
@@ -84,7 +83,7 @@ impl<'a, 'tcx> VarianceTest<'a, 'tcx> {
8483
);
8584
}
8685

87-
_ => {
86+
name => {
8887
self.tcx.sess.span_err(
8988
meta_item.span(),
9089
&format!("unrecognized field name `{}`", name),

src/librustc_plugin/load.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ pub fn load_plugins(sess: &Session,
5656

5757
for plugin in plugins {
5858
// plugins must have a name and can't be key = value
59-
match plugin.ident_str() {
60-
Some(name) if !plugin.is_value_str() => {
61-
let args = plugin.meta_item_list().map(ToOwned::to_owned);
62-
loader.load_plugin(plugin.span(), name, args.unwrap_or_default());
63-
},
64-
_ => call_malformed_plugin_attribute(sess, attr.span),
59+
let name = plugin.name_or_empty();
60+
if !name.is_empty() && !plugin.is_value_str() {
61+
let args = plugin.meta_item_list().map(ToOwned::to_owned);
62+
loader.load_plugin(plugin.span(), &name, args.unwrap_or_default());
63+
} else {
64+
call_malformed_plugin_attribute(sess, attr.span);
6565
}
6666
}
6767
}

src/librustdoc/core.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -529,21 +529,21 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
529529
for attr in krate.module.as_ref().unwrap().attrs.lists("doc") {
530530
let diag = ctxt.sess().diagnostic();
531531

532-
let name = attr.ident_str();
532+
let name = attr.name_or_empty();
533533
if attr.is_word() {
534-
if name == Some("no_default_passes") {
534+
if name == "no_default_passes" {
535535
report_deprecated_attr("no_default_passes", diag);
536536
if default_passes == passes::DefaultPassOption::Default {
537537
default_passes = passes::DefaultPassOption::None;
538538
}
539539
}
540540
} else if let Some(value) = attr.value_str() {
541-
let sink = match name {
542-
Some("passes") => {
541+
let sink = match name.get() {
542+
"passes" => {
543543
report_deprecated_attr("passes = \"...\"", diag);
544544
&mut manual_passes
545545
},
546-
Some("plugins") => {
546+
"plugins" => {
547547
report_deprecated_attr("plugins = \"...\"", diag);
548548
eprintln!("WARNING: #![doc(plugins = \"...\")] no longer functions; \
549549
see CVE-2018-1000622");
@@ -556,7 +556,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
556556
}
557557
}
558558

559-
if attr.is_word() && name == Some("document_private_items") {
559+
if attr.is_word() && name == "document_private_items" {
560560
if default_passes == passes::DefaultPassOption::Default {
561561
default_passes = passes::DefaultPassOption::Private;
562562
}

src/librustdoc/html/render.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -562,23 +562,23 @@ pub fn run(mut krate: clean::Crate,
562562
// going to emit HTML
563563
if let Some(attrs) = krate.module.as_ref().map(|m| &m.attrs) {
564564
for attr in attrs.lists("doc") {
565-
match (attr.ident_str(), attr.value_str()) {
566-
(Some("html_favicon_url"), Some(s)) => {
565+
match (attr.name_or_empty().get(), attr.value_str()) {
566+
("html_favicon_url", Some(s)) => {
567567
scx.layout.favicon = s.to_string();
568568
}
569-
(Some("html_logo_url"), Some(s)) => {
569+
("html_logo_url", Some(s)) => {
570570
scx.layout.logo = s.to_string();
571571
}
572-
(Some("html_playground_url"), Some(s)) => {
572+
("html_playground_url", Some(s)) => {
573573
markdown::PLAYGROUND.with(|slot| {
574574
let name = krate.name.clone();
575575
*slot.borrow_mut() = Some((Some(name), s.to_string()));
576576
});
577577
}
578-
(Some("issue_tracker_base_url"), Some(s)) => {
578+
("issue_tracker_base_url", Some(s)) => {
579579
scx.issue_tracker_base_url = Some(s.to_string());
580580
}
581-
(Some("html_no_source"), None) if attr.is_word() => {
581+
("html_no_source", None) if attr.is_word() => {
582582
scx.include_sources = false;
583583
}
584584
_ => {}
@@ -3751,7 +3751,7 @@ fn render_attributes(w: &mut fmt::Formatter<'_>, it: &clean::Item) -> fmt::Resul
37513751
let mut attrs = String::new();
37523752

37533753
for attr in &it.attrs.other_attrs {
3754-
if !attr.ident_str().map_or(false, |name| ATTRIBUTE_WHITELIST.contains(&name)) {
3754+
if !ATTRIBUTE_WHITELIST.contains(&attr.name_or_empty().get()) {
37553755
continue;
37563756
}
37573757
if let Some(s) = render_attribute(&attr.meta().unwrap()) {

src/libsyntax/attr/builtin.rs

+23-23
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,9 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
222222
)+
223223
for meta in metas {
224224
if let Some(mi) = meta.meta_item() {
225-
match mi.ident_str() {
225+
match mi.name_or_empty().get() {
226226
$(
227-
Some(stringify!($name))
227+
stringify!($name)
228228
=> if !get(mi, &mut $name) { continue 'outer },
229229
)+
230230
_ => {
@@ -252,7 +252,7 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
252252
}
253253
}
254254

255-
match meta.ident_str().expect("not a stability level") {
255+
match meta.name_or_empty().get() {
256256
"rustc_deprecated" => {
257257
if rustc_depr.is_some() {
258258
span_err!(diagnostic, item_sp, E0540,
@@ -306,10 +306,10 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
306306
let mut issue = None;
307307
for meta in metas {
308308
if let Some(mi) = meta.meta_item() {
309-
match mi.ident_str() {
310-
Some("feature") => if !get(mi, &mut feature) { continue 'outer },
311-
Some("reason") => if !get(mi, &mut reason) { continue 'outer },
312-
Some("issue") => if !get(mi, &mut issue) { continue 'outer },
309+
match mi.name_or_empty().get() {
310+
"feature" => if !get(mi, &mut feature) { continue 'outer },
311+
"reason" => if !get(mi, &mut reason) { continue 'outer },
312+
"issue" => if !get(mi, &mut issue) { continue 'outer },
313313
_ => {
314314
handle_errors(
315315
sess,
@@ -377,10 +377,10 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
377377
for meta in metas {
378378
match meta {
379379
NestedMetaItem::MetaItem(mi) => {
380-
match mi.ident_str() {
381-
Some("feature") =>
380+
match mi.name_or_empty().get() {
381+
"feature" =>
382382
if !get(mi, &mut feature) { continue 'outer },
383-
Some("since") =>
383+
"since" =>
384384
if !get(mi, &mut since) { continue 'outer },
385385
_ => {
386386
handle_errors(
@@ -532,14 +532,14 @@ pub fn eval_condition<F>(cfg: &ast::MetaItem, sess: &ParseSess, eval: &mut F)
532532

533533
// The unwraps below may look dangerous, but we've already asserted
534534
// that they won't fail with the loop above.
535-
match cfg.ident_str() {
536-
Some("any") => mis.iter().any(|mi| {
535+
match cfg.name_or_empty().get() {
536+
"any" => mis.iter().any(|mi| {
537537
eval_condition(mi.meta_item().unwrap(), sess, eval)
538538
}),
539-
Some("all") => mis.iter().all(|mi| {
539+
"all" => mis.iter().all(|mi| {
540540
eval_condition(mi.meta_item().unwrap(), sess, eval)
541541
}),
542-
Some("not") => {
542+
"not" => {
543543
if mis.len() != 1 {
544544
span_err!(sess.span_diagnostic, cfg.span, E0536, "expected 1 cfg-pattern");
545545
return false;
@@ -635,9 +635,9 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess,
635635
for meta in list {
636636
match meta {
637637
NestedMetaItem::MetaItem(mi) => {
638-
match mi.ident_str() {
639-
Some("since") => if !get(mi, &mut since) { continue 'outer },
640-
Some("note") => if !get(mi, &mut note) { continue 'outer },
638+
match mi.name_or_empty().get() {
639+
"since" => if !get(mi, &mut since) { continue 'outer },
640+
"note" => if !get(mi, &mut note) { continue 'outer },
641641
_ => {
642642
handle_errors(
643643
sess,
@@ -729,12 +729,12 @@ pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> {
729729

730730
let mut recognised = false;
731731
if item.is_word() {
732-
let hint = match item.ident_str() {
733-
Some("C") => Some(ReprC),
734-
Some("packed") => Some(ReprPacked(1)),
735-
Some("simd") => Some(ReprSimd),
736-
Some("transparent") => Some(ReprTransparent),
737-
name => name.and_then(|name| int_type_of_word(name)).map(ReprInt),
732+
let hint = match item.name_or_empty().get() {
733+
"C" => Some(ReprC),
734+
"packed" => Some(ReprPacked(1)),
735+
"simd" => Some(ReprSimd),
736+
"transparent" => Some(ReprTransparent),
737+
name => int_type_of_word(name).map(ReprInt),
738738
};
739739

740740
if let Some(h) = hint {

src/libsyntax/attr/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::parse::parser::Parser;
2222
use crate::parse::{self, ParseSess, PResult};
2323
use crate::parse::token::{self, Token};
2424
use crate::ptr::P;
25-
use crate::symbol::Symbol;
25+
use crate::symbol::{keywords, LocalInternedString, Symbol};
2626
use crate::ThinVec;
2727
use crate::tokenstream::{TokenStream, TokenTree, DelimSpan};
2828
use crate::GLOBALS;
@@ -89,8 +89,8 @@ impl NestedMetaItem {
8989
pub fn ident(&self) -> Option<Ident> {
9090
self.meta_item().and_then(|meta_item| meta_item.ident())
9191
}
92-
pub fn ident_str(&self) -> Option<&str> {
93-
self.ident().map(|name| name.as_str().get())
92+
pub fn name_or_empty(&self) -> LocalInternedString {
93+
self.ident().unwrap_or(keywords::Invalid.ident()).name.as_str()
9494
}
9595

9696
/// Gets the string value if self is a MetaItem and the MetaItem is a
@@ -167,8 +167,8 @@ impl Attribute {
167167
None
168168
}
169169
}
170-
pub fn ident_str(&self) -> Option<&str> {
171-
self.ident().map(|name| name.as_str().get())
170+
pub fn name_or_empty(&self) -> LocalInternedString {
171+
self.ident().unwrap_or(keywords::Invalid.ident()).name.as_str()
172172
}
173173

174174
pub fn value_str(&self) -> Option<Symbol> {
@@ -205,8 +205,8 @@ impl MetaItem {
205205
None
206206
}
207207
}
208-
pub fn ident_str(&self) -> Option<&str> {
209-
self.ident().map(|name| name.as_str().get())
208+
pub fn name_or_empty(&self) -> LocalInternedString {
209+
self.ident().unwrap_or(keywords::Invalid.ident()).name.as_str()
210210
}
211211

212212
// #[attribute(name = "value")]

0 commit comments

Comments
 (0)