Skip to content

Commit 6449917

Browse files
bors[bot]xFrednet
andauthored
Merge #83
83: Minor cleanup around the project r=Niki4tap a=xFrednet This is a collection of smaller things which have been at the back of my mind. Each commit addresses on TODO. For the ident commit, it's noteworthy that I only changed usages in the items module. It's probably best to review this on a commit by commit basis. Co-authored-by: xFrednet <[email protected]>
2 parents 9b79e50 + 04e410e commit 6449917

23 files changed

+140
-83
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

marker_api/src/ast/common/ast_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<'ast> AstPathSegment<'ast> {
5454
}
5555

5656
impl<'ast> AstPathSegment<'ast> {
57-
pub fn ident(&self) -> String {
57+
pub fn ident(&self) -> &str {
5858
with_cx(self, |cx| cx.symbol_str(self.ident))
5959
}
6060

marker_api/src/ast/common/callable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<'ast> Parameter<'ast> {
7575
impl<'ast> Parameter<'ast> {
7676
// Function items actually use patterns and not names. Patterns are not yet
7777
// implemented though. A name should be good enough for now.
78-
pub fn name(&self) -> Option<String> {
78+
pub fn name(&self) -> Option<&str> {
7979
self.name.get().map(|sym| with_cx(self, |cx| cx.symbol_str(*sym)))
8080
}
8181

marker_api/src/ast/common/span.rs

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use std::path::PathBuf;
1+
use std::{marker::PhantomData, path::PathBuf};
22

33
use crate::context::with_cx;
44

5-
use super::{Applicability, AstPath, ItemId, SpanId};
5+
use super::{Applicability, AstPath, ItemId, SpanId, SymbolId};
66

77
#[repr(C)]
88
#[doc(hidden)]
9-
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
9+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1010
#[cfg_attr(feature = "driver-api", visibility::make(pub))]
1111
#[allow(clippy::exhaustive_enums)]
1212
enum SpanSource<'ast> {
@@ -15,7 +15,7 @@ enum SpanSource<'ast> {
1515
}
1616

1717
#[repr(C)]
18-
#[derive(Clone)]
18+
#[derive(Debug, Clone)]
1919
pub struct Span<'ast> {
2020
source: SpanSource<'ast>,
2121
/// The start marks the first byte in the [`SpanSource`] that is included in this
@@ -147,3 +147,69 @@ impl From<SpanId> for SpanOwner {
147147
Self::SpecificSpan(id)
148148
}
149149
}
150+
151+
pub struct Ident<'ast> {
152+
_lifetime: PhantomData<&'ast ()>,
153+
sym: SymbolId,
154+
span: SpanId,
155+
}
156+
157+
impl<'ast> Ident<'ast> {
158+
pub fn name(&self) -> &str {
159+
with_cx(self, |cx| cx.symbol_str(self.sym))
160+
}
161+
162+
pub fn span(&self) -> &Span<'ast> {
163+
with_cx(self, |cx| cx.get_span(self.span))
164+
}
165+
}
166+
167+
#[cfg(feature = "driver-api")]
168+
impl<'ast> Ident<'ast> {
169+
pub fn new(sym: SymbolId, span: SpanId) -> Self {
170+
Self {
171+
_lifetime: PhantomData,
172+
sym,
173+
span,
174+
}
175+
}
176+
}
177+
178+
impl<'ast> std::fmt::Debug for Ident<'ast> {
179+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
180+
f.debug_struct("Ident")
181+
.field("name", &self.name())
182+
.field("span", &self.span())
183+
.finish()
184+
}
185+
}
186+
187+
macro_rules! impl_ident_eq_for {
188+
($ty:ty) => {
189+
impl<'ast> PartialEq<$ty> for Ident<'ast> {
190+
fn eq(&self, other: &$ty) -> bool {
191+
self.name().eq(other)
192+
}
193+
}
194+
impl<'ast> PartialEq<Ident<'ast>> for $ty {
195+
fn eq(&self, other: &Ident<'ast>) -> bool {
196+
other.name().eq(self)
197+
}
198+
}
199+
};
200+
($($ty:ty),+) => {
201+
$(
202+
impl_ident_eq_for!($ty);
203+
)+
204+
};
205+
}
206+
207+
use impl_ident_eq_for;
208+
209+
impl_ident_eq_for!(
210+
str,
211+
String,
212+
std::ffi::OsStr,
213+
std::ffi::OsString,
214+
std::borrow::Cow<'_, str>
215+
);

marker_api/src/ast/expr/str_lit_expr.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,18 @@ impl<'ast> StrLitExpr<'ast> {
2121
/// This returns the UTF-8 string value of the string, if possible. Normal
2222
/// and raw strings in Rust are required to be UTF-8. Byte strings will be
2323
/// converted to UTF-8 if possible, otherwise `None` will be returned
24-
pub fn str_value(&self) -> Option<String> {
24+
pub fn str_value(&self) -> Option<&str> {
2525
match &self.str_data {
2626
StrKindWithData::Str(sym) | StrKindWithData::Raw(sym) => Some(with_cx(self, |cx| cx.symbol_str(*sym))),
27-
StrKindWithData::Byte(bytes) => std::str::from_utf8(bytes.get()).map(ToString::to_string).ok(),
27+
StrKindWithData::Byte(bytes) => std::str::from_utf8(bytes.get()).ok(),
2828
}
2929
}
3030

3131
/// Returns the value of the string as bytes.
32-
pub fn byte_value(&self) -> Box<[u8]> {
33-
// The context currently returns symbols as a `String` which makes it
34-
// difficult to return the bytes as a byte slice. This could be improved
35-
// if the `symbol_str` returns `&'ast str`. But that's a larger todo for
36-
// another time :)
37-
fn box_slice(bytes: &[u8]) -> Box<[u8]> {
38-
let mut vec = Vec::with_capacity(bytes.len());
39-
vec.extend_from_slice(bytes);
40-
vec.into_boxed_slice()
41-
}
32+
pub fn byte_value(&self) -> &[u8] {
4233
match &self.str_data {
43-
StrKindWithData::Str(sym) | StrKindWithData::Raw(sym) => {
44-
let str_value = with_cx(self, |cx| cx.symbol_str(*sym));
45-
box_slice(str_value.as_bytes())
46-
},
47-
StrKindWithData::Byte(bytes) => box_slice(bytes.get()),
34+
StrKindWithData::Str(sym) | StrKindWithData::Raw(sym) => with_cx(self, |cx| cx.symbol_str(*sym)).as_bytes(),
35+
StrKindWithData::Byte(bytes) => bytes.get(),
4836
}
4937
}
5038
}

marker_api/src/ast/generic/arg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'ast> Lifetime<'ast> {
6767
}
6868

6969
/// Note that the `'static` lifetime is not a label and will therefore return [`None`]
70-
pub fn label(&self) -> Option<String> {
70+
pub fn label(&self) -> Option<&str> {
7171
match self.kind {
7272
LifetimeKind::Label(sym, _) => Some(with_cx(self, |cx| cx.symbol_str(sym))),
7373
_ => None,
@@ -121,7 +121,7 @@ impl<'ast> BindingGenericArg<'ast> {
121121
/// ```
122122
///
123123
/// Would return `Item` as the identifier.
124-
pub fn ident(&self) -> String {
124+
pub fn ident(&self) -> &str {
125125
with_cx(self, |cx| cx.symbol_str(self.ident))
126126
}
127127

marker_api/src/ast/generic/param.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<'ast> TyParam<'ast> {
9090
self.id
9191
}
9292

93-
pub fn name(&self) -> String {
93+
pub fn name(&self) -> &str {
9494
with_cx(self, |cx| cx.symbol_str(self.name))
9595
}
9696
}
@@ -140,7 +140,7 @@ impl<'ast> LifetimeParam<'ast> {
140140
self.id
141141
}
142142

143-
pub fn name(&self) -> String {
143+
pub fn name(&self) -> &str {
144144
with_cx(self, |cx| cx.symbol_str(self.name))
145145
}
146146
}

marker_api/src/ast/item.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{fmt::Debug, marker::PhantomData};
22

33
use super::expr::ExprKind;
4-
use super::{ItemId, Span, SymbolId};
4+
use super::{Ident, ItemId, Span};
55

66
// Item implementations
77
mod extern_crate_item;
@@ -10,8 +10,8 @@ mod mod_item;
1010
pub use mod_item::ModItem;
1111
mod static_item;
1212
pub use self::static_item::StaticItem;
13-
mod use_decl_item;
14-
pub use self::use_decl_item::*;
13+
mod use_item;
14+
pub use self::use_item::*;
1515
mod const_item;
1616
pub use self::const_item::ConstItem;
1717
mod fn_item;
@@ -42,7 +42,7 @@ pub trait ItemData<'ast>: Debug {
4242
fn visibility(&self) -> &Visibility<'ast>;
4343

4444
/// This function can return [`None`] if the item was generated and has no real name
45-
fn name(&self) -> Option<String>;
45+
fn ident(&self) -> Option<&Ident<'ast>>;
4646

4747
/// This returns this [`ItemData`] instance as a [`ItemKind`]. This can be useful for
4848
/// functions that take [`ItemKind`] as a parameter. For general function calls it's better
@@ -76,7 +76,7 @@ impl<'ast> ItemKind<'ast> {
7676
impl_item_type_fn!(ItemKind: id() -> ItemId);
7777
impl_item_type_fn!(ItemKind: span() -> &Span<'ast>);
7878
impl_item_type_fn!(ItemKind: visibility() -> &Visibility<'ast>);
79-
impl_item_type_fn!(ItemKind: name() -> Option<String>);
79+
impl_item_type_fn!(ItemKind: ident() -> Option<&Ident<'ast>>);
8080
impl_item_type_fn!(ItemKind: attrs() -> ());
8181
}
8282

@@ -92,7 +92,7 @@ impl<'ast> AssocItemKind<'ast> {
9292
impl_item_type_fn!(AssocItemKind: id() -> ItemId);
9393
impl_item_type_fn!(AssocItemKind: span() -> &Span<'ast>);
9494
impl_item_type_fn!(AssocItemKind: visibility() -> &Visibility<'ast>);
95-
impl_item_type_fn!(AssocItemKind: name() -> Option<String>);
95+
impl_item_type_fn!(AssocItemKind: ident() -> Option<&Ident<'ast>>);
9696
impl_item_type_fn!(AssocItemKind: attrs() -> ());
9797
impl_item_type_fn!(AssocItemKind: as_item() -> ItemKind<'ast>);
9898
// FIXME: Potentially add a field to the items to optionally store the owner id
@@ -132,7 +132,7 @@ impl<'ast> ExternItemKind<'ast> {
132132
impl_item_type_fn!(ExternItemKind: id() -> ItemId);
133133
impl_item_type_fn!(ExternItemKind: span() -> &Span<'ast>);
134134
impl_item_type_fn!(ExternItemKind: visibility() -> &Visibility<'ast>);
135-
impl_item_type_fn!(ExternItemKind: name() -> Option<String>);
135+
impl_item_type_fn!(ExternItemKind: ident() -> Option<&Ident<'ast>>);
136136
impl_item_type_fn!(ExternItemKind: attrs() -> ());
137137
impl_item_type_fn!(ExternItemKind: as_item() -> ItemKind<'ast>);
138138
}
@@ -194,7 +194,7 @@ use impl_item_type_fn;
194194
struct CommonItemData<'ast> {
195195
id: ItemId,
196196
vis: Visibility<'ast>,
197-
name: SymbolId,
197+
ident: Ident<'ast>,
198198
}
199199

200200
macro_rules! impl_item_data {
@@ -212,8 +212,8 @@ macro_rules! impl_item_data {
212212
&self.data.vis
213213
}
214214

215-
fn name(&self) -> Option<String> {
216-
Some($crate::context::with_cx(self, |cx| cx.symbol_str(self.data.name)))
215+
fn ident(&self) -> Option<&crate::ast::Ident<'ast>> {
216+
Some(&self.data.ident)
217217
}
218218

219219
fn as_item(&'ast self) -> crate::ast::item::ItemKind<'ast> {
@@ -231,11 +231,11 @@ use impl_item_data;
231231

232232
#[cfg(feature = "driver-api")]
233233
impl<'ast> CommonItemData<'ast> {
234-
pub fn new(id: ItemId, name: SymbolId) -> Self {
234+
pub fn new(id: ItemId, ident: Ident<'ast>) -> Self {
235235
Self {
236236
id,
237237
vis: Visibility::new(id),
238-
name,
238+
ident,
239239
}
240240
}
241241
}

marker_api/src/ast/item/adt_item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub struct EnumVariant<'ast> {
100100
}
101101

102102
impl<'ast> EnumVariant<'ast> {
103-
pub fn ident(&self) -> String {
103+
pub fn ident(&self) -> &str {
104104
with_cx(self, |cx| cx.symbol_str(self.ident))
105105
}
106106

@@ -270,7 +270,7 @@ impl<'ast> Field<'ast> {
270270
&self.vis
271271
}
272272

273-
pub fn ident(&self) -> String {
273+
pub fn ident(&self) -> &str {
274274
with_cx(self, |cx| cx.symbol_str(self.ident))
275275
}
276276

marker_api/src/ast/item/extern_crate_item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ super::impl_item_data!(ExternCrateItem, ExternCrate);
2626

2727
impl<'ast> ExternCrateItem<'ast> {
2828
/// This will return the original name of external crate. This will only differ
29-
/// with [`ItemData::get_name`](`super::ItemData::name`) if the user has
29+
/// with [`ItemData::ident`](`super::ItemData::ident`) if the user has
3030
/// declared an alias with `as`.
3131
///
3232
/// In most cases, you want to use this over the `get_name()` function.
33-
pub fn crate_name(&self) -> String {
33+
pub fn crate_name(&self) -> &str {
3434
with_cx(self, |cx| cx.symbol_str(self.crate_name))
3535
}
3636
}

marker_api/src/ast/item/unstable_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct UnstableItem<'ast> {
1515
super::impl_item_data!(UnstableItem, Unstable);
1616

1717
impl<'ast> UnstableItem<'ast> {
18-
pub fn feature(&self) -> Option<String> {
18+
pub fn feature(&self) -> Option<&str> {
1919
self.feature
2020
.get()
2121
.map(|feature| with_cx(self, |cx| cx.symbol_str(*feature)))

marker_api/src/ast/pat/ident_pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct IdentPat<'ast> {
1818
}
1919

2020
impl<'ast> IdentPat<'ast> {
21-
pub fn name(&self) -> String {
21+
pub fn name(&self) -> &str {
2222
with_cx(self, |cx| cx.symbol_str(self.name))
2323
}
2424

marker_api/src/ast/pat/struct_pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<'ast> StructFieldPat<'ast> {
6969
with_cx(self, |cx| cx.get_span(self.span))
7070
}
7171

72-
pub fn ident(&self) -> String {
72+
pub fn ident(&self) -> &str {
7373
with_cx(self, |cx| cx.symbol_str(self.ident))
7474
}
7575

marker_api/src/ast/ty/relative_ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<'ast> RelativeTy<'ast> {
1818
self.ty
1919
}
2020

21-
pub fn name(&self) -> String {
21+
pub fn name(&self) -> &str {
2222
with_cx(self, |cx| cx.symbol_str(self.name))
2323
}
2424
}

marker_api/src/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<'ast> AstContext<'ast> {
143143
self.driver.call_get_span(&span_owner.into())
144144
}
145145

146-
pub(crate) fn symbol_str(&self, sym: SymbolId) -> String {
146+
pub(crate) fn symbol_str(&self, sym: SymbolId) -> &'ast str {
147147
self.driver.call_symbol_str(sym)
148148
}
149149
}
@@ -198,8 +198,8 @@ impl<'ast> DriverCallbacks<'ast> {
198198
let result: Option<ffi::Str> = (self.span_snippet)(self.driver_context, span).into();
199199
result.map(|x| x.to_string())
200200
}
201-
fn call_symbol_str(&self, sym: SymbolId) -> String {
202-
(self.symbol_str)(self.driver_context, sym).to_string()
201+
fn call_symbol_str(&self, sym: SymbolId) -> &'ast str {
202+
(self.symbol_str)(self.driver_context, sym).get()
203203
}
204204
pub fn call_body(&self, id: BodyId) -> &'ast Body<'ast> {
205205
(self.body)(self.driver_context, id)

marker_api/src/ffi.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ impl<'a> From<&'a str> for Str<'a> {
3030
}
3131
}
3232

33+
impl<'a> Str<'a> {
34+
pub fn get(&self) -> &'a str {
35+
unsafe {
36+
let data = slice::from_raw_parts(self.data, self.len);
37+
std::str::from_utf8_unchecked(data)
38+
}
39+
}
40+
}
41+
3342
impl<'a> From<&Str<'a>> for &'a str {
3443
fn from(src: &Str<'a>) -> Self {
3544
unsafe {

0 commit comments

Comments
 (0)