2727//! ```
2828
2929use rustc_data_structures:: fx:: FxHashMap ;
30- use rustc_errors:: { DiagnosticMessage , SubdiagnosticMessage } ;
30+ use rustc_errors:: { DiagnosticBuilder , DiagnosticMessage } ;
3131use rustc_hir:: def_id:: DefId ;
3232use rustc_middle:: ty:: TyCtxt ;
3333pub ( crate ) use rustc_resolve:: rustdoc:: main_body_opts;
@@ -234,10 +234,6 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
234234
235235 fn next ( & mut self ) -> Option < Self :: Item > {
236236 let event = self . inner . next ( ) ;
237- let compile_fail;
238- let should_panic;
239- let ignore;
240- let edition;
241237 let Some ( Event :: Start ( Tag :: CodeBlock ( kind) ) ) = event else {
242238 return event;
243239 } ;
@@ -253,49 +249,44 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
253249 }
254250 }
255251
256- let parse_result = match kind {
257- CodeBlockKind :: Fenced ( ref lang) => {
258- let parse_result = LangString :: parse_without_check (
259- lang,
260- self . check_error_codes ,
261- false ,
262- self . custom_code_classes_in_docs ,
263- ) ;
264- if !parse_result. rust {
265- let added_classes = parse_result. added_classes ;
266- let lang_string = if let Some ( lang) = parse_result. unknown . first ( ) {
267- format ! ( "language-{}" , lang)
268- } else {
269- String :: new ( )
270- } ;
271- let whitespace = if added_classes. is_empty ( ) { "" } else { " " } ;
272- return Some ( Event :: Html (
273- format ! (
274- "<div class=\" example-wrap\" >\
252+ let LangString { added_classes, compile_fail, should_panic, ignore, edition, .. } =
253+ match kind {
254+ CodeBlockKind :: Fenced ( ref lang) => {
255+ let parse_result = LangString :: parse_without_check (
256+ lang,
257+ self . check_error_codes ,
258+ false ,
259+ self . custom_code_classes_in_docs ,
260+ ) ;
261+ if !parse_result. rust {
262+ let added_classes = parse_result. added_classes ;
263+ let lang_string = if let Some ( lang) = parse_result. unknown . first ( ) {
264+ format ! ( "language-{}" , lang)
265+ } else {
266+ String :: new ( )
267+ } ;
268+ let whitespace = if added_classes. is_empty ( ) { "" } else { " " } ;
269+ return Some ( Event :: Html (
270+ format ! (
271+ "<div class=\" example-wrap\" >\
275272 <pre class=\" {lang_string}{whitespace}{added_classes}\" >\
276273 <code>{text}</code>\
277274 </pre>\
278275 </div>",
279- added_classes = added_classes. join( " " ) ,
280- text = Escape ( & original_text) ,
281- )
282- . into ( ) ,
283- ) ) ;
276+ added_classes = added_classes. join( " " ) ,
277+ text = Escape ( & original_text) ,
278+ )
279+ . into ( ) ,
280+ ) ) ;
281+ }
282+ parse_result
284283 }
285- parse_result
286- }
287- CodeBlockKind :: Indented => Default :: default ( ) ,
288- } ;
284+ CodeBlockKind :: Indented => Default :: default ( ) ,
285+ } ;
289286
290- let added_classes = parse_result. added_classes ;
291287 let lines = original_text. lines ( ) . filter_map ( |l| map_line ( l) . for_html ( ) ) ;
292288 let text = lines. intersperse ( "\n " . into ( ) ) . collect :: < String > ( ) ;
293289
294- compile_fail = parse_result. compile_fail ;
295- should_panic = parse_result. should_panic ;
296- ignore = parse_result. ignore ;
297- edition = parse_result. edition ;
298-
299290 let explicit_edition = edition. is_some ( ) ;
300291 let edition = edition. unwrap_or ( self . edition ) ;
301292
@@ -852,15 +843,17 @@ impl<'tcx> ExtraInfo<'tcx> {
852843 fn error_invalid_codeblock_attr_with_help (
853844 & self ,
854845 msg : impl Into < DiagnosticMessage > ,
855- help : impl Into < SubdiagnosticMessage > ,
846+ f : impl for <' a , ' b > FnOnce (
847+ & ' b mut DiagnosticBuilder < ' a , ( ) > ,
848+ ) -> & ' b mut DiagnosticBuilder < ' a , ( ) > ,
856849 ) {
857850 if let Some ( def_id) = self . def_id . as_local ( ) {
858851 self . tcx . struct_span_lint_hir (
859852 crate :: lint:: INVALID_CODEBLOCK_ATTRIBUTES ,
860853 self . tcx . local_def_id_to_hir_id ( def_id) ,
861854 self . sp ,
862855 msg,
863- |lint| lint . help ( help ) ,
856+ f ,
864857 ) ;
865858 }
866859 }
@@ -1293,6 +1286,21 @@ impl LangString {
12931286 LangStringToken :: LangToken ( x) if x. starts_with ( "edition" ) => {
12941287 data. edition = x[ 7 ..] . parse :: < Edition > ( ) . ok ( ) ;
12951288 }
1289+ LangStringToken :: LangToken ( x)
1290+ if x. starts_with ( "rust" ) && x[ 4 ..] . parse :: < Edition > ( ) . is_ok ( ) =>
1291+ {
1292+ if let Some ( extra) = extra {
1293+ extra. error_invalid_codeblock_attr_with_help (
1294+ format ! ( "unknown attribute `{x}`" ) ,
1295+ |lint| {
1296+ lint. help ( format ! (
1297+ "there is an attribute with a similar name: `edition{}`" ,
1298+ & x[ 4 ..] ,
1299+ ) )
1300+ } ,
1301+ ) ;
1302+ }
1303+ }
12961304 LangStringToken :: LangToken ( x)
12971305 if allow_error_code_check && x. starts_with ( 'E' ) && x. len ( ) == 5 =>
12981306 {
@@ -1337,8 +1345,13 @@ impl LangString {
13371345 } {
13381346 if let Some ( extra) = extra {
13391347 extra. error_invalid_codeblock_attr_with_help (
1340- format ! ( "unknown attribute `{x}`. Did you mean `{flag}`?" ) ,
1341- help,
1348+ format ! ( "unknown attribute `{x}`" ) ,
1349+ |lint| {
1350+ lint. help ( format ! (
1351+ "there is an attribute with a similar name: `{flag}`"
1352+ ) )
1353+ . help ( help)
1354+ } ,
13421355 ) ;
13431356 }
13441357 }
0 commit comments