Skip to content

Commit fddd771

Browse files
committed
Avoid using TLS when hashing a Span
1 parent c989ac1 commit fddd771

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

src/librustc_middle/ich/hcx.rs

+3
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ impl<'a> HashStable<StableHashingContext<'a>> for ast::NodeId {
226226
}
227227

228228
impl<'a> rustc_span::HashStableContext for StableHashingContext<'a> {
229+
fn session_globals(&self) -> &rustc_span::SessionGlobals {
230+
&self.sess.parse_sess.span_globals
231+
}
229232
fn hash_spans(&self) -> bool {
230233
self.hash_spans
231234
}

src/librustc_session/parse.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_feature::{find_feature_issue, GateIssue, UnstableFeatures};
1111
use rustc_span::edition::Edition;
1212
use rustc_span::hygiene::ExpnId;
1313
use rustc_span::source_map::{FilePathMapping, SourceMap};
14-
use rustc_span::{MultiSpan, Span, Symbol};
14+
use rustc_span::{MultiSpan, Span, Symbol, SessionGlobals};
1515

1616
use std::collections::BTreeMap;
1717
use std::path::PathBuf;
@@ -140,6 +140,7 @@ pub struct ParseSess {
140140
pub env_depinfo: Lock<FxHashSet<(Symbol, Option<Symbol>)>>,
141141
/// All the type ascriptions expressions that have had a suggestion for likely path typo.
142142
pub type_ascription_path_suggestions: Lock<FxHashSet<Span>>,
143+
pub span_globals: Lrc<SessionGlobals>
143144
}
144145

145146
impl ParseSess {
@@ -150,6 +151,7 @@ impl ParseSess {
150151
}
151152

152153
pub fn with_span_handler(handler: Handler, source_map: Lrc<SourceMap>) -> Self {
154+
let span_globals = rustc_span::SESSION_GLOBALS.with(|g| g.clone());
153155
Self {
154156
span_diagnostic: handler,
155157
unstable_features: UnstableFeatures::from_environment(),
@@ -167,6 +169,7 @@ impl ParseSess {
167169
reached_eof: Lock::new(false),
168170
env_depinfo: Default::default(),
169171
type_ascription_path_suggestions: Default::default(),
172+
span_globals
170173
}
171174
}
172175

src/librustc_span/lib.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl SessionGlobals {
8888
}
8989

9090
pub fn with_session_globals<R>(edition: Edition, f: impl FnOnce() -> R) -> R {
91-
let session_globals = SessionGlobals::new(edition);
91+
let session_globals = Lrc::new(SessionGlobals::new(edition));
9292
SESSION_GLOBALS.set(&session_globals, f)
9393
}
9494

@@ -99,7 +99,7 @@ pub fn with_default_session_globals<R>(f: impl FnOnce() -> R) -> R {
9999
// If this ever becomes non thread-local, `decode_syntax_context`
100100
// and `decode_expn_id` will need to be updated to handle concurrent
101101
// deserialization.
102-
scoped_tls::scoped_thread_local!(pub static SESSION_GLOBALS: SessionGlobals);
102+
scoped_tls::scoped_thread_local!(pub static SESSION_GLOBALS: Lrc<SessionGlobals>);
103103

104104
// FIXME: Perhaps this should not implement Rustc{Decodable, Encodable}
105105
//
@@ -1746,6 +1746,7 @@ fn lookup_line(lines: &[BytePos], pos: BytePos) -> isize {
17461746
/// This is a hack to allow using the `HashStable_Generic` derive macro
17471747
/// instead of implementing everything in librustc_middle.
17481748
pub trait HashStableContext {
1749+
fn session_globals(&self) -> &SessionGlobals;
17491750
fn hash_def_id(&mut self, _: DefId, hasher: &mut StableHasher);
17501751
fn hash_crate_num(&mut self, _: CrateNum, hasher: &mut StableHasher);
17511752
fn hash_spans(&self) -> bool;
@@ -1781,10 +1782,12 @@ where
17811782
return;
17821783
}
17831784

1785+
let globals = ctx.session_globals();
1786+
17841787
// If this is not an empty or invalid span, we want to hash the last
17851788
// position that belongs to it, as opposed to hashing the first
17861789
// position past it.
1787-
let span = self.data();
1790+
let span = self.data_from_globals(globals);
17881791
let (file_lo, line_lo, col_lo) = match ctx.byte_pos_to_line_and_col(span.lo) {
17891792
Some(pos) => pos,
17901793
None => {

src/librustc_span/span_encoding.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use crate::hygiene::SyntaxContext;
88
use crate::SESSION_GLOBALS;
9-
use crate::{BytePos, SpanData};
9+
use crate::{BytePos, SpanData, SessionGlobals};
1010

1111
use rustc_data_structures::fx::FxHashMap;
1212

@@ -92,6 +92,16 @@ impl Span {
9292

9393
#[inline]
9494
pub fn data(self) -> SpanData {
95+
self.data_with_interner(|index| with_span_interner(|interner| *interner.get(index)))
96+
}
97+
98+
#[inline]
99+
pub fn data_from_globals(self, globals: &SessionGlobals) -> SpanData {
100+
self.data_with_interner(|index| *globals.span_interner.lock().get(index))
101+
}
102+
103+
#[inline]
104+
pub fn data_with_interner(self, interner_get: impl FnOnce(u32) -> SpanData) -> SpanData {
95105
if self.len_or_tag != LEN_TAG {
96106
// Inline format.
97107
debug_assert!(self.len_or_tag as u32 <= MAX_LEN);
@@ -104,7 +114,7 @@ impl Span {
104114
// Interned format.
105115
debug_assert!(self.ctxt_or_zero == 0);
106116
let index = self.base_or_index;
107-
with_span_interner(|interner| *interner.get(index))
117+
interner_get(index)
108118
}
109119
}
110120
}

0 commit comments

Comments
 (0)