Skip to content

Commit

Permalink
Resolve true and false as booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
jyn514 committed Aug 18, 2020
1 parent f032cba commit 4f9cd74
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
disambiguator,
None | Some(Disambiguator::Namespace(Namespace::TypeNS))
) {
if let Some(prim) = is_primitive(path_str, ns) {
if let Some((path, prim)) = is_primitive(path_str, ns) {
if extra_fragment.is_some() {
return Err(ErrorKind::AnchorFailure(AnchorFailure::Primitive));
}
return Ok((prim, Some(path_str.to_owned())));
return Ok((prim, Some(path.to_owned())));
}
}
return Ok((res, extra_fragment.clone()));
Expand All @@ -239,11 +239,11 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
if value != (ns == ValueNS) {
return Err(ErrorKind::ResolutionFailure);
}
} else if let Some(prim) = is_primitive(path_str, ns) {
} else if let Some((path, prim)) = is_primitive(path_str, ns) {
if extra_fragment.is_some() {
return Err(ErrorKind::AnchorFailure(AnchorFailure::Primitive));
}
return Ok((prim, Some(path_str.to_owned())));
return Ok((prim, Some(path.to_owned())));
} else {
// If resolution failed, it may still be a method
// because methods are not handled by the resolver
Expand All @@ -269,7 +269,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
})
.ok_or(ErrorKind::ResolutionFailure)?;

if let Some(prim) = is_primitive(&path, TypeNS) {
if let Some((path, prim)) = is_primitive(&path, TypeNS) {
let did = primitive_impl(cx, &path).ok_or(ErrorKind::ResolutionFailure)?;
return cx
.tcx
Expand Down Expand Up @@ -1220,11 +1220,22 @@ const PRIMITIVES: &[(&str, Res)] = &[
("f64", Res::PrimTy(hir::PrimTy::Float(rustc_ast::ast::FloatTy::F64))),
("str", Res::PrimTy(hir::PrimTy::Str)),
("bool", Res::PrimTy(hir::PrimTy::Bool)),
("true", Res::PrimTy(hir::PrimTy::Bool)),
("false", Res::PrimTy(hir::PrimTy::Bool)),
("char", Res::PrimTy(hir::PrimTy::Char)),
];

fn is_primitive(path_str: &str, ns: Namespace) -> Option<Res> {
if ns == TypeNS { PRIMITIVES.iter().find(|x| x.0 == path_str).map(|x| x.1) } else { None }
fn is_primitive(path_str: &str, ns: Namespace) -> Option<(&'static str, Res)> {
if ns == TypeNS {
PRIMITIVES
.iter()
.filter(|x| x.0 == path_str)
.copied()
.map(|x| if x.0 == "true" || x.0 == "false" { ("bool", x.1) } else { x })
.next()
} else {
None
}
}

fn primitive_impl(cx: &DocContext<'_>, path_str: &str) -> Option<DefId> {
Expand Down
10 changes: 10 additions & 0 deletions src/test/rustdoc/intra-doc-link-true-false.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![deny(broken_intra_doc_links)]
#![crate_name = "foo"]

// ignore-tidy-linelength

// @has foo/index.html
// @has - '//*[@id="main"]//a[@href="https://doc.rust-lang.org/nightly/std/primitive.bool.html"]' 'true'
// @has - '//*[@id="main"]//a[@href="https://doc.rust-lang.org/nightly/std/primitive.bool.html"]' 'false'

//! A `bool` is either [`true`] or [`false`].

0 comments on commit 4f9cd74

Please sign in to comment.