check_code_block_syntax.rs 6.5 KB
Newer Older
1
//! Validates syntax inside Rust code blocks (\`\`\`rust).
2
use rustc_data_structures::sync::{Lock, Lrc};
3
use rustc_errors::{emitter::Emitter, Applicability, Diagnostic, Handler};
4
use rustc_middle::lint::LintDiagnosticBuilder;
5
use rustc_parse::parse_stream_from_source_str;
M
Mazdak Farrokhzad 已提交
6
use rustc_session::parse::ParseSess;
7
use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId};
8
use rustc_span::source_map::{FilePathMapping, SourceMap};
9
use rustc_span::{FileName, InnerSpan, DUMMY_SP};
10

11 12 13
use crate::clean;
use crate::core::DocContext;
use crate::html::markdown::{self, RustCodeBlock};
J
Joshua Nelson 已提交
14
use crate::passes::Pass;
15
use crate::visit::DocVisitor;
16

17
crate const CHECK_CODE_BLOCK_SYNTAX: Pass = Pass {
18
    name: "check-code-block-syntax",
19
    run: check_code_block_syntax,
20 21
    description: "validates syntax inside Rust code blocks",
};
22

J
Joshua Nelson 已提交
23
crate fn check_code_block_syntax(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
24 25
    SyntaxChecker { cx }.visit_crate(&krate);
    krate
26 27
}

28
struct SyntaxChecker<'a, 'tcx> {
29
    cx: &'a DocContext<'tcx>,
30 31
}

32
impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
33
    fn check_rust_syntax(&self, item: &clean::Item, dox: &str, code_block: RustCodeBlock) {
34 35
        let buffer = Lrc::new(Lock::new(Buffer::default()));
        let emitter = BufferEmitter { buffer: Lrc::clone(&buffer) };
36

37
        let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
38
        let handler = Handler::with_emitter(false, None, Box::new(emitter));
39
        let source = dox[code_block.code].to_owned();
40
        let sess = ParseSess::with_span_handler(handler, sm);
41

G
Guillaume Gomez 已提交
42
        let edition = code_block.lang_string.edition.unwrap_or_else(|| self.cx.tcx.sess.edition());
43 44 45 46 47 48 49
        let expn_data = ExpnData::default(
            ExpnKind::AstPass(AstPass::TestHarness),
            DUMMY_SP,
            edition,
            None,
            None,
        );
50 51
        let expn_id = LocalExpnId::fresh(expn_data, self.cx.tcx.create_stable_hashing_context());
        let span = DUMMY_SP.fresh_expansion(expn_id);
52

53 54 55 56 57
        let is_empty = rustc_driver::catch_fatal_errors(|| {
            parse_stream_from_source_str(
                FileName::Custom(String::from("doctest")),
                source,
                &sess,
58
                Some(span),
59 60
            )
            .is_empty()
61
        })
62 63
        .unwrap_or(false);
        let buffer = buffer.borrow();
64

J
Joshua Nelson 已提交
65
        if !buffer.has_errors && !is_empty {
66 67 68 69
            // No errors in a non-empty program.
            return;
        }

70 71
        let Some(local_id) = item.def_id.as_def_id().and_then(|x| x.as_local())
        else {
72 73
            // We don't need to check the syntax for other crates so returning
            // without doing anything should not be a problem.
74
            return;
75 76 77
        };

        let hir_id = self.cx.tcx.hir().local_def_id_to_hir_id(local_id);
78 79
        let empty_block = code_block.lang_string == Default::default() && code_block.is_fenced;
        let is_ignore = code_block.lang_string.ignore != markdown::Ignore::None;
80 81 82 83

        // The span and whether it is precise or not.
        let (sp, precise_span) = match super::source_span_for_markdown_range(
            self.cx.tcx,
G
Guillaume Gomez 已提交
84
            dox,
85 86 87 88 89 90 91 92 93 94
            &code_block.range,
            &item.attrs,
        ) {
            Some(sp) => (sp, true),
            None => (item.attr_span(self.cx.tcx), false),
        };

        // lambda that will use the lint to start a new diagnostic and add
        // a suggestion to it when needed.
        let diag_builder = |lint: LintDiagnosticBuilder<'_>| {
95 96 97 98 99 100 101 102 103 104 105 106
            let explanation = if is_ignore {
                "`ignore` code blocks require valid Rust code for syntax highlighting; \
                    mark blocks that do not contain Rust code as text"
            } else {
                "mark blocks that do not contain Rust code as text"
            };
            let msg = if buffer.has_errors {
                "could not parse code block as Rust code"
            } else {
                "Rust code block is empty"
            };
            let mut diag = lint.build(msg);
107

108 109 110 111 112 113 114 115 116
            if precise_span {
                if is_ignore {
                    // giving an accurate suggestion is hard because `ignore` might not have come first in the list.
                    // just give a `help` instead.
                    diag.span_help(
                        sp.from_inner(InnerSpan::new(0, 3)),
                        &format!("{}: ```text", explanation),
                    );
                } else if empty_block {
117
                    diag.span_suggestion(
118
                        sp.from_inner(InnerSpan::new(0, 3)).shrink_to_hi(),
119
                        explanation,
120
                        String::from("text"),
121 122 123
                        Applicability::MachineApplicable,
                    );
                }
124 125 126
            } else if empty_block || is_ignore {
                diag.help(&format!("{}: ```text", explanation));
            }
127

128
            // FIXME(#67563): Provide more context for these errors by displaying the spans inline.
129
            for message in buffer.messages.iter() {
G
Guillaume Gomez 已提交
130
                diag.note(message);
131 132
            }

133
            diag.emit();
134 135 136 137 138 139
        };

        // Finally build and emit the completed diagnostic.
        // All points of divergence have been handled earlier so this can be
        // done the same way whether the span is precise or not.
        self.cx.tcx.struct_span_lint_hir(
140
            crate::lint::INVALID_RUST_CODEBLOCKS,
141 142 143 144
            hir_id,
            sp,
            diag_builder,
        );
145 146 147
    }
}

148 149
impl<'a, 'tcx> DocVisitor for SyntaxChecker<'a, 'tcx> {
    fn visit_item(&mut self, item: &clean::Item) {
150
        if let Some(dox) = &item.attrs.collapsed_doc_value() {
J
Joshua Nelson 已提交
151
            let sp = item.attr_span(self.cx.tcx);
152 153
            let extra = crate::html::markdown::ExtraInfo::new_did(
                self.cx.tcx,
154
                item.def_id.expect_def_id(),
155 156
                sp,
            );
G
Guillaume Gomez 已提交
157 158
            for code_block in markdown::rust_code_blocks(dox, &extra) {
                self.check_rust_syntax(&item, dox, code_block);
159 160 161
            }
        }

162
        self.visit_item_recur(item)
163 164
    }
}
165

166 167 168 169 170 171
#[derive(Default)]
struct Buffer {
    messages: Vec<String>,
    has_errors: bool,
}

172
struct BufferEmitter {
173
    buffer: Lrc<Lock<Buffer>>,
174 175 176 177
}

impl Emitter for BufferEmitter {
    fn emit_diagnostic(&mut self, diag: &Diagnostic) {
178 179 180 181 182
        let mut buffer = self.buffer.borrow_mut();
        buffer.messages.push(format!("error from rustc: {}", diag.message[0].0));
        if diag.is_error() {
            buffer.has_errors = true;
        }
183 184 185 186 187 188
    }

    fn source_map(&self) -> Option<&Lrc<SourceMap>> {
        None
    }
}