Skip to content

Commit 7c8bf4b

Browse files
authored
Merge pull request dtolnay#150 from dtolnay/marker
Remove Send and Sync from SourceFile
2 parents 0d9991c + e7b17ec commit 7c8bf4b

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

src/lib.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,20 @@ pub use imp::FileName;
220220
/// This type is semver exempt and not exposed by default.
221221
#[cfg(procmacro2_semver_exempt)]
222222
#[derive(Clone, PartialEq, Eq)]
223-
pub struct SourceFile(imp::SourceFile);
223+
pub struct SourceFile {
224+
inner: imp::SourceFile,
225+
_marker: marker::PhantomData<Rc<()>>,
226+
}
224227

225228
#[cfg(procmacro2_semver_exempt)]
226229
impl SourceFile {
230+
fn _new(inner: imp::SourceFile) -> Self {
231+
SourceFile {
232+
inner: inner,
233+
_marker: marker::PhantomData,
234+
}
235+
}
236+
227237
/// Get the path to this source file.
228238
///
229239
/// ### Note
@@ -238,27 +248,27 @@ impl SourceFile {
238248
///
239249
/// [`is_real`]: #method.is_real
240250
pub fn path(&self) -> &FileName {
241-
self.0.path()
251+
self.inner.path()
242252
}
243253

244254
/// Returns `true` if this source file is a real source file, and not
245255
/// generated by an external macro's expansion.
246256
pub fn is_real(&self) -> bool {
247-
self.0.is_real()
257+
self.inner.is_real()
248258
}
249259
}
250260

251261
#[cfg(procmacro2_semver_exempt)]
252262
impl AsRef<FileName> for SourceFile {
253263
fn as_ref(&self) -> &FileName {
254-
self.0.path()
264+
self.inner.path()
255265
}
256266
}
257267

258268
#[cfg(procmacro2_semver_exempt)]
259269
impl fmt::Debug for SourceFile {
260270
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
261-
self.0.fmt(f)
271+
self.inner.fmt(f)
262272
}
263273
}
264274

@@ -344,7 +354,7 @@ impl Span {
344354
/// This method is semver exempt and not exposed by default.
345355
#[cfg(procmacro2_semver_exempt)]
346356
pub fn source_file(&self) -> SourceFile {
347-
SourceFile(self.inner.source_file())
357+
SourceFile::_new(self.inner.source_file())
348358
}
349359

350360
/// Get the starting line/column in the source file for this span.

tests/marker.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
extern crate proc_macro2;
2+
3+
use proc_macro2::*;
4+
5+
macro_rules! assert_impl {
6+
($ty:ident is $($marker:ident) and +) => {
7+
#[test]
8+
#[allow(non_snake_case)]
9+
fn $ty() {
10+
fn assert_implemented<T: $($marker +)+>() {}
11+
assert_implemented::<$ty>();
12+
}
13+
};
14+
15+
($ty:ident is not $($marker:ident) or +) => {
16+
#[test]
17+
#[allow(non_snake_case)]
18+
fn $ty() {
19+
$(
20+
{
21+
// Implemented for types that implement $marker.
22+
trait IsNotImplemented {
23+
fn assert_not_implemented() {}
24+
}
25+
impl<T: $marker> IsNotImplemented for T {}
26+
27+
// Implemented for the type being tested.
28+
trait IsImplemented {
29+
fn assert_not_implemented() {}
30+
}
31+
impl IsImplemented for $ty {}
32+
33+
// If $ty does not implement $marker, there is no ambiguity
34+
// in the following trait method call.
35+
<$ty>::assert_not_implemented();
36+
}
37+
)+
38+
}
39+
};
40+
}
41+
42+
assert_impl!(Delimiter is Send and Sync);
43+
assert_impl!(Spacing is Send and Sync);
44+
45+
assert_impl!(Group is not Send or Sync);
46+
assert_impl!(Ident is not Send or Sync);
47+
assert_impl!(LexError is not Send or Sync);
48+
assert_impl!(Literal is not Send or Sync);
49+
assert_impl!(Punct is not Send or Sync);
50+
assert_impl!(Span is not Send or Sync);
51+
assert_impl!(TokenStream is not Send or Sync);
52+
assert_impl!(TokenTree is not Send or Sync);
53+
54+
#[cfg(procmacro2_semver_exempt)]
55+
mod semver_exempt {
56+
use super::*;
57+
58+
assert_impl!(LineColumn is Send and Sync);
59+
60+
assert_impl!(SourceFile is not Send or Sync);
61+
}

0 commit comments

Comments
 (0)