Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use TLS less in rustc_span #73110

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/librustc_middle/ich/hcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ impl<'a> HashStable<StableHashingContext<'a>> for ast::NodeId {
}

impl<'a> rustc_span::HashStableContext for StableHashingContext<'a> {
fn session_globals(&self) -> &rustc_span::SessionGlobals {
&self.sess.parse_sess.span_globals
}
fn hash_spans(&self) -> bool {
self.hash_spans
}
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_session/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_feature::{find_feature_issue, GateIssue, UnstableFeatures};
use rustc_span::edition::Edition;
use rustc_span::hygiene::ExpnId;
use rustc_span::source_map::{FilePathMapping, SourceMap};
use rustc_span::{MultiSpan, Span, Symbol};
use rustc_span::{MultiSpan, Span, Symbol, SessionGlobals};

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

impl ParseSess {
Expand All @@ -150,6 +151,7 @@ impl ParseSess {
}

pub fn with_span_handler(handler: Handler, source_map: Lrc<SourceMap>) -> Self {
let span_globals = rustc_span::SESSION_GLOBALS.with(|g| g.clone());
Self {
span_diagnostic: handler,
unstable_features: UnstableFeatures::from_environment(),
Expand All @@ -167,6 +169,7 @@ impl ParseSess {
reached_eof: Lock::new(false),
env_depinfo: Default::default(),
type_ascription_path_suggestions: Default::default(),
span_globals
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/librustc_span/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl SessionGlobals {
}

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

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

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

let globals = ctx.session_globals();

// If this is not an empty or invalid span, we want to hash the last
// position that belongs to it, as opposed to hashing the first
// position past it.
let span = self.data();
let span = self.data_from_globals(globals);
let (file_lo, line_lo, col_lo) = match ctx.byte_pos_to_line_and_col(span.lo) {
Some(pos) => pos,
None => {
Expand Down
14 changes: 12 additions & 2 deletions src/librustc_span/span_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use crate::hygiene::SyntaxContext;
use crate::SESSION_GLOBALS;
use crate::{BytePos, SpanData};
use crate::{BytePos, SpanData, SessionGlobals};

use rustc_data_structures::fx::FxHashMap;

Expand Down Expand Up @@ -92,6 +92,16 @@ impl Span {

#[inline]
pub fn data(self) -> SpanData {
self.data_with_interner(|index| with_span_interner(|interner| *interner.get(index)))
}

#[inline]
pub fn data_from_globals(self, globals: &SessionGlobals) -> SpanData {
self.data_with_interner(|index| *globals.span_interner.lock().get(index))
}

#[inline]
pub fn data_with_interner(self, interner_get: impl FnOnce(u32) -> SpanData) -> SpanData {
if self.len_or_tag != LEN_TAG {
// Inline format.
debug_assert!(self.len_or_tag as u32 <= MAX_LEN);
Expand All @@ -104,7 +114,7 @@ impl Span {
// Interned format.
debug_assert!(self.ctxt_or_zero == 0);
let index = self.base_or_index;
with_span_interner(|interner| *interner.get(index))
interner_get(index)
}
}
}
Expand Down