Skip to content

Commit

Permalink
Rollup merge of #94260 - GuillaumeGomez:infinite-redirection, r=notri…
Browse files Browse the repository at this point in the history
…ddle

Fix rustdoc infinite redirection generation

Someone came to me about a funny bug they had when clicking on any link on [this page](https://world.pages.gitlab.gnome.org/Rust/libadwaita-rs/stable/latest/docs/libadwaita/builders/index.html): it ended one page redirecting to itself indefinitely.

I was able to make a minimum reproducible case to trigger this bug which I now use as a test.

r? ``@notriddle``
  • Loading branch information
matthiaskrgr authored Feb 23, 2022
2 parents 09e4f34 + 1d95acb commit a9eb5f0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 26 deletions.
60 changes: 34 additions & 26 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,19 @@ impl<'tcx> Context<'tcx> {
} else {
tyname.as_str()
};
let page = layout::Page {
css_class: tyname_s,
root_path: &self.root_path(),
static_root_path: self.shared.static_root_path.as_deref(),
title: &title,
description: &desc,
keywords: &keywords,
resource_suffix: &self.shared.resource_suffix,
extra_scripts: &[],
static_extra_scripts: &[],
};

if !self.render_redirect_pages {
let page = layout::Page {
css_class: tyname_s,
root_path: &self.root_path(),
static_root_path: self.shared.static_root_path.as_deref(),
title: &title,
description: &desc,
keywords: &keywords,
resource_suffix: &self.shared.resource_suffix,
extra_scripts: &[],
static_extra_scripts: &[],
};
layout::render(
&self.shared.layout,
&page,
Expand All @@ -223,23 +223,31 @@ impl<'tcx> Context<'tcx> {
)
} else {
if let Some(&(ref names, ty)) = self.cache().paths.get(&it.def_id.expect_def_id()) {
let mut path = String::new();
for name in &names[..names.len() - 1] {
path.push_str(&name.as_str());
path.push('/');
}
path.push_str(&item_path(ty, &names.last().unwrap().as_str()));
match self.shared.redirections {
Some(ref redirections) => {
let mut current_path = String::new();
for name in &self.current {
current_path.push_str(&name.as_str());
current_path.push('/');
if self.current.len() + 1 != names.len()
|| self.current.iter().zip(names.iter()).any(|(a, b)| a != b)
{
// We checked that the redirection isn't pointing to the current file,
// preventing an infinite redirection loop in the generated
// documentation.

let mut path = String::new();
for name in &names[..names.len() - 1] {
path.push_str(&name.as_str());
path.push('/');
}
path.push_str(&item_path(ty, &names.last().unwrap().as_str()));
match self.shared.redirections {
Some(ref redirections) => {
let mut current_path = String::new();
for name in &self.current {
current_path.push_str(&name.as_str());
current_path.push('/');
}
current_path.push_str(&item_path(ty, &names.last().unwrap().as_str()));
redirections.borrow_mut().insert(current_path, path);
}
current_path.push_str(&item_path(ty, &names.last().unwrap().as_str()));
redirections.borrow_mut().insert(current_path, path);
None => return layout::redirect(&format!("{}{}", self.root_path(), path)),
}
None => return layout::redirect(&format!("{}{}", self.root_path(), path)),
}
}
String::new()
Expand Down
29 changes: 29 additions & 0 deletions src/test/rustdoc/infinite-redirection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#![crate_name = "foo"]

// This test ensures that there is no "infinite redirection" file generated (a
// file which redirects to itself).

// We check it's not a redirection file.
// @has 'foo/builders/struct.ActionRowBuilder.html'
// @has - '//*[@id="synthetic-implementations"]' 'Auto Trait Implementations'

// And that the link in the module is targetting it.
// @has 'foo/builders/index.html'
// @has - '//a[@href="struct.ActionRowBuilder.html"]' 'ActionRowBuilder'

mod auto {
mod action_row {
pub struct ActionRowBuilder;
}

#[doc(hidden)]
pub mod builders {
pub use super::action_row::ActionRowBuilder;
}
}

pub use auto::*;

pub mod builders {
pub use crate::auto::builders::*;
}

0 comments on commit a9eb5f0

Please sign in to comment.