Skip to content

Commit f399207

Browse files
bors[bot]xFrednet
andauthored
Merge #58
58: Add more syntactic types, update rustc impl and general clean up r=xFrednet a=xFrednet Not a long description, as I feel like nobody is reading these. If you do, please leave me a comment, and I'll go into more detail. I'd also be very happy!!! Reviews are welcome, but I'll merge it without, if there has been no activity by the next step. --- Closes #56 Closes #38 Co-authored-by: xFrednet <[email protected]>
2 parents dc6e411 + 60a8d72 commit f399207

28 files changed

+617
-481
lines changed

linter_adapter/src/context.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use linter_api::{
2-
ast::{Span, SpanOwner, SymbolId},
2+
ast::{item::ItemKind, ItemId, Span, SpanOwner, SymbolId},
33
context::DriverCallbacks,
4-
ffi,
4+
ffi::{self, FfiOption},
55
lint::Lint,
66
};
77

@@ -32,13 +32,20 @@ impl<'ast> DriverContextWrapper<'ast> {
3232
DriverCallbacks {
3333
driver_context: unsafe { &*(self as *const DriverContextWrapper).cast::<()>() },
3434
emit_lint,
35+
item,
3536
get_span,
3637
span_snippet,
3738
symbol_str,
3839
}
3940
}
4041
}
4142

43+
#[allow(improper_ctypes_definitions, reason = "fp because `ItemKind` is non-exhaustive")]
44+
extern "C" fn item<'ast>(data: &(), id: ItemId) -> FfiOption<ItemKind<'ast>> {
45+
let wrapper = unsafe { &*(data as *const ()).cast::<DriverContextWrapper>() };
46+
wrapper.driver_cx.item(id).into()
47+
}
48+
4249
extern "C" fn emit_lint<'ast>(data: &(), lint: &'static Lint, msg: ffi::Str, span: &Span<'ast>) {
4350
let wrapper = unsafe { &*(data as *const ()).cast::<DriverContextWrapper>() };
4451
wrapper.driver_cx.emit_lint(lint, (&msg).into(), span);
@@ -60,6 +67,7 @@ extern "C" fn symbol_str<'ast>(data: &(), sym: SymbolId) -> ffi::Str<'ast> {
6067
}
6168

6269
pub trait DriverContext<'ast> {
70+
fn item(&'ast self, id: ItemId) -> Option<ItemKind<'ast>>;
6371
fn emit_lint(&'ast self, lint: &'static Lint, msg: &str, span: &Span<'ast>);
6472
fn get_span(&'ast self, owner: &SpanOwner) -> &'ast Span<'ast>;
6573
fn span_snippet(&'ast self, span: &Span) -> Option<&'ast str>;

linter_api/src/ast/common.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,6 @@ pub enum Applicability {
4343
Unspecified,
4444
}
4545

46-
/// Used to indicate the safety. [`Safety::Default`] is the default safe rust mode.
47-
#[non_exhaustive]
48-
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
49-
pub enum Safety {
50-
Default,
51-
Unsafe,
52-
}
53-
54-
#[non_exhaustive]
55-
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
56-
pub enum Constness {
57-
Default,
58-
Const,
59-
}
60-
61-
#[non_exhaustive]
62-
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
63-
pub enum Asyncness {
64-
Default,
65-
Async,
66-
}
67-
6846
#[non_exhaustive]
6947
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
7048
pub enum Abi {
@@ -78,14 +56,6 @@ pub enum Abi {
7856
Other,
7957
}
8058

81-
#[repr(C)]
82-
#[non_exhaustive]
83-
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
84-
pub enum Mutability {
85-
Mut,
86-
Not,
87-
}
88-
8959
pub struct Spanned<'ast, T> {
9060
pub node: T,
9161
pub span: &'ast Span<'ast>,

linter_api/src/ast/common/callable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl<'ast> Parameter<'ast> {
8080
}
8181

8282
pub fn ty(&self) -> Option<TyKind<'ast>> {
83-
self.ty.get().copied()
83+
self.ty.copy()
8484
}
8585

8686
pub fn span(&self) -> Option<&Span<'ast>> {

linter_api/src/ast/generic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ pub enum GenericArgKind<'ast> {
8787
// FIXME: Potentualy add a specific `Arg` wrapper for the `Lifetime` and `Type`
8888
}
8989

90-
/// This represents the generic parameters of a generic item.
90+
/// This represents the generic parameters of a generic item. The bounds applied
91+
/// to parameters as the declaration are stored as clauses in this struct.
9192
///
9293
/// ```
9394
/// # use std::fmt::Debug;

linter_api/src/ast/generic/param.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use std::marker::PhantomData;
2+
13
use crate::ast::{GenericId, Span, SpanId, SymbolId};
24
use crate::context::with_cx;
3-
use crate::ffi::{FfiOption, FfiSlice};
4-
5-
use super::{Lifetime, TyParamBound};
5+
use crate::ffi::FfiOption;
66

77
/// A singular generic parameter, like `'a` and `T` in this example:
88
///
@@ -16,6 +16,10 @@ use super::{Lifetime, TyParamBound};
1616
/// }
1717
/// ```
1818
///
19+
/// Bounds declared with the parameter, like the `: Copy` in the example above,
20+
/// are stored in the [`GenericParams`][`super::GenericParams`] of the item, that
21+
/// introduced this parameter.
22+
///
1923
/// See: <https://doc.rust-lang.org/reference/items/generics.html>
2024
#[repr(C)]
2125
#[derive(Debug, PartialEq, Eq, Hash)]
@@ -63,19 +67,19 @@ pub trait GenericParamData<'ast> {
6367
#[repr(C)]
6468
#[derive(Debug, PartialEq, Eq, Hash)]
6569
pub struct TyParam<'ast> {
70+
_data: PhantomData<&'ast ()>,
6671
id: GenericId,
6772
name: SymbolId,
68-
bounds: FfiSlice<'ast, TyParamBound<'ast>>,
6973
span: FfiOption<SpanId>,
7074
}
7175

7276
#[cfg(feature = "driver-api")]
7377
impl<'ast> TyParam<'ast> {
74-
pub fn new(span: Option<SpanId>, name: SymbolId, id: GenericId, bounds: &'ast [TyParamBound<'ast>]) -> Self {
78+
pub fn new(span: Option<SpanId>, name: SymbolId, id: GenericId) -> Self {
7579
Self {
80+
_data: PhantomData,
7681
id,
7782
name,
78-
bounds: bounds.into(),
7983
span: span.into(),
8084
}
8185
}
@@ -89,10 +93,6 @@ impl<'ast> TyParam<'ast> {
8993
pub fn name(&self) -> String {
9094
with_cx(self, |cx| cx.symbol_str(self.name))
9195
}
92-
93-
pub fn span(&self) -> Option<&Span<'ast>> {
94-
self.span.get().map(|span| with_cx(self, |cx| cx.get_span(*span)))
95-
}
9696
}
9797

9898
impl<'ast> GenericParamData<'ast> for TyParam<'ast> {
@@ -117,19 +117,19 @@ impl<'ast> From<&'ast TyParam<'ast>> for GenericParamKind<'ast> {
117117
#[repr(C)]
118118
#[derive(Debug, PartialEq, Eq, Hash)]
119119
pub struct LifetimeParam<'ast> {
120+
_data: PhantomData<&'ast ()>,
120121
id: GenericId,
121122
name: SymbolId,
122-
bounds: FfiSlice<'ast, Lifetime<'ast>>,
123123
span: FfiOption<SpanId>,
124124
}
125125

126126
#[cfg(feature = "driver-api")]
127127
impl<'ast> LifetimeParam<'ast> {
128-
pub fn new(id: GenericId, name: SymbolId, bounds: &'ast [Lifetime<'ast>], span: Option<SpanId>) -> Self {
128+
pub fn new(id: GenericId, name: SymbolId, span: Option<SpanId>) -> Self {
129129
Self {
130+
_data: PhantomData,
130131
id,
131132
name,
132-
bounds: bounds.into(),
133133
span: span.into(),
134134
}
135135
}
@@ -143,17 +143,6 @@ impl<'ast> LifetimeParam<'ast> {
143143
pub fn name(&self) -> String {
144144
with_cx(self, |cx| cx.symbol_str(self.name))
145145
}
146-
147-
/// Returns the bounds defined as part of the parameter. For example `'long`
148-
/// would be returned as the bound for `'short`.
149-
///
150-
/// ```
151-
/// fn foo<'long, 'short: 'long>() {}
152-
/// // ^^^^^^^^^^^^
153-
/// ```
154-
pub fn bounds(&self) -> &'ast [Lifetime<'ast>] {
155-
self.bounds.get()
156-
}
157146
}
158147

159148
impl<'ast> GenericParamData<'ast> for LifetimeParam<'ast> {

linter_api/src/ast/item/const_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl<'ast> ConstItem<'ast> {
2424
}
2525

2626
pub fn body_id(&self) -> Option<BodyId> {
27-
self.body_id.get().copied()
27+
self.body_id.copy()
2828
}
2929
}
3030

linter_api/src/ast/item/fn_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<'ast> FnItem<'ast> {
3737
}
3838

3939
pub fn body(&self) -> Option<BodyId> {
40-
self.body.get().copied()
40+
self.body.copy()
4141
}
4242
}
4343

linter_api/src/ast/item/static_item.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
ast::{ty::TyKind, BodyId, Mutability},
2+
ast::{ty::TyKind, BodyId},
33
ffi::FfiOption,
44
};
55

@@ -20,7 +20,7 @@ use super::CommonItemData;
2020
#[derive(Debug)]
2121
pub struct StaticItem<'ast> {
2222
data: CommonItemData<'ast>,
23-
mutability: Mutability,
23+
is_mut: bool,
2424
body_id: FfiOption<BodyId>,
2525
ty: TyKind<'ast>,
2626
}
@@ -30,7 +30,7 @@ super::impl_item_data!(StaticItem, Static);
3030
impl<'ast> StaticItem<'ast> {
3131
/// The mutability of this item
3232
pub fn is_mutable(&self) -> bool {
33-
self.mutability == Mutability::Mut
33+
self.is_mut
3434
}
3535

3636
/// The defined type of this static item
@@ -40,16 +40,16 @@ impl<'ast> StaticItem<'ast> {
4040

4141
/// This returns the [`BodyId`] of the initialization body.
4242
pub fn body_id(&self) -> Option<BodyId> {
43-
self.body_id.get().copied()
43+
self.body_id.copy()
4444
}
4545
}
4646

4747
#[cfg(feature = "driver-api")]
4848
impl<'ast> StaticItem<'ast> {
49-
pub fn new(data: CommonItemData<'ast>, mutability: Mutability, body_id: Option<BodyId>, ty: TyKind<'ast>) -> Self {
49+
pub fn new(data: CommonItemData<'ast>, is_mut: bool, body_id: Option<BodyId>, ty: TyKind<'ast>) -> Self {
5050
Self {
5151
data,
52-
mutability,
52+
is_mut,
5353
body_id: body_id.into(),
5454
ty,
5555
}

linter_api/src/ast/item/ty_alias_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl<'ast> TyAliasItem<'ast> {
4040
}
4141

4242
pub fn aliased_ty(&self) -> Option<TyKind> {
43-
self.aliased_ty.get().copied()
43+
self.aliased_ty.copy()
4444
}
4545
}
4646

linter_api/src/ast/ty.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ mod inferred_ty;
4949
pub use inferred_ty::*;
5050
mod generic_ty;
5151
pub use generic_ty::*;
52+
mod alias_ty;
53+
pub use alias_ty::*;
54+
mod self_ty;
55+
pub use self_ty::*;
56+
mod relative_ty;
57+
pub use relative_ty::*;
5258

5359
pub trait TyData<'ast> {
5460
fn as_kind(&'ast self) -> TyKind<'ast>;
@@ -155,6 +161,11 @@ pub enum TyKind<'ast> {
155161
Inferred(&'ast InferredTy<'ast>),
156162
/// A generic type, that has been specified in a surrounding item
157163
Generic(&'ast GenericTy<'ast>),
164+
Alias(&'ast AliasTy<'ast>),
165+
/// The `Self` in impl blocks or trait declarations
166+
SelfTy(&'ast SelfTy<'ast>),
167+
/// A type declared relative to another type, like `Iterator::Item`
168+
Relative(&'ast RelativeTy<'ast>),
158169
}
159170

160171
impl<'ast> TyKind<'ast> {
@@ -216,8 +227,9 @@ impl<'ast> TyKind<'ast> {
216227
macro_rules! impl_ty_data_fn {
217228
($method:ident () -> $return_ty:ty) => {
218229
impl_ty_data_fn!($method() -> $return_ty,
219-
Bool, Num, Text, Never, Tuple, Array, Slice, Struct, Enum, Union,
220-
Fn, Closure, Ref, RawPtr, FnPtr, TraitObj, ImplTrait, Inferred, Generic);
230+
Bool, Num, Text, Never, Tuple, Array, Slice, Struct, Enum, Union, Fn,
231+
Closure, Ref, RawPtr, FnPtr, TraitObj, ImplTrait, Inferred, Generic,
232+
Alias, SelfTy, Relative);
221233
};
222234
($method:ident () -> $return_ty:ty $(, $item:ident)+) => {
223235
pub fn $method(&self) -> $return_ty {

linter_api/src/ast/ty/alias_ty.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use crate::ast::ItemId;
2+
3+
use super::CommonTyData;
4+
5+
#[repr(C)]
6+
#[derive(Debug, PartialEq, Eq, Hash)]
7+
pub struct AliasTy<'ast> {
8+
data: CommonTyData<'ast>,
9+
item: ItemId,
10+
}
11+
12+
super::impl_ty_data!(AliasTy<'ast>, Alias);
13+
14+
impl<'ast> AliasTy<'ast> {
15+
/// This returns the [`ItemId`] belonging to the
16+
/// [`TyAliasItem`][`super::super::item::TyAliasItem`] that declared this item.
17+
pub fn alias_item_id(&self) -> ItemId {
18+
self.item
19+
}
20+
}
21+
22+
#[cfg(feature = "driver-api")]
23+
impl<'ast> AliasTy<'ast> {
24+
pub fn new(data: CommonTyData<'ast>, item: ItemId) -> Self {
25+
Self { data, item }
26+
}
27+
}

linter_api/src/ast/ty/raw_ptr_ty.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
1-
use crate::ast::Mutability;
2-
31
use super::{CommonTyData, TyKind};
42

53
#[repr(C)]
64
#[derive(Debug, PartialEq, Eq, Hash)]
75
pub struct RawPtrTy<'ast> {
86
data: CommonTyData<'ast>,
9-
mutability: Mutability,
7+
is_mut: bool,
108
inner_ty: TyKind<'ast>,
119
}
1210

1311
#[cfg(feature = "driver-api")]
1412
impl<'ast> RawPtrTy<'ast> {
15-
pub fn new(data: CommonTyData<'ast>, mutability: Mutability, inner_ty: TyKind<'ast>) -> Self {
16-
Self {
17-
data,
18-
mutability,
19-
inner_ty,
20-
}
13+
pub fn new(data: CommonTyData<'ast>, is_mut: bool, inner_ty: TyKind<'ast>) -> Self {
14+
Self { data, is_mut, inner_ty }
2115
}
2216
}
2317

2418
super::impl_ty_data!(RawPtrTy<'ast>, RawPtr);
2519

2620
impl<'ast> RawPtrTy<'ast> {
2721
pub fn is_mut(&self) -> bool {
28-
matches!(self.mutability, Mutability::Mut)
22+
self.is_mut
2923
}
3024

3125
pub fn inner_ty(&self) -> TyKind<'ast> {

0 commit comments

Comments
 (0)