Skip to content

Commit

Permalink
Rollup merge of #84953 - GuillaumeGomez:remove-unneeded-with_default_…
Browse files Browse the repository at this point in the history
…session_globals, r=jyn514

Remove unneeded call to with_default_session_globals in rustdoc highlight

This was the origin of the `Span` bug in #84176.

cc `````@Aaron1011`````
r? `````@jyn514`````
  • Loading branch information
Dylan-DPC authored May 6, 2021
2 parents eec3ae3 + 3c489a3 commit 3c5da6e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
37 changes: 17 additions & 20 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use std::iter::Peekable;
use rustc_lexer::{LiteralKind, TokenKind};
use rustc_span::edition::Edition;
use rustc_span::symbol::Symbol;
use rustc_span::with_default_session_globals;

use super::format::Buffer;

Expand Down Expand Up @@ -238,28 +237,26 @@ impl<'a> Classifier<'a> {
/// possibly giving it an HTML span with a class specifying what flavor of
/// token is used.
fn highlight(mut self, sink: &mut dyn FnMut(Highlight<'a>)) {
with_default_session_globals(|| {
loop {
if self
.tokens
.peek()
.map(|t| matches!(t.0, TokenKind::Colon | TokenKind::Ident))
.unwrap_or(false)
{
let tokens = self.get_full_ident_path();
for (token, start, end) in tokens {
let text = &self.src[start..end];
self.advance(token, text, sink);
self.byte_pos += text.len() as u32;
}
}
if let Some((token, text)) = self.next() {
loop {
if self
.tokens
.peek()
.map(|t| matches!(t.0, TokenKind::Colon | TokenKind::Ident))
.unwrap_or(false)
{
let tokens = self.get_full_ident_path();
for (token, start, end) in tokens {
let text = &self.src[start..end];
self.advance(token, text, sink);
} else {
break;
self.byte_pos += text.len() as u32;
}
}
})
if let Some((token, text)) = self.next() {
self.advance(token, text, sink);
} else {
break;
}
}
}

/// Single step of highlighting. This will classify `token`, but maybe also
Expand Down
27 changes: 16 additions & 11 deletions src/librustdoc/html/highlight/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::write_code;
use crate::html::format::Buffer;
use expect_test::expect_file;
use rustc_span::edition::Edition;
use rustc_span::with_default_session_globals;

const STYLE: &str = r#"
<style>
Expand All @@ -17,21 +18,25 @@ const STYLE: &str = r#"

#[test]
fn test_html_highlighting() {
let src = include_str!("fixtures/sample.rs");
let html = {
let mut out = Buffer::new();
write_code(&mut out, src, Edition::Edition2018);
format!("{}<pre><code>{}</code></pre>\n", STYLE, out.into_inner())
};
expect_file!["fixtures/sample.html"].assert_eq(&html);
with_default_session_globals(|| {
let src = include_str!("fixtures/sample.rs");
let html = {
let mut out = Buffer::new();
write_code(&mut out, src, Edition::Edition2018);
format!("{}<pre><code>{}</code></pre>\n", STYLE, out.into_inner())
};
expect_file!["fixtures/sample.html"].assert_eq(&html);
});
}

#[test]
fn test_dos_backline() {
let src = "pub fn foo() {\r\n\
with_default_session_globals(|| {
let src = "pub fn foo() {\r\n\
println!(\"foo\");\r\n\
}\r\n";
let mut html = Buffer::new();
write_code(&mut html, src, Edition::Edition2018);
expect_file!["fixtures/dos_line.html"].assert_eq(&html.into_inner());
let mut html = Buffer::new();
write_code(&mut html, src, Edition::Edition2018);
expect_file!["fixtures/dos_line.html"].assert_eq(&html.into_inner());
});
}

0 comments on commit 3c5da6e

Please sign in to comment.