Skip to content

Commit

Permalink
Rollup merge of rust-lang#128838 - notriddle:notriddle/invalid-tag-is…
Browse files Browse the repository at this point in the history
…-not-rust, r=GuillaumeGomez

rustdoc: do not run doctests with invalid langstrings

rust-lang#124577 (comment)

CC `@decathorpe`
  • Loading branch information
GuillaumeGomez authored Aug 9, 2024
2 parents c04890c + 1d19c2c commit a6a496b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
20 changes: 16 additions & 4 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,7 @@ pub(crate) struct TagIterator<'a, 'tcx> {
data: &'a str,
is_in_attribute_block: bool,
extra: Option<&'a ExtraInfo<'tcx>>,
is_error: bool,
}

#[derive(Clone, Debug, Eq, PartialEq)]
Expand All @@ -950,13 +951,20 @@ struct Indices {

impl<'a, 'tcx> TagIterator<'a, 'tcx> {
pub(crate) fn new(data: &'a str, extra: Option<&'a ExtraInfo<'tcx>>) -> Self {
Self { inner: data.char_indices().peekable(), data, is_in_attribute_block: false, extra }
Self {
inner: data.char_indices().peekable(),
data,
is_in_attribute_block: false,
extra,
is_error: false,
}
}

fn emit_error(&self, err: impl Into<DiagMessage>) {
fn emit_error(&mut self, err: impl Into<DiagMessage>) {
if let Some(extra) = self.extra {
extra.error_invalid_codeblock_attr(err);
}
self.is_error = true;
}

fn skip_separators(&mut self) -> Option<usize> {
Expand Down Expand Up @@ -1154,6 +1162,9 @@ impl<'a, 'tcx> Iterator for TagIterator<'a, 'tcx> {
type Item = LangStringToken<'a>;

fn next(&mut self) -> Option<Self::Item> {
if self.is_error {
return None;
}
let Some(start) = self.skip_separators() else {
if self.is_in_attribute_block {
self.emit_error("unclosed attribute block (`{}`): missing `}` at the end");
Expand Down Expand Up @@ -1342,14 +1353,15 @@ impl LangString {
}
};

call(&mut TagIterator::new(string, extra));
let mut tag_iter = TagIterator::new(string, extra);
call(&mut tag_iter);

// ignore-foo overrides ignore
if !ignores.is_empty() {
data.ignore = Ignore::Some(ignores);
}

data.rust &= !seen_custom_tag && (!seen_other_tags || seen_rust_tags);
data.rust &= !seen_custom_tag && (!seen_other_tags || seen_rust_tags) && !tag_iter.is_error;

data
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/html/markdown/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn test_lang_string_parse() {
..Default::default()
});
// error
t(LangString { original: "{rust}".into(), rust: true, ..Default::default() });
t(LangString { original: "{rust}".into(), rust: false, ..Default::default() });
t(LangString {
original: "{.rust}".into(),
rust: true,
Expand Down Expand Up @@ -233,7 +233,7 @@ fn test_lang_string_parse() {
..Default::default()
});
// error
t(LangString { original: "{class=first=second}".into(), rust: true, ..Default::default() });
t(LangString { original: "{class=first=second}".into(), rust: false, ..Default::default() });
// error
t(LangString {
original: "{class=first.second}".into(),
Expand Down Expand Up @@ -261,7 +261,7 @@ fn test_lang_string_parse() {
..Default::default()
});
// error
t(LangString { original: r#"{class=f"irst"}"#.into(), rust: true, ..Default::default() });
t(LangString { original: r#"{class=f"irst"}"#.into(), rust: false, ..Default::default() });
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@ compile-flags:--test
//@ check-pass
#![allow(rustdoc::invalid_codeblock_attributes)]

// https://github.com/rust-lang/rust/pull/124577#issuecomment-2276034737

// Test that invalid langstrings don't get run.

/// ```{rust,ignore}
/// panic!();
/// ```
pub struct Foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

0 comments on commit a6a496b

Please sign in to comment.