Rollup merge of #76689 - jyn514:update-pulldown, r=GuillaumeGomez

Upgrade to pulldown-cmark 0.8.0

Thanks to marcusklaas' hard work in https://github.com/raphlinus/pulldown-cmark/pull/469, this fixes a lot of rustdoc bugs!

- Get rid of unnecessary `RefCell`
- Fix duplicate warnings for broken implicit reference link
- Remove unnecessary copy of links

Closes https://github.com/rust-lang/rust/issues/73264, closes https://github.com/rust-lang/rust/issues/76687.
r? @euclio

I'm not sure if the switch away from `locate` fixes any open bugs - euclio mentioned some in https://github.com/raphlinus/pulldown-cmark/issues/165, but I didn't see any related issues open for rustdoc. Let me know if I missed one.
This commit is contained in:
Dylan DPC 2020-09-16 01:30:44 +02:00 committed by GitHub
commit 1fd22fc34e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 52 additions and 20 deletions

View File

@ -534,7 +534,7 @@ dependencies = [
"if_chain", "if_chain",
"itertools 0.9.0", "itertools 0.9.0",
"lazy_static", "lazy_static",
"pulldown-cmark", "pulldown-cmark 0.7.2",
"quine-mc_cluskey", "quine-mc_cluskey",
"quote", "quote",
"regex-syntax", "regex-syntax",
@ -1853,7 +1853,7 @@ dependencies = [
"log", "log",
"memchr", "memchr",
"open", "open",
"pulldown-cmark", "pulldown-cmark 0.7.2",
"regex", "regex",
"serde", "serde",
"serde_derive", "serde_derive",
@ -2511,6 +2511,17 @@ dependencies = [
"unicase", "unicase",
] ]
[[package]]
name = "pulldown-cmark"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffade02495f22453cd593159ea2f59827aae7f53fa8323f756799b670881dcf8"
dependencies = [
"bitflags",
"memchr",
"unicase",
]
[[package]] [[package]]
name = "punycode" name = "punycode"
version = "0.4.1" version = "0.4.1"
@ -4122,7 +4133,7 @@ dependencies = [
"expect-test", "expect-test",
"itertools 0.9.0", "itertools 0.9.0",
"minifier", "minifier",
"pulldown-cmark", "pulldown-cmark 0.8.0",
"rustc-rayon", "rustc-rayon",
"serde", "serde",
"serde_json", "serde_json",

View File

@ -8,7 +8,7 @@ edition = "2018"
path = "lib.rs" path = "lib.rs"
[dependencies] [dependencies]
pulldown-cmark = { version = "0.7", default-features = false } pulldown-cmark = { version = "0.8", default-features = false }
minifier = "0.0.33" minifier = "0.0.33"
rayon = { version = "0.3.0", package = "rustc-rayon" } rayon = { version = "0.3.0", package = "rustc-rayon" }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }

View File

@ -27,7 +27,6 @@
use rustc_span::edition::Edition; use rustc_span::edition::Edition;
use rustc_span::Span; use rustc_span::Span;
use std::borrow::Cow; use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::default::Default; use std::default::Default;
use std::fmt::Write; use std::fmt::Write;
@ -39,7 +38,7 @@
use crate::html::highlight; use crate::html::highlight;
use crate::html::toc::TocBuilder; use crate::html::toc::TocBuilder;
use pulldown_cmark::{html, CodeBlockKind, CowStr, Event, Options, Parser, Tag}; use pulldown_cmark::{html, BrokenLink, CodeBlockKind, CowStr, Event, Options, Parser, Tag};
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
@ -931,15 +930,17 @@ pub fn into_string(self) -> String {
if md.is_empty() { if md.is_empty() {
return String::new(); return String::new();
} }
let replacer = |_: &str, s: &str| { let mut replacer = |broken_link: BrokenLink<'_>| {
if let Some(link) = links.iter().find(|link| &*link.original_text == s) { if let Some(link) =
Some((link.href.clone(), link.new_text.clone())) links.iter().find(|link| &*link.original_text == broken_link.reference)
{
Some((link.href.as_str().into(), link.new_text.as_str().into()))
} else { } else {
None None
} }
}; };
let p = Parser::new_with_broken_link_callback(md, opts(), Some(&replacer)); let p = Parser::new_with_broken_link_callback(md, opts(), Some(&mut replacer));
let mut s = String::with_capacity(md.len() * 3 / 2); let mut s = String::with_capacity(md.len() * 3 / 2);
@ -1009,9 +1010,11 @@ pub fn into_string(self) -> String {
return String::new(); return String::new();
} }
let replacer = |_: &str, s: &str| { let mut replacer = |broken_link: BrokenLink<'_>| {
if let Some(link) = links.iter().find(|link| &*link.original_text == s) { if let Some(link) =
Some((link.href.clone(), link.new_text.clone())) links.iter().find(|link| &*link.original_text == broken_link.reference)
{
Some((link.href.as_str().into(), link.new_text.as_str().into()))
} else { } else {
None None
} }
@ -1020,7 +1023,7 @@ pub fn into_string(self) -> String {
let p = Parser::new_with_broken_link_callback( let p = Parser::new_with_broken_link_callback(
md, md,
Options::ENABLE_STRIKETHROUGH, Options::ENABLE_STRIKETHROUGH,
Some(&replacer), Some(&mut replacer),
); );
let mut s = String::new(); let mut s = String::new();
@ -1067,7 +1070,7 @@ pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
} }
let mut links = vec![]; let mut links = vec![];
let shortcut_links = RefCell::new(vec![]); let mut shortcut_links = vec![];
{ {
let locate = |s: &str| unsafe { let locate = |s: &str| unsafe {
@ -1084,11 +1087,13 @@ pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
} }
}; };
let push = |_: &str, s: &str| { let mut push = |link: BrokenLink<'_>| {
shortcut_links.borrow_mut().push((s.to_owned(), locate(s))); // FIXME: use `link.span` instead of `locate`
// (doing it now includes the `[]` as well as the text)
shortcut_links.push((link.reference.to_owned(), locate(link.reference)));
None None
}; };
let p = Parser::new_with_broken_link_callback(md, opts(), Some(&push)); let p = Parser::new_with_broken_link_callback(md, opts(), Some(&mut push));
// There's no need to thread an IdMap through to here because // There's no need to thread an IdMap through to here because
// the IDs generated aren't going to be emitted anywhere. // the IDs generated aren't going to be emitted anywhere.
@ -1106,8 +1111,7 @@ pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
} }
} }
let mut shortcut_links = shortcut_links.into_inner(); links.append(&mut shortcut_links);
links.extend(shortcut_links.drain(..));
links links
} }

View File

@ -0,0 +1,7 @@
// check-pass
// regression test for #73264
// should only give one error
/// docs [label][with#anchor#error]
//~^ WARNING multiple anchors
pub struct S;

View File

@ -0,0 +1,10 @@
warning: `with#anchor#error` contains multiple anchors
--> $DIR/intra-link-double-anchor.rs:5:18
|
LL | /// docs [label][with#anchor#error]
| ^^^^^^^^^^^^^^^^^ contains invalid anchor
|
= note: `#[warn(broken_intra_doc_links)]` on by default
warning: 1 warning emitted