-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #67614 - Mark-Simulacrum:global-callbacks, r=Zoxc
Set callbacks globally This sets the callbacks from syntax and rustc_errors just once, utilizing static (rather than thread-local) storage.
- Loading branch information
Showing
9 changed files
with
99 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use std::marker::PhantomData; | ||
use std::sync::atomic::{AtomicPtr, Ordering}; | ||
|
||
/// This is essentially an `AtomicPtr` but is guaranteed to always be valid | ||
pub struct AtomicRef<T: 'static>(AtomicPtr<T>, PhantomData<&'static T>); | ||
|
||
impl<T: 'static> AtomicRef<T> { | ||
pub const fn new(initial: &'static T) -> AtomicRef<T> { | ||
AtomicRef(AtomicPtr::new(initial as *const T as *mut T), PhantomData) | ||
} | ||
|
||
pub fn swap(&self, new: &'static T) -> &'static T { | ||
// We never allow storing anything but a `'static` reference so it's safe to | ||
// return it for the same. | ||
unsafe { &*self.0.swap(new as *const T as *mut T, Ordering::SeqCst) } | ||
} | ||
} | ||
|
||
impl<T: 'static> std::ops::Deref for AtomicRef<T> { | ||
type Target = T; | ||
fn deref(&self) -> &Self::Target { | ||
// We never allow storing anything but a `'static` reference so it's safe to lend | ||
// it out for any amount of time. | ||
unsafe { &*self.0.load(Ordering::SeqCst) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//! Throughout the compiler tree, there are several places which want to have | ||
//! access to state or queries while being inside crates that are dependencies | ||
//! of librustc. To facilitate this, we have the | ||
//! `rustc_data_structures::AtomicRef` type, which allows us to setup a global | ||
//! static which can then be set in this file at program startup. | ||
//! | ||
//! See `SPAN_DEBUG` for an example of how to set things up. | ||
//! | ||
//! The functions in this file should fall back to the default set in their | ||
//! origin crate when the `TyCtxt` is not present in TLS. | ||
use rustc::ty::tls; | ||
use rustc_errors::{Diagnostic, TRACK_DIAGNOSTICS}; | ||
use std::fmt; | ||
use syntax_pos; | ||
|
||
/// This is a callback from libsyntax as it cannot access the implicit state | ||
/// in librustc otherwise. | ||
fn span_debug(span: syntax_pos::Span, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
tls::with_opt(|tcx| { | ||
if let Some(tcx) = tcx { | ||
write!(f, "{}", tcx.sess.source_map().span_to_string(span)) | ||
} else { | ||
syntax_pos::default_span_debug(span, f) | ||
} | ||
}) | ||
} | ||
|
||
/// This is a callback from libsyntax as it cannot access the implicit state | ||
/// in librustc otherwise. It is used to when diagnostic messages are | ||
/// emitted and stores them in the current query, if there is one. | ||
fn track_diagnostic(diagnostic: &Diagnostic) { | ||
tls::with_context_opt(|icx| { | ||
if let Some(icx) = icx { | ||
if let Some(ref diagnostics) = icx.diagnostics { | ||
let mut diagnostics = diagnostics.lock(); | ||
diagnostics.extend(Some(diagnostic.clone())); | ||
} | ||
} | ||
}) | ||
} | ||
|
||
/// Sets up the callbacks in prior crates which we want to refer to the | ||
/// TyCtxt in. | ||
pub fn setup_callbacks() { | ||
syntax_pos::SPAN_DEBUG.swap(&(span_debug as fn(_, &mut fmt::Formatter<'_>) -> _)); | ||
TRACK_DIAGNOSTICS.swap(&(track_diagnostic as fn(&_))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
#[cfg(unix)] | ||
extern crate libc; | ||
|
||
mod callbacks; | ||
pub mod interface; | ||
mod passes; | ||
mod proc_macro_decls; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters