Skip to content

Commit 9a5fe9c

Browse files
xFrednetVeetaha
andcommitted
Chore: Address PR review
Co-authored-by: Veetaha <[email protected]>
1 parent 7fffe76 commit 9a5fe9c

File tree

5 files changed

+80
-46
lines changed

5 files changed

+80
-46
lines changed

marker_adapter/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ extern "C" fn span_snippet<'ast>(data: &'ast (), span: &Span<'ast>) -> ffi::FfiO
9595
unsafe { as_driver_cx(data) }.span_snippet(span).map(Into::into).into()
9696
}
9797

98+
// False positive because `SpanSource` is non-exhaustive
99+
#[allow(improper_ctypes_definitions)]
98100
extern "C" fn span_source<'ast>(data: &'ast (), span: &Span<'_>) -> SpanSource<'ast> {
99101
unsafe { as_driver_cx(data) }.span_source(span)
100102
}

marker_api/src/ast/common/id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ new_id! {
7070
}
7171

7272
new_id! {
73-
/// This ID uniquely identifies a user defined type during linting.
73+
/// This ID uniquely identifies a macro during linting.
7474
pub MacroId: u64
7575
}
7676

marker_api/src/ast/common/span.rs

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl SpanPos {
9797
/// ex3b!(2);
9898
/// ```
9999
///
100-
/// In this example `ex3b!(2)` expands to `ex3a(2, 3)` which in turn expands to
100+
/// In this example `ex3b!(2)` expands to `ex3a!(2, 3)` which in turn expands to
101101
/// `2 + 3`. This expansion has three layers, first the root, which contains the
102102
/// `ex3b!(2)` call. The next layer is the `ex3a!(2, 3)` call. The binary expression
103103
/// comes from the third layer, the number 3 from the second, and the number 2 from
@@ -122,8 +122,8 @@ impl SpanPos {
122122
///
123123
/// This example expands `ex4a` into a new `ex4b` macro, which in turn expands
124124
/// into a `4 + 4` expression. The [`Span`] of the binary expression has three
125-
/// layers. First the root layer calling the `ex4b` macro, this would then
126-
/// expand to the `4 + 4` expression, which actually comes from the `ex4a` expansion.
125+
/// layers. First the root layer calling the `ex4b` macro, which calls the `ex4a`
126+
/// macro, which inturn expands into the `4 + 4` expression.
127127
///
128128
/// ### Proc Macros
129129
///
@@ -142,6 +142,7 @@ pub struct ExpnInfo<'ast> {
142142
}
143143

144144
impl<'ast> ExpnInfo<'ast> {
145+
/// This returns [`Some`] if this expansion comes from another expansion.
145146
#[must_use]
146147
pub fn parent(&self) -> Option<&ExpnInfo<'ast>> {
147148
with_cx(self, |cx| cx.span_expn_info(self.parent))
@@ -204,7 +205,7 @@ pub struct Span<'ast> {
204205
source_id: SpanSrcId,
205206
/// This information could also be retrieved, by requesting the [`ExpnInfo`]
206207
/// of this span. However, from looking at Clippy and rustc lints, it looks
207-
/// like the main interested is, if this comes from a macro expansion, not from
208+
/// like the main interest is, if this comes from a macro expansion, not from
208209
/// which one. Having this boolean flag will be sufficient to answer this simple
209210
/// question and will save on extra [`SpanSrcId`] mappings.
210211
from_expansion: bool,
@@ -226,8 +227,8 @@ impl<'ast> std::fmt::Debug for Span<'ast> {
226227
SpanSource::File(file) => format!(
227228
"{}:{} - {}",
228229
file.file(),
229-
fmt_pos(file.to_file_pos(self.start)),
230-
fmt_pos(file.to_file_pos(self.end))
230+
fmt_pos(file.try_to_file_pos(self.start)),
231+
fmt_pos(file.try_to_file_pos(self.end))
231232
),
232233
SpanSource::Macro(expn) => format!("[Inside Macro] {:#?}", expn.call_site()),
233234
};
@@ -271,8 +272,12 @@ impl<'ast> Span<'ast> {
271272
///
272273
/// If you're planning to use this snippet in a suggestion, consider using
273274
/// [`snippet_with_applicability`](Self::snippet_with_applicability) instead.
274-
pub fn snippet_or(&self, default: &str) -> String {
275-
self.snippet().unwrap_or(default).to_string()
275+
pub fn snippet_or<'a, 'b>(&self, default: &'a str) -> &'b str
276+
where
277+
'a: 'b,
278+
'ast: 'b,
279+
{
280+
self.snippet().unwrap_or(default)
276281
}
277282

278283
/// Adjusts the given [`Applicability`] according to the context and returns the
@@ -288,26 +293,39 @@ impl<'ast> Span<'ast> {
288293
/// your suggestion should have, and then use it with this snippet function
289294
/// to adjust it accordingly. The applicability is then used to submit the
290295
/// suggestion to the driver.
291-
pub fn snippet_with_applicability(&self, placeholder: &str, applicability: &mut Applicability) -> String {
296+
///
297+
/// Here is an example, for constructing a string with two expressions `a` and `b`:
298+
///
299+
/// ```rust,ignore
300+
/// let mut app = Applicability::MachineApplicable;
301+
/// let sugg = format!(
302+
/// "{}..{}",
303+
/// a.span().snippet_with_applicability("<expr-a>", &mut app),
304+
/// b.span().snippet_with_applicability("<expr-b>", &mut app),
305+
/// );
306+
/// ```
307+
pub fn snippet_with_applicability<'a, 'b>(&self, placeholder: &'a str, applicability: &mut Applicability) -> &'b str
308+
where
309+
'a: 'b,
310+
'ast: 'b,
311+
{
292312
if *applicability != Applicability::Unspecified && self.is_from_expansion() {
293313
*applicability = Applicability::MaybeIncorrect;
294314
}
295-
self.snippet()
296-
.unwrap_or_else(|| {
297-
if matches!(
298-
*applicability,
299-
Applicability::MachineApplicable | Applicability::MaybeIncorrect
300-
) {
301-
*applicability = Applicability::HasPlaceholders;
302-
}
303-
placeholder
304-
})
305-
.to_string()
315+
self.snippet().unwrap_or_else(|| {
316+
if matches!(
317+
*applicability,
318+
Applicability::MachineApplicable | Applicability::MaybeIncorrect
319+
) {
320+
*applicability = Applicability::HasPlaceholders;
321+
}
322+
placeholder
323+
})
306324
}
307325

308326
/// Returns the length of the this [`Span`] in bytes.
309327
pub fn len(&self) -> usize {
310-
(self.start.0 - self.end.0)
328+
(self.end.0 - self.start.0)
311329
.try_into()
312330
.expect("Marker is not compiled for usize::BITs < 32")
313331
}
@@ -388,7 +406,7 @@ impl<'ast> Span<'ast> {
388406

389407
#[repr(C)]
390408
#[derive(Debug)]
391-
#[allow(clippy::exhaustive_enums)]
409+
#[non_exhaustive]
392410
pub enum SpanSource<'ast> {
393411
File(&'ast FileInfo<'ast>),
394412
Macro(&'ast ExpnInfo<'ast>),
@@ -406,11 +424,22 @@ impl<'ast> FileInfo<'ast> {
406424
self.file.get()
407425
}
408426

409-
/// Maps the given [`SpanPos`] to a [`FilePos`]. The [`SpanPos`] has to correspond
410-
/// to a position that belongs to this [`FileInfo`].
411-
pub fn to_file_pos(&self, span_pos: SpanPos) -> Option<FilePos> {
427+
/// Tries to map the given [`SpanPos`] to a [`FilePos`]. It will return [`None`]
428+
/// if the given [`FilePos`] belongs to a different [`FileInfo`].
429+
pub fn try_to_file_pos(&self, span_pos: SpanPos) -> Option<FilePos> {
412430
with_cx(self, |cx| cx.span_pos_to_file_loc(self, span_pos))
413431
}
432+
433+
/// Map the given [`SpanPos`] to a [`FilePos`]. This will panic, if the
434+
/// [`SpanPos`] doesn't belong to this [`FileInfo`]
435+
pub fn to_file_pos(&self, span_pos: SpanPos) -> FilePos {
436+
self.try_to_file_pos(span_pos).unwrap_or_else(|| {
437+
panic!(
438+
"the given span position `{span_pos:#?}` is out of range of the file `{}`",
439+
self.file.get()
440+
)
441+
})
442+
}
414443
}
415444

416445
#[cfg(feature = "driver-api")]
@@ -428,7 +457,7 @@ impl<'ast> FileInfo<'ast> {
428457
}
429458
}
430459

431-
/// A locating inside a file.
460+
/// A location inside a file.
432461
///
433462
/// [`SpanPos`] instances belonging to files can be mapped to [`FilePos`] with
434463
/// the [`FileInfo`] from the [`SpanSource`] of the [`Span`] they belong to. See:

marker_rustc_driver/src/context/storage.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ use std::marker::PhantomData;
33
use bumpalo::Bump;
44

55
pub struct Storage<'ast> {
6-
/// The `'ast` lifetime is bound by the `buffer` field. Having it as a parameter
7-
/// makes it easier to declare it in the return of functions
6+
/// The `'ast` lifetime is the lifetime of the `buffer` field.
7+
///
8+
/// Having it as an explicit parameter allows us to later add fields to cache
9+
/// values.
810
_lifetime: PhantomData<&'ast ()>,
911
buffer: Bump,
1012
}

marker_rustc_driver/src/conversion/marker/common/span.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,25 @@ impl<'ast, 'tcx> MarkerConverterInner<'ast, 'tcx> {
2323

2424
pub fn to_span_source(&self, rust_span: rustc_span::Span) -> SpanSource<'ast> {
2525
let ctxt = rust_span.ctxt();
26-
if ctxt.is_root() {
27-
let src_file = self.rustc_cx.sess.source_map().lookup_source_file(rust_span.lo());
28-
let name = match &src_file.name {
29-
rustc_span::FileName::Real(
30-
rustc_span::RealFileName::LocalPath(file_path)
31-
| rustc_span::RealFileName::Remapped {
32-
virtual_name: file_path,
33-
..
34-
},
35-
) => file_path.to_string_lossy().to_string(),
36-
_ => {
37-
format!("MarkerConverter::to_span_source(): Unexpected file name: {rust_span:#?} -> {src_file:#?}")
38-
},
39-
};
40-
SpanSource::File(self.alloc(FileInfo::new(self.storage.alloc_str(&name), self.to_span_src_id(ctxt))))
41-
} else {
42-
SpanSource::Macro(self.alloc(self.to_expn_info(&ctxt.outer_expn_data())))
26+
27+
if !ctxt.is_root() {
28+
return SpanSource::Macro(self.alloc(self.to_expn_info(&ctxt.outer_expn_data())));
4329
}
30+
31+
let src_file = self.rustc_cx.sess.source_map().lookup_source_file(rust_span.lo());
32+
let name = match &src_file.name {
33+
rustc_span::FileName::Real(
34+
rustc_span::RealFileName::LocalPath(file_path)
35+
| rustc_span::RealFileName::Remapped {
36+
virtual_name: file_path,
37+
..
38+
},
39+
) => file_path.to_string_lossy().into_owned(),
40+
_ => {
41+
format!("MarkerConverter::to_span_source(): Unexpected file name: {rust_span:#?} -> {src_file:#?}")
42+
},
43+
};
44+
SpanSource::File(self.alloc(FileInfo::new(self.storage.alloc_str(&name), self.to_span_src_id(ctxt))))
4445
}
4546

4647
pub fn try_to_expn_info(&self, id: rustc_span::ExpnId) -> Option<&'ast ExpnInfo<'ast>> {

0 commit comments

Comments
 (0)