Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 41 additions & 10 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,11 @@ impl<'a> Iterator for TokenIter<'a> {

/// Classifies into identifier class; returns `None` if this is a non-keyword identifier.
fn get_real_ident_class(text: &str, allow_path_keywords: bool) -> Option<Class> {
let ignore: &[&str] =
if allow_path_keywords { &["self", "Self", "super", "crate"] } else { &["self", "Self"] };
let ignore: &[&str] = if allow_path_keywords {
&["self", "Self", "super", "crate", "_"]
} else {
&["self", "Self", "_"]
};
if ignore.contains(&text) {
return None;
}
Expand All @@ -798,6 +801,9 @@ fn get_real_ident_class(text: &str, allow_path_keywords: bool) -> Option<Class>
})
}

/// Used to know if a keyword followed by a `!` should never be treated as a macro.
const KEYWORDS_FOLLOWABLE_BY_VALUE: &[&str] = &["if", "while", "match", "break", "return"];

/// This iterator comes from the same idea than "Peekable" except that it allows to "peek" more than
/// just the next item by using `peek_next`. The `peek` method always returns the next item after
/// the current one whereas `peek_next` will return the next item after the last one peeked.
Expand Down Expand Up @@ -1017,6 +1023,19 @@ impl<'src> Classifier<'src> {
}
}

fn new_macro_span(
&mut self,
text: &'src str,
sink: &mut dyn FnMut(Span, Highlight<'src>),
before: u32,
file_span: Span,
) {
self.in_macro = true;
let span = new_span(before, text, file_span);
sink(DUMMY_SP, Highlight::EnterSpan { class: Class::Macro(span) });
sink(span, Highlight::Token { text, class: None });
}

/// Single step of highlighting. This will classify `token`, but maybe also a couple of
/// following ones as well.
///
Expand Down Expand Up @@ -1223,17 +1242,18 @@ impl<'src> Classifier<'src> {
LiteralKind::Float { .. } | LiteralKind::Int { .. } => Class::Number,
},
TokenKind::GuardedStrPrefix => return no_highlight(sink),
TokenKind::Ident | TokenKind::RawIdent
if self.peek_non_whitespace() == Some(TokenKind::Bang) =>
{
self.in_macro = true;
let span = new_span(before, text, file_span);
sink(DUMMY_SP, Highlight::EnterSpan { class: Class::Macro(span) });
sink(span, Highlight::Token { text, class: None });
TokenKind::RawIdent if self.peek_non_whitespace() == Some(TokenKind::Bang) => {
self.new_macro_span(text, sink, before, file_span);
return;
}
TokenKind::Ident => {
match get_real_ident_class(text, false) {
// If it's not a keyword and the next non whitespace token is a `!`, then
// we consider it's a macro.
None if self.peek_non_whitespace() == Some(TokenKind::Bang) => {
self.new_macro_span(text, sink, before, file_span);
return;
}
None => match text {
"Option" | "Result" => Class::PreludeTy(new_span(before, text, file_span)),
"Some" | "None" | "Ok" | "Err" => {
Expand All @@ -1249,7 +1269,18 @@ impl<'src> Classifier<'src> {
"self" | "Self" => Class::Self_(new_span(before, text, file_span)),
_ => Class::Ident(new_span(before, text, file_span)),
},
Some(c) => c,
Some(c) => {
// So if it's not a keyword which can be followed by a value (like `if` or
// `return`) and the next non-whitespace token is a `!`, then we consider
// it's a macro.
if !KEYWORDS_FOLLOWABLE_BY_VALUE.contains(&text)
&& self.peek_non_whitespace() == Some(TokenKind::Bang)
{
self.new_macro_span(text, sink, before, file_span);
return;
}
c
}
}
}
TokenKind::RawIdent | TokenKind::UnknownPrefix | TokenKind::InvalidIdent => {
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/html/highlight/fixtures/sample.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
#[cfg(target_os = <span class="string">"windows"</span>)]
</span><span class="kw">fn </span>main() -&gt; () {
<span class="kw">let </span>foo = <span class="bool-val">true </span>&amp;&amp; <span class="bool-val">false </span>|| <span class="bool-val">true</span>;
<span class="kw">let _</span>: <span class="kw-2">*const </span>() = <span class="number">0</span>;
<span class="kw">let _ </span>= <span class="kw-2">&amp;</span>foo;
<span class="kw">let _ </span>= &amp;&amp;foo;
<span class="kw">let _ </span>= <span class="kw-2">*</span>foo;
<span class="kw">let </span>_: <span class="kw-2">*const </span>() = <span class="number">0</span>;
<span class="kw">let </span>_ = <span class="kw-2">&amp;</span>foo;
<span class="kw">let </span>_ = &amp;&amp;foo;
<span class="kw">let </span>_ = <span class="kw-2">*</span>foo;
<span class="macro">mac!</span>(foo, <span class="kw-2">&amp;mut </span>bar);
<span class="macro">assert!</span>(<span class="self">self</span>.length &lt; N &amp;&amp; index &lt;= <span class="self">self</span>.length);
::std::env::var(<span class="string">"gateau"</span>).is_ok();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This code crashed because a `if` followed by a `!` was considered a macro,
// creating an invalid class stack.
// Regression test for <https://github.com/rust-lang/rust/issues/148617>.

//@ compile-flags: -Zunstable-options --generate-macro-expansion

enum Enum {
Variant,
}

pub fn repro() {
if !matches!(Enum::Variant, Enum::Variant) {}
}
19 changes: 19 additions & 0 deletions tests/rustdoc/source-code-pages/keyword-macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This test ensures that keywords which can be followed by values (and therefore `!`)
// are not considered as macros.
// This is a regression test for <https://github.com/rust-lang/rust/issues/148617>.

#![crate_name = "foo"]

//@ has 'src/foo/keyword-macros.rs.html'

fn a() {
//@ has - '//*[@class="rust"]//*[@class="kw"]' 'if'
if !true{}
//@ has - '//*[@class="rust"]//*[@class="kw"]' 'match'
match !true { _ => {} }
//@ has - '//*[@class="rust"]//*[@class="kw"]' 'while'
let _ = while !true {
//@ has - '//*[@class="rust"]//*[@class="kw"]' 'break'
break !true;
};
}
Loading