Skip to content

Commit 5824ab1

Browse files
committed
Support lists and stylings in more places for rustc --explain
1 parent bda221a commit 5824ab1

File tree

5 files changed

+96
-36
lines changed

5 files changed

+96
-36
lines changed

compiler/rustc_error_codes/src/error_codes/E0373.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,4 @@ fn spawn<F: Future + Send + 'static>(future: F) {
7070

7171
Similarly to closures, `async` blocks are not executed immediately and may
7272
capture closed-over data by reference. For more information, see
73-
https://rust-lang.github.io/async-book/03_async_await/01_chapter.html.
73+
<https://rust-lang.github.io/async-book/03_async_await/01_chapter.html>.

compiler/rustc_error_codes/src/error_codes/E0378.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ where
2020

2121
The `DispatchFromDyn` trait currently can only be implemented for
2222
builtin pointer types and structs that are newtype wrappers around them
23-
— that is, the struct must have only one field (except for`PhantomData`),
23+
— that is, the struct must have only one field (except for `PhantomData`),
2424
and that field must itself implement `DispatchFromDyn`.
2525

2626
```

compiler/rustc_errors/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#![feature(box_patterns)]
1616
#![feature(error_reporter)]
1717
#![feature(extract_if)]
18+
#![feature(if_let_guard)]
1819
#![feature(let_chains)]
1920
#![feature(negative_impls)]
2021
#![feature(never_type)]

compiler/rustc_errors/src/markdown/parse.rs

+34-28
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ const CBK: &[u8] = b"```";
1010
const CIL: &[u8] = b"`";
1111
const CMT_E: &[u8] = b"-->";
1212
const CMT_S: &[u8] = b"<!--";
13-
const EMP: &[u8] = b"_";
13+
const EMP_U: &[u8] = b"_";
14+
const EMP_A: &[u8] = b"*";
1415
const HDG: &[u8] = b"#";
1516
const LNK_CHARS: &str = "$-_.+!*'()/&?=:%";
1617
const LNK_E: &[u8] = b"]";
1718
const LNK_S: &[u8] = b"[";
18-
const STG: &[u8] = b"**";
19+
const STG_U: &[u8] = b"__";
20+
const STG_A: &[u8] = b"**";
1921
const STK: &[u8] = b"~~";
20-
const UL1: &[u8] = b"* ";
21-
const UL2: &[u8] = b"- ";
2222

2323
/// Pattern replacements
2424
const REPLACEMENTS: &[(&str, &str)] = &[
@@ -100,22 +100,29 @@ fn parse_recursive<'a>(buf: &'a [u8], ctx: Context) -> MdStream<'_> {
100100
};
101101

102102
let res: ParseResult<'_> = match (top_blk, prev) {
103-
(_, Newline | Whitespace) if loop_buf.starts_with(CMT_S) => {
103+
_ if loop_buf.starts_with(CMT_S) => {
104104
parse_simple_pat(loop_buf, CMT_S, CMT_E, Po::TrimNoEsc, MdTree::Comment)
105105
}
106106
(true, Newline) if loop_buf.starts_with(CBK) => Some(parse_codeblock(loop_buf)),
107-
(_, Newline | Whitespace) if loop_buf.starts_with(CIL) => parse_codeinline(loop_buf),
107+
_ if loop_buf.starts_with(CIL) => parse_codeinline(loop_buf),
108108
(true, Newline | Whitespace) if loop_buf.starts_with(HDG) => parse_heading(loop_buf),
109109
(true, Newline) if loop_buf.starts_with(BRK) => {
110110
Some((MdTree::HorizontalRule, parse_to_newline(loop_buf).1))
111111
}
112-
(_, Newline | Whitespace) if loop_buf.starts_with(EMP) => {
113-
parse_simple_pat(loop_buf, EMP, EMP, Po::None, MdTree::Emphasis)
112+
(_, Newline) if unordered_list_start(loop_buf) => Some(parse_unordered_li(loop_buf)),
113+
(_, Newline | Whitespace) if loop_buf.starts_with(STG_U) => {
114+
parse_simple_pat(loop_buf, STG_U, STG_U, Po::None, MdTree::Strong)
114115
}
115-
(_, Newline | Whitespace) if loop_buf.starts_with(STG) => {
116-
parse_simple_pat(loop_buf, STG, STG, Po::None, MdTree::Strong)
116+
_ if loop_buf.starts_with(STG_A) => {
117+
parse_simple_pat(loop_buf, STG_A, STG_A, Po::None, MdTree::Strong)
117118
}
118-
(_, Newline | Whitespace) if loop_buf.starts_with(STK) => {
119+
(_, Newline | Whitespace) if loop_buf.starts_with(EMP_U) => {
120+
parse_simple_pat(loop_buf, EMP_U, EMP_U, Po::None, MdTree::Emphasis)
121+
}
122+
_ if loop_buf.starts_with(EMP_A) => {
123+
parse_simple_pat(loop_buf, EMP_A, EMP_A, Po::None, MdTree::Emphasis)
124+
}
125+
_ if loop_buf.starts_with(STK) => {
119126
parse_simple_pat(loop_buf, STK, STK, Po::None, MdTree::Strikethrough)
120127
}
121128
(_, Newline | Whitespace) if loop_buf.starts_with(ANC_S) => {
@@ -130,11 +137,8 @@ fn parse_recursive<'a>(buf: &'a [u8], ctx: Context) -> MdStream<'_> {
130137
_ => None,
131138
}
132139
}
133-
(_, Newline) if (loop_buf.starts_with(UL1) || loop_buf.starts_with(UL2)) => {
134-
Some(parse_unordered_li(loop_buf))
135-
}
136140
(_, Newline) if ord_list_start(loop_buf).is_some() => Some(parse_ordered_li(loop_buf)),
137-
(_, Newline | Whitespace) if loop_buf.starts_with(LNK_S) => {
141+
_ if loop_buf.starts_with(LNK_S) => {
138142
parse_any_link(loop_buf, top_blk && prev == Prev::Newline)
139143
}
140144
(_, Escape | _) => None,
@@ -251,7 +255,6 @@ fn parse_heading(buf: &[u8]) -> ParseResult<'_> {
251255

252256
/// Bulleted list
253257
fn parse_unordered_li(buf: &[u8]) -> Parsed<'_> {
254-
debug_assert!(buf.starts_with(b"* ") || buf.starts_with(b"- "));
255258
let (txt, rest) = get_indented_section(&buf[2..]);
256259
let ctx = Context { top_block: false, prev: Prev::Whitespace };
257260
let stream = parse_recursive(trim_ascii_start(txt), ctx);
@@ -267,25 +270,28 @@ fn parse_ordered_li(buf: &[u8]) -> Parsed<'_> {
267270
(MdTree::OrderedListItem(num, stream), rest)
268271
}
269272

270-
/// Find first line that isn't empty or doesn't start with whitespace, that will
271-
/// be our contents
272273
fn get_indented_section(buf: &[u8]) -> (&[u8], &[u8]) {
273-
let mut end = buf.len();
274-
for (idx, window) in buf.windows(2).enumerate() {
275-
let &[ch, next_ch] = window else { unreachable!("always 2 elements") };
276-
if idx >= buf.len().saturating_sub(2) && next_ch == b'\n' {
277-
// End of stream
278-
end = buf.len().saturating_sub(1);
279-
break;
280-
} else if ch == b'\n' && (!next_ch.is_ascii_whitespace() || next_ch == b'\n') {
281-
end = idx;
282-
break;
274+
let mut lines = buf.split(|&byte| byte == b'\n');
275+
let mut end = lines.next().map_or(0, |line| line.len());
276+
for line in lines {
277+
if let Some(first) = line.first() {
278+
if unordered_list_start(line) || !first.is_ascii_whitespace() {
279+
break;
280+
}
283281
}
282+
end += line.len() + 1;
284283
}
285284

286285
(&buf[..end], &buf[end..])
287286
}
288287

288+
fn unordered_list_start(mut buf: &[u8]) -> bool {
289+
while let [b' ', rest @ ..] = buf {
290+
buf = rest;
291+
}
292+
matches!(buf, [b'*' | b'-', b' ', ..])
293+
}
294+
289295
/// Verify a valid ordered list start (e.g. `1.`) and parse it. Returns the
290296
/// parsed number and offset of character after the dot.
291297
fn ord_list_start(buf: &[u8]) -> Option<(u16, usize)> {

compiler/rustc_errors/src/markdown/tests/parse.rs

+59-6
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use ParseOpt as PO;
44
#[test]
55
fn test_parse_simple() {
66
let buf = "**abcd** rest";
7-
let (t, r) = parse_simple_pat(buf.as_bytes(), STG, STG, PO::None, MdTree::Strong).unwrap();
7+
let (t, r) = parse_simple_pat(buf.as_bytes(), b"**", b"**", PO::None, MdTree::Strong).unwrap();
88
assert_eq!(t, MdTree::Strong("abcd"));
99
assert_eq!(r, b" rest");
1010

1111
// Escaping should fail
1212
let buf = r"**abcd\** rest";
13-
let res = parse_simple_pat(buf.as_bytes(), STG, STG, PO::None, MdTree::Strong);
13+
let res = parse_simple_pat(buf.as_bytes(), b"**", b"**", PO::None, MdTree::Strong);
1414
assert!(res.is_none());
1515
}
1616

@@ -141,12 +141,12 @@ fn test_indented_section() {
141141
assert_eq!(str::from_utf8(r).unwrap(), "\nnot ind");
142142

143143
let (txt, rest) = get_indented_section(IND2.as_bytes());
144-
assert_eq!(str::from_utf8(txt).unwrap(), "test end of stream\n 1\n 2");
145-
assert_eq!(str::from_utf8(rest).unwrap(), "\n");
144+
assert_eq!(str::from_utf8(txt).unwrap(), "test end of stream\n 1\n 2\n");
145+
assert_eq!(str::from_utf8(rest).unwrap(), "");
146146

147147
let (txt, rest) = get_indented_section(IND3.as_bytes());
148-
assert_eq!(str::from_utf8(txt).unwrap(), "test empty lines\n 1\n 2");
149-
assert_eq!(str::from_utf8(rest).unwrap(), "\n\nnot ind");
148+
assert_eq!(str::from_utf8(txt).unwrap(), "test empty lines\n 1\n 2\n");
149+
assert_eq!(str::from_utf8(rest).unwrap(), "\nnot ind");
150150
}
151151

152152
const HBT: &str = r"# Heading
@@ -310,3 +310,56 @@ fn test_code_at_start() {
310310
let res = entrypoint(CODE_STARTLINE);
311311
assert_eq!(res, expected);
312312
}
313+
314+
#[test]
315+
fn test_code_in_parens() {
316+
let expected =
317+
vec![MdTree::PlainText("("), MdTree::CodeInline("Foo"), MdTree::PlainText(")")].into();
318+
let res = entrypoint("(`Foo`)");
319+
assert_eq!(res, expected);
320+
}
321+
322+
const LIST_WITH_SPACE: &str = "
323+
para
324+
* l1
325+
* l2
326+
";
327+
328+
#[test]
329+
fn test_list_with_space() {
330+
let expected = vec![
331+
MdTree::PlainText("para"),
332+
MdTree::ParagraphBreak,
333+
MdTree::UnorderedListItem(vec![MdTree::PlainText("l1")].into()),
334+
MdTree::LineBreak,
335+
MdTree::UnorderedListItem(vec![MdTree::PlainText("l2")].into()),
336+
]
337+
.into();
338+
let res = entrypoint(LIST_WITH_SPACE);
339+
assert_eq!(res, expected);
340+
}
341+
342+
const SNAKE_CASE: &str = "
343+
foo*bar*
344+
foo**bar**
345+
foo_bar_
346+
foo__bar__
347+
";
348+
349+
#[test]
350+
fn test_snake_case() {
351+
let expected = vec![
352+
MdTree::PlainText("foo"),
353+
MdTree::Emphasis("bar"),
354+
MdTree::PlainText(" "),
355+
MdTree::PlainText("foo"),
356+
MdTree::Strong("bar"),
357+
MdTree::PlainText(" "),
358+
MdTree::PlainText("foo_bar_"),
359+
MdTree::PlainText(" "),
360+
MdTree::PlainText("foo__bar__"),
361+
]
362+
.into();
363+
let res = entrypoint(SNAKE_CASE);
364+
assert_eq!(res, expected);
365+
}

0 commit comments

Comments
 (0)