1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//! Validates syntax inside Rust code blocks (\`\`\`rust).
use rustc_data_structures::sync::{Lock, Lrc};
use rustc_errors::{
    emitter::Emitter, translation::Translate, Applicability, Diagnostic, Handler,
    LazyFallbackBundle, LintDiagnosticBuilder,
};
use rustc_parse::parse_stream_from_source_str;
use rustc_session::parse::ParseSess;
use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId};
use rustc_span::source_map::{FilePathMapping, SourceMap};
use rustc_span::{FileName, InnerSpan, DUMMY_SP};

use crate::clean;
use crate::core::DocContext;
use crate::html::markdown::{self, RustCodeBlock};
use crate::passes::Pass;
use crate::visit::DocVisitor;

pub(crate) const CHECK_CODE_BLOCK_SYNTAX: Pass = Pass {
    name: "check-code-block-syntax",
    run: check_code_block_syntax,
    description: "validates syntax inside Rust code blocks",
};

pub(crate) fn check_code_block_syntax(
    krate: clean::Crate,
    cx: &mut DocContext<'_>,
) -> clean::Crate {
    SyntaxChecker { cx }.visit_crate(&krate);
    krate
}

struct SyntaxChecker<'a, 'tcx> {
    cx: &'a DocContext<'tcx>,
}

impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
    fn check_rust_syntax(&self, item: &clean::Item, dox: &str, code_block: RustCodeBlock) {
        let buffer = Lrc::new(Lock::new(Buffer::default()));
        let fallback_bundle =
            rustc_errors::fallback_fluent_bundle(rustc_errors::DEFAULT_LOCALE_RESOURCES, false);
        let emitter = BufferEmitter { buffer: Lrc::clone(&buffer), fallback_bundle };

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

        let edition = code_block.lang_string.edition.unwrap_or_else(|| self.cx.tcx.sess.edition());
        let expn_data = ExpnData::default(
            ExpnKind::AstPass(AstPass::TestHarness),
            DUMMY_SP,
            edition,
            None,
            None,
        );
        let expn_id =
            self.cx.tcx.with_stable_hashing_context(|hcx| LocalExpnId::fresh(expn_data, hcx));
        let span = DUMMY_SP.fresh_expansion(expn_id);

        let is_empty = rustc_driver::catch_fatal_errors(|| {
            parse_stream_from_source_str(
                FileName::Custom(String::from("doctest")),
                source,
                &sess,
                Some(span),
            )
            .is_empty()
        })
        .unwrap_or(false);
        let buffer = buffer.borrow();

        if !buffer.has_errors && !is_empty {
            // No errors in a non-empty program.
            return;
        }

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

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

        // The span and whether it is precise or not.
        let (sp, precise_span) = match super::source_span_for_markdown_range(
            self.cx.tcx,
            dox,
            &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<'_, ()>| {
            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);

            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 {
                    diag.span_suggestion(
                        sp.from_inner(InnerSpan::new(0, 3)).shrink_to_hi(),
                        explanation,
                        "text",
                        Applicability::MachineApplicable,
                    );
                }
            } else if empty_block || is_ignore {
                diag.help(&format!("{}: ```text", explanation));
            }

            // FIXME(#67563): Provide more context for these errors by displaying the spans inline.
            for message in buffer.messages.iter() {
                diag.note(message);
            }

            diag.emit();
        };

        // 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(
            crate::lint::INVALID_RUST_CODEBLOCKS,
            hir_id,
            sp,
            diag_builder,
        );
    }
}

impl<'a, 'tcx> DocVisitor for SyntaxChecker<'a, 'tcx> {
    fn visit_item(&mut self, item: &clean::Item) {
        if let Some(dox) = &item.attrs.collapsed_doc_value() {
            let sp = item.attr_span(self.cx.tcx);
            let extra = crate::html::markdown::ExtraInfo::new_did(
                self.cx.tcx,
                item.item_id.expect_def_id(),
                sp,
            );
            for code_block in markdown::rust_code_blocks(dox, &extra) {
                self.check_rust_syntax(item, dox, code_block);
            }
        }

        self.visit_item_recur(item)
    }
}

#[derive(Default)]
struct Buffer {
    messages: Vec<String>,
    has_errors: bool,
}

struct BufferEmitter {
    buffer: Lrc<Lock<Buffer>>,
    fallback_bundle: LazyFallbackBundle,
}

impl Translate for BufferEmitter {
    fn fluent_bundle(&self) -> Option<&Lrc<rustc_errors::FluentBundle>> {
        None
    }

    fn fallback_fluent_bundle(&self) -> &rustc_errors::FluentBundle {
        &**self.fallback_bundle
    }
}

impl Emitter for BufferEmitter {
    fn emit_diagnostic(&mut self, diag: &Diagnostic) {
        let mut buffer = self.buffer.borrow_mut();
        // FIXME(davidtwco): need to support translation here eventually
        buffer.messages.push(format!("error from rustc: {}", diag.message[0].0.expect_str()));
        if diag.is_error() {
            buffer.has_errors = true;
        }
    }

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