Skip to content

Commit

Permalink
Rollup merge of #136927 - GuillaumeGomez:add-missing-hashtag-escape, …
Browse files Browse the repository at this point in the history
…r=notriddle

Correctly escape hashtags when running `invalid_rust_codeblocks` lint

Fixes #136899.

We forgot to use `map_line` when we wrote this lint.

r? ``@notriddle``
  • Loading branch information
jhpratt authored Feb 13, 2025
2 parents de712f9 + 17cf100 commit 9fe0d25
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,7 @@ impl IndividualTestOptions {
/// [`clean`]: crate::clean
/// [`run_merged_tests`]: crate::doctest::runner::DocTestRunner::run_merged_tests
/// [`generate_unique_doctest`]: crate::doctest::make::DocTestBuilder::generate_unique_doctest
#[derive(Debug)]
pub(crate) struct ScrapedDocTest {
filename: FileName,
line: usize,
Expand Down
8 changes: 5 additions & 3 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl ErrorCodes {
/// Controls whether a line will be hidden or shown in HTML output.
///
/// All lines are used in documentation tests.
enum Line<'a> {
pub(crate) enum Line<'a> {
Hidden(&'a str),
Shown(Cow<'a, str>),
}
Expand All @@ -152,20 +152,22 @@ impl<'a> Line<'a> {
}
}

fn for_code(self) -> Cow<'a, str> {
pub(crate) fn for_code(self) -> Cow<'a, str> {
match self {
Line::Shown(l) => l,
Line::Hidden(l) => Cow::Borrowed(l),
}
}
}

/// This function is used to handle the "hidden lines" (ie starting with `#`) in
/// doctests. It also transforms `##` back into `#`.
// FIXME: There is a minor inconsistency here. For lines that start with ##, we
// have no easy way of removing a potential single space after the hashes, which
// is done in the single # case. This inconsistency seems okay, if non-ideal. In
// order to fix it we'd have to iterate to find the first non-# character, and
// then reallocate to remove it; which would make us return a String.
fn map_line(s: &str) -> Line<'_> {
pub(crate) fn map_line(s: &str) -> Line<'_> {
let trimmed = s.trim();
if trimmed.starts_with("##") {
Line::Shown(Cow::Owned(s.replacen("##", "#", 1)))
Expand Down
7 changes: 6 additions & 1 deletion src/librustdoc/passes/lint/check_code_block_syntax.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Validates syntax inside Rust code blocks (\`\`\`rust).
use std::borrow::Cow;
use std::sync::Arc;

use rustc_data_structures::sync::Lock;
Expand Down Expand Up @@ -43,7 +44,11 @@ fn check_rust_syntax(

let sm = Arc::new(SourceMap::new(FilePathMapping::empty()));
let dcx = DiagCtxt::new(Box::new(emitter)).disable_warnings();
let source = dox[code_block.code].to_owned();
let source = dox[code_block.code]
.lines()
.map(|line| crate::html::markdown::map_line(line).for_code())
.intersperse(Cow::Borrowed("\n"))
.collect::<String>();
let psess = ParseSess::with_dcx(dcx, sm);

let edition = code_block.lang_string.edition.unwrap_or_else(|| cx.tcx.sess.edition());
Expand Down
17 changes: 17 additions & 0 deletions tests/rustdoc-ui/hashtag-doctest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This test ensures that `##` are not emitting a warning when generating
// docs with the 2024 edition (or any edition).
// Regression test for <https://github.com/rust-lang/rust/issues/136899>.

//@ check-pass
//@revisions: edition2021 edition2024
//@[edition2021] edition:2021
//@[edition2024] edition:2024

#![deny(warnings)]

//! Test
//!
//! ```
//! ##[allow(dead_code)]
//! println!("hello world");
//! ```

0 comments on commit 9fe0d25

Please sign in to comment.