Skip to content

Commit e1dfde8

Browse files
authored
API: Add impl From<*> for sem::TyKind and correct some names of semantic types (#322)
* API: Add `sem::CommonTyData` * API: Rename `sem::TyKind::FnTy` -> `sem::TyKind::Fn` * Doc: Track changes * PR Review 1 * PR Review 2
1 parent 3560c23 commit e1dfde8

File tree

15 files changed

+360
-237
lines changed

15 files changed

+360
-237
lines changed

CHANGELOG.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
[Unreleased]: https://github.com/rust-marker/marker/compare/v0.4.0...HEAD
1+
[Unreleased]: https://github.com/rust-marker/marker/compare/v0.4.2...HEAD
2+
[0.4.2]: https://github.com/rust-marker/marker/releases/tag/v0.4.2
3+
[0.4.1]: https://github.com/rust-marker/marker/releases/tag/v0.4.1
24
[0.4.0]: https://github.com/rust-marker/marker/releases/tag/v0.4.0
35
[0.3.0]: https://github.com/rust-marker/marker/releases/tag/v0.3.0
46
[0.2.1]: https://github.com/rust-marker/marker/releases/tag/v0.2.1
@@ -24,10 +26,24 @@ The following components are considered to be internal and they are excluded fro
2426

2527
## [Unreleased]
2628

29+
[#322]: https://github.com/rust-marker/marker/pull/322
30+
31+
### Added
32+
33+
- [#322]: `sem::TyKind` now implements `From<*>` for all semantic types.
34+
35+
### Breaking Changes
36+
37+
- [#322]: Renamed `sem::TyKind::FnTy` -> `sem::TyKind::Fn`
38+
- [#322]: Renamed `sem::TyKind::ClosureTy` -> `sem::TyKind::Closure`
39+
- [#322]: Renamed `sem::ClosureTy::closure_ty_id` -> `sem::ClosureTy::def_id`
40+
2741
## [0.4.2] - 2023-11-25
2842

2943
[#320]: https://github.com/rust-marker/marker/pull/320
3044

45+
### Fixed
46+
3147
- [#320]: Disable LTO on release builds to fix the crash on Windows with `exit code: 0xc0000005, STATUS_ACCESS_VIOLATION`
3248

3349
## [0.4.1] - 2023-11-24

marker_api/src/common/id.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ new_id! {
142142
pub(crate) SymbolId: u32
143143
}
144144

145+
new_id! {
146+
/// **Unstable**
147+
///
148+
/// This id is used by the driver to lint the semantic type representation, back to the
149+
/// driver type representation, if needed.
150+
#[cfg_attr(feature = "driver-api", visibility::make(pub))]
151+
pub(crate) DriverTyId: u64
152+
}
153+
145154
new_id! {
146155
/// This ID uniquely identifies a statement during linting.
147156
pub StmtId: u64

marker_api/src/sem/ty.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod ptr_ty;
55
mod sequence_ty;
66
mod trait_ty;
77
mod user_ty;
8+
89
pub use fn_ty::*;
910
pub use other_ty::*;
1011
pub use prim_ty::*;
@@ -13,6 +14,9 @@ pub use sequence_ty::*;
1314
pub use trait_ty::*;
1415
pub use user_ty::*;
1516

17+
use crate::common::DriverTyId;
18+
use std::{fmt::Debug, marker::PhantomData};
19+
1620
/// The semantic representation of a type.
1721
#[repr(C)]
1822
#[non_exhaustive]
@@ -43,10 +47,10 @@ pub enum TyKind<'ast> {
4347
// ================================
4448
/// A [function item type](https://doc.rust-lang.org/reference/types/function-item.html)
4549
/// identifying a specific function and potentualy additional generics.
46-
FnTy(&'ast FnTy<'ast>),
50+
Fn(&'ast FnTy<'ast>),
4751
/// The semantic representation of a
4852
/// [closure type](https://doc.rust-lang.org/reference/types/closure.html).
49-
ClosureTy(&'ast ClosureTy<'ast>),
53+
Closure(&'ast ClosureTy<'ast>),
5054
// ================================
5155
// Pointer types
5256
// ================================
@@ -110,3 +114,43 @@ impl<'ast> TyKind<'ast> {
110114
ty
111115
}
112116
}
117+
118+
#[repr(C)]
119+
#[cfg_attr(feature = "driver-api", visibility::make(pub))]
120+
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
121+
pub(crate) struct CommonTyData<'ast> {
122+
#[cfg_attr(feature = "driver-api", builder(default))]
123+
_lifetime: PhantomData<&'ast ()>,
124+
driver_id: DriverTyId,
125+
}
126+
127+
impl<'ast> Debug for CommonTyData<'ast> {
128+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
129+
f.debug_struct("CommonTyData {...}").finish()
130+
}
131+
}
132+
133+
#[cfg(feature = "driver-api")]
134+
impl<'ast> CommonTyData<'ast> {
135+
pub fn driver_id(&self) -> DriverTyId {
136+
self.driver_id
137+
}
138+
}
139+
140+
macro_rules! impl_ty_data {
141+
($self_ty:ty, $enum_name:ident) => {
142+
#[cfg(feature = "driver_api")]
143+
impl<'ast> $self_ty {
144+
pub fn data(&self) -> &$crate::sem::ty::CommonTyData<'ast> {
145+
&self.data
146+
}
147+
}
148+
149+
impl<'ast> From<&'ast $self_ty> for $crate::sem::ty::TyKind<'ast> {
150+
fn from(from: &'ast $self_ty) -> Self {
151+
$crate::sem::ty::TyKind::$enum_name(from)
152+
}
153+
}
154+
};
155+
}
156+
use impl_ty_data;

marker_api/src/sem/ty/fn_ty.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ use crate::{
33
sem::generic::GenericArgs,
44
};
55

6+
use super::CommonTyData;
7+
68
/// A [function item type](https://doc.rust-lang.org/reference/types/function-item.html)
79
/// identifying a specific function and potentualy additional generics.
810
#[repr(C)]
911
#[derive(Debug)]
12+
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
1013
pub struct FnTy<'ast> {
14+
data: CommonTyData<'ast>,
1115
fn_id: ItemId,
1216
generics: GenericArgs<'ast>,
1317
}
@@ -24,12 +28,7 @@ impl<'ast> FnTy<'ast> {
2428
}
2529
}
2630

27-
#[cfg(feature = "driver-api")]
28-
impl<'ast> FnTy<'ast> {
29-
pub fn new(fn_id: ItemId, generics: GenericArgs<'ast>) -> Self {
30-
Self { fn_id, generics }
31-
}
32-
}
31+
super::impl_ty_data!(FnTy<'ast>, Fn);
3332

3433
/// The semantic representation of a
3534
/// [closure type](https://doc.rust-lang.org/reference/types/closure.html).
@@ -38,18 +37,20 @@ impl<'ast> FnTy<'ast> {
3837
/// closure. This type on it's own therefore only identifies the type of the closure.
3938
#[repr(C)]
4039
#[derive(Debug)]
40+
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
4141
pub struct ClosureTy<'ast> {
42-
closure_ty_id: TyDefId,
42+
data: CommonTyData<'ast>,
43+
def_id: TyDefId,
4344
generics: GenericArgs<'ast>,
4445
}
4546

4647
impl<'ast> ClosureTy<'ast> {
47-
/// This returns the [`ItemId`] of the identified function.
48-
pub fn closure_ty_id(&self) -> TyDefId {
49-
self.closure_ty_id
48+
/// This returns the [`ItemId`] of the struct that was generated for this closure.
49+
pub fn def_id(&self) -> TyDefId {
50+
self.def_id
5051
}
5152

52-
/// This returns the [`GenericArgs`] used by identified function
53+
/// This returns the [`GenericArgs`] used by closure.
5354
pub fn generics(&self) -> &GenericArgs<'ast> {
5455
&self.generics
5556
}
@@ -58,12 +59,4 @@ impl<'ast> ClosureTy<'ast> {
5859
// parameters and return type.
5960
}
6061

61-
#[cfg(feature = "driver-api")]
62-
impl<'ast> ClosureTy<'ast> {
63-
pub fn new(closure_ty_id: TyDefId, generics: GenericArgs<'ast>) -> Self {
64-
Self {
65-
closure_ty_id,
66-
generics,
67-
}
68-
}
69-
}
62+
super::impl_ty_data!(ClosureTy<'ast>, Closure);

marker_api/src/sem/ty/other_ty.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
use std::marker::PhantomData;
1+
use super::CommonTyData;
22

33
/// The placeholder type, signalling that the semantic type is still unstable
44
/// and therefor not represented as part of the API.
55
#[repr(C)]
66
#[derive(Debug)]
7+
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
78
pub struct UnstableTy<'ast> {
8-
_lt: PhantomData<&'ast ()>,
9+
data: CommonTyData<'ast>,
910
}
1011

11-
#[cfg(feature = "driver-api")]
12-
impl<'ast> UnstableTy<'ast> {
13-
pub fn new() -> Self {
14-
Self { _lt: PhantomData }
15-
}
16-
}
12+
super::impl_ty_data!(UnstableTy<'ast>, Unstable);

marker_api/src/sem/ty/prim_ty.rs

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
use std::marker::PhantomData;
2-
31
use crate::common::{NumKind, TextKind};
42

3+
use super::CommonTyData;
4+
55
/// The semantic representation of the [`bool`] type.
66
#[repr(C)]
77
#[derive(Debug)]
8+
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
89
pub struct BoolTy<'ast> {
9-
_lt: PhantomData<&'ast ()>,
10-
}
11-
12-
#[cfg(feature = "driver-api")]
13-
impl<'ast> BoolTy<'ast> {
14-
pub fn new() -> Self {
15-
Self { _lt: PhantomData }
16-
}
10+
data: CommonTyData<'ast>,
1711
}
1812

1913
impl<'ast> std::fmt::Display for BoolTy<'ast> {
@@ -22,24 +16,17 @@ impl<'ast> std::fmt::Display for BoolTy<'ast> {
2216
}
2317
}
2418

19+
super::impl_ty_data!(BoolTy<'ast>, Bool);
20+
2521
/// The semantic representation of a numeric type like [`u32`], [`i32`], [`f64`].
2622
#[repr(C)]
2723
#[derive(Debug)]
24+
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
2825
pub struct NumTy<'ast> {
29-
_ast: PhantomData<&'ast ()>,
26+
data: CommonTyData<'ast>,
3027
numeric_kind: NumKind,
3128
}
3229

33-
#[cfg(feature = "driver-api")]
34-
impl<'ast> NumTy<'ast> {
35-
pub fn new(numeric_kind: NumKind) -> Self {
36-
Self {
37-
_ast: PhantomData,
38-
numeric_kind,
39-
}
40-
}
41-
}
42-
4330
impl<'ast> NumTy<'ast> {
4431
pub fn numeric_kind(&self) -> NumKind {
4532
self.numeric_kind
@@ -62,6 +49,8 @@ impl<'ast> NumTy<'ast> {
6249
}
6350
}
6451

52+
super::impl_ty_data!(NumTy<'ast>, Num);
53+
6554
impl<'ast> std::fmt::Display for NumTy<'ast> {
6655
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
6756
write!(f, "{:?}", self.numeric_kind)
@@ -70,21 +59,12 @@ impl<'ast> std::fmt::Display for NumTy<'ast> {
7059

7160
/// The semantic representation of a textual type like [`char`] or [`str`].
7261
#[repr(C)]
62+
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
7363
pub struct TextTy<'ast> {
74-
_ast: PhantomData<&'ast ()>,
64+
data: CommonTyData<'ast>,
7565
textual_kind: TextKind,
7666
}
7767

78-
#[cfg(feature = "driver-api")]
79-
impl<'ast> TextTy<'ast> {
80-
pub fn new(textual_kind: TextKind) -> Self {
81-
Self {
82-
_ast: PhantomData,
83-
textual_kind,
84-
}
85-
}
86-
}
87-
8868
impl<'ast> TextTy<'ast> {
8969
pub fn textual_kind(&self) -> TextKind {
9070
self.textual_kind
@@ -99,6 +79,8 @@ impl<'ast> TextTy<'ast> {
9979
}
10080
}
10181

82+
super::impl_ty_data!(TextTy<'ast>, Text);
83+
10284
impl<'ast> std::fmt::Debug for TextTy<'ast> {
10385
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
10486
write!(f, "{:?}", self.textual_kind)
@@ -107,16 +89,12 @@ impl<'ast> std::fmt::Debug for TextTy<'ast> {
10789

10890
/// The semantic representation of the never type [`!`](prim@never).
10991
#[repr(C)]
92+
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
11093
pub struct NeverTy<'ast> {
111-
_lt: PhantomData<&'ast ()>,
94+
data: CommonTyData<'ast>,
11295
}
11396

114-
#[cfg(feature = "driver-api")]
115-
impl<'ast> NeverTy<'ast> {
116-
pub fn new() -> Self {
117-
Self { _lt: PhantomData }
118-
}
119-
}
97+
super::impl_ty_data!(NeverTy<'ast>, Never);
12098

12199
impl<'ast> std::fmt::Debug for NeverTy<'ast> {
122100
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

0 commit comments

Comments
 (0)