Skip to content

Commit c18b64c

Browse files
authored
Rollup merge of #75378 - petrochenkov:isident, r=Mark-Simulacrum
Introduce `rustc_lexer::is_ident` and use it in couple of places Implements the suggestion from #74537 (comment).
2 parents dcccb47 + 6bbf455 commit c18b64c

File tree

8 files changed

+42
-26
lines changed

8 files changed

+42
-26
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3257,6 +3257,7 @@ dependencies = [
32573257
"rustc_data_structures",
32583258
"rustc_errors",
32593259
"rustc_feature",
3260+
"rustc_lexer",
32603261
"rustc_macros",
32613262
"rustc_serialize",
32623263
"rustc_session",

src/librustc_attr/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ rustc_errors = { path = "../librustc_errors" }
1616
rustc_span = { path = "../librustc_span" }
1717
rustc_data_structures = { path = "../librustc_data_structures" }
1818
rustc_feature = { path = "../librustc_feature" }
19+
rustc_lexer = { path = "../librustc_lexer" }
1920
rustc_macros = { path = "../librustc_macros" }
2021
rustc_session = { path = "../librustc_session" }
2122
rustc_ast = { path = "../librustc_ast" }

src/librustc_attr/builtin.rs

+12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ enum AttrError {
2020
MultipleItem(String),
2121
UnknownMetaItem(String, &'static [&'static str]),
2222
MissingSince,
23+
NonIdentFeature,
2324
MissingFeature,
2425
MultipleStabilityLevels,
2526
UnsupportedLiteral(&'static str, /* is_bytestr */ bool),
@@ -40,6 +41,9 @@ fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
4041
AttrError::MissingSince => {
4142
struct_span_err!(diag, span, E0542, "missing 'since'").emit();
4243
}
44+
AttrError::NonIdentFeature => {
45+
struct_span_err!(diag, span, E0546, "'feature' is not an identifier").emit();
46+
}
4347
AttrError::MissingFeature => {
4448
struct_span_err!(diag, span, E0546, "missing 'feature'").emit();
4549
}
@@ -344,6 +348,14 @@ where
344348

345349
match (feature, reason, issue) {
346350
(Some(feature), reason, Some(_)) => {
351+
if !rustc_lexer::is_ident(&feature.as_str()) {
352+
handle_errors(
353+
&sess.parse_sess,
354+
attr.span,
355+
AttrError::NonIdentFeature,
356+
);
357+
continue;
358+
}
347359
let level = Unstable { reason, issue: issue_num, is_soft };
348360
if sym::unstable == meta_name {
349361
stab = Some(Stability { level, feature });

src/librustc_expand/proc_macro_server.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -319,18 +319,10 @@ pub struct Ident {
319319
}
320320

321321
impl Ident {
322-
fn is_valid(string: &str) -> bool {
323-
let mut chars = string.chars();
324-
if let Some(start) = chars.next() {
325-
rustc_lexer::is_id_start(start) && chars.all(rustc_lexer::is_id_continue)
326-
} else {
327-
false
328-
}
329-
}
330322
fn new(sess: &ParseSess, sym: Symbol, is_raw: bool, span: Span) -> Ident {
331323
let sym = nfc_normalize(&sym.as_str());
332324
let string = sym.as_str();
333-
if !Self::is_valid(&string) {
325+
if !rustc_lexer::is_ident(&string) {
334326
panic!("`{:?}` is not a valid identifier", string)
335327
}
336328
if is_raw && !sym.can_be_raw() {

src/librustc_lexer/src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,16 @@ pub fn is_id_continue(c: char) -> bool {
274274
|| (c > '\x7f' && unicode_xid::UnicodeXID::is_xid_continue(c))
275275
}
276276

277+
/// The passed string is lexically an identifier.
278+
pub fn is_ident(string: &str) -> bool {
279+
let mut chars = string.chars();
280+
if let Some(start) = chars.next() {
281+
is_id_start(start) && chars.all(is_id_continue)
282+
} else {
283+
false
284+
}
285+
}
286+
277287
impl Cursor<'_> {
278288
/// Parses a token from the input string.
279289
fn advance_token(&mut self) -> Token {

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2358,7 +2358,7 @@ impl Clean<Stability> for attr::Stability {
23582358
fn clean(&self, _: &DocContext<'_>) -> Stability {
23592359
Stability {
23602360
level: stability::StabilityLevel::from_attr_level(&self.level),
2361-
feature: Some(self.feature.to_string()).filter(|f| !f.is_empty()),
2361+
feature: self.feature.to_string(),
23622362
since: match self.level {
23632363
attr::Stable { ref since } => since.to_string(),
23642364
_ => String::new(),

src/librustdoc/clean/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,7 @@ pub struct ProcMacro {
15251525
#[derive(Clone, Debug)]
15261526
pub struct Stability {
15271527
pub level: stability::StabilityLevel,
1528-
pub feature: Option<String>,
1528+
pub feature: String,
15291529
pub since: String,
15301530
pub unstable_reason: Option<String>,
15311531
pub issue: Option<NonZeroU32>,

src/librustdoc/html/render/mod.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -2144,7 +2144,7 @@ fn stability_tags(item: &clean::Item) -> String {
21442144
if item
21452145
.stability
21462146
.as_ref()
2147-
.map(|s| s.level == stability::Unstable && s.feature.as_deref() != Some("rustc_private"))
2147+
.map(|s| s.level == stability::Unstable && s.feature != "rustc_private")
21482148
== Some(true)
21492149
{
21502150
tags += &tag_html("unstable", "Experimental");
@@ -2195,25 +2195,25 @@ fn short_stability(item: &clean::Item, cx: &Context) -> Vec<String> {
21952195

21962196
// Render unstable items. But don't render "rustc_private" crates (internal compiler crates).
21972197
// Those crates are permanently unstable so it makes no sense to render "unstable" everywhere.
2198-
if let Some(stab) = item.stability.as_ref().filter(|stab| {
2199-
stab.level == stability::Unstable && stab.feature.as_deref() != Some("rustc_private")
2200-
}) {
2198+
if let Some(stab) = item
2199+
.stability
2200+
.as_ref()
2201+
.filter(|stab| stab.level == stability::Unstable && stab.feature != "rustc_private")
2202+
{
22012203
let mut message =
22022204
"<span class='emoji'>🔬</span> This is a nightly-only experimental API.".to_owned();
22032205

2204-
if let Some(feature) = stab.feature.as_deref() {
2205-
let mut feature = format!("<code>{}</code>", Escape(&feature));
2206-
if let (Some(url), Some(issue)) = (&cx.shared.issue_tracker_base_url, stab.issue) {
2207-
feature.push_str(&format!(
2208-
"&nbsp;<a href=\"{url}{issue}\">#{issue}</a>",
2209-
url = url,
2210-
issue = issue
2211-
));
2212-
}
2213-
2214-
message.push_str(&format!(" ({})", feature));
2206+
let mut feature = format!("<code>{}</code>", Escape(&stab.feature));
2207+
if let (Some(url), Some(issue)) = (&cx.shared.issue_tracker_base_url, stab.issue) {
2208+
feature.push_str(&format!(
2209+
"&nbsp;<a href=\"{url}{issue}\">#{issue}</a>",
2210+
url = url,
2211+
issue = issue
2212+
));
22152213
}
22162214

2215+
message.push_str(&format!(" ({})", feature));
2216+
22172217
if let Some(unstable_reason) = &stab.unstable_reason {
22182218
let mut ids = cx.id_map.borrow_mut();
22192219
message = format!(

0 commit comments

Comments
 (0)