Skip to content

Commit

Permalink
API: Use cfged typed-builder for type construction
Browse files Browse the repository at this point in the history
  • Loading branch information
xFrednet committed Sep 17, 2023
1 parent 883ddbb commit 3ed653e
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 74 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ toml = "0.7"
tracing = "0.1"
tracing-error = "0.2"
tracing-subscriber = "0.3"
typed-builder = "0.16.0"
ui_test = "0.11.7"
visibility = "0.0.1"
yansi = "1.0.0-rc.1"
Expand Down
5 changes: 3 additions & 2 deletions marker_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ version = { workspace = true }
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
visibility = { workspace = true }
typed-builder = { workspace = true, optional = true }
visibility = { workspace = true, optional = true }

[dev-dependencies]
expect-test = { workspace = true }
Expand All @@ -22,4 +23,4 @@ expect-test = { workspace = true }
# Some items should only be used by the driver implementing the functionality,
# this feature enables the export of these items. Note that this interface is
# unstable. All usage of the driver api can change between releases.
driver-api = []
driver-api = ["dep:visibility", "dep:typed-builder"]
8 changes: 1 addition & 7 deletions marker_api/src/ast/pat/lit_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::CommonPatData;
/// ```
#[repr(C)]
#[derive(Debug)]
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
pub struct LitPat<'ast> {
data: CommonPatData<'ast>,
lit: LitExprKind<'ast>,
Expand All @@ -27,10 +28,3 @@ impl<'ast> LitPat<'ast> {
}

super::impl_pat_data!(LitPat<'ast>, Lit);

#[cfg(feature = "driver-api")]
impl<'ast> LitPat<'ast> {
pub fn new(data: CommonPatData<'ast>, lit: LitExprKind<'ast>) -> Self {
Self { data, lit }
}
}
8 changes: 1 addition & 7 deletions marker_api/src/ast/pat/place_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use super::CommonPatData;
/// [`PatKind::Path`](crate::ast::pat::PatKind::Path) variant.
#[repr(C)]
#[derive(Debug)]
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
pub struct PlacePat<'ast> {
data: CommonPatData<'ast>,
place: ExprKind<'ast>,
Expand All @@ -48,10 +49,3 @@ impl<'ast> PlacePat<'ast> {
}

super::impl_pat_data!(PlacePat<'ast>, Place);

#[cfg(feature = "driver-api")]
impl<'ast> PlacePat<'ast> {
pub fn new(data: CommonPatData<'ast>, place: ExprKind<'ast>) -> Self {
Self { data, place }
}
}
52 changes: 8 additions & 44 deletions marker_api/src/ast/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,19 @@ impl<'ast> StmtKind<'ast> {
#[repr(C)]
#[derive(Debug)]
#[cfg_attr(feature = "driver-api", visibility::make(pub))]
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
struct CommonStmtData<'ast> {
/// The lifetime is not needed right now, but it's safer to include it for
/// future additions. Having it in this struct makes it easier for all
/// pattern structs, as they will have a valid use for `'ast` even if they
/// don't need it. Otherwise, we might need to declare this field in each
/// pattern.
#[cfg_attr(feature = "driver-api", builder(default))]
_lifetime: PhantomData<&'ast ()>,
id: StmtId,
span: SpanId,
}

#[cfg(feature = "driver-api")]
impl<'ast> CommonStmtData<'ast> {
pub fn new(id: StmtId, span: SpanId) -> Self {
Self {
_lifetime: PhantomData,
id,
span,
}
}
}

macro_rules! impl_stmt_data {
($self_ty:ty, $enum_name:ident) => {
impl<'ast> StmtData<'ast> for $self_ty {
Expand All @@ -102,11 +93,15 @@ use impl_stmt_data;

#[repr(C)]
#[derive(Debug)]
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
pub struct LetStmt<'ast> {
data: CommonStmtData<'ast>,
pat: PatKind<'ast>,
#[cfg_attr(feature = "driver-api", builder(setter(into)))]
ty: FfiOption<SynTyKind<'ast>>,
#[cfg_attr(feature = "driver-api", builder(setter(into)))]
init: FfiOption<ExprKind<'ast>>,
#[cfg_attr(feature = "driver-api", builder(setter(into)))]
els: FfiOption<ExprKind<'ast>>,
}

Expand Down Expand Up @@ -134,27 +129,9 @@ impl<'ast> LetStmt<'ast> {

impl_stmt_data!(LetStmt<'ast>, Let);

#[cfg(feature = "driver-api")]
impl<'ast> LetStmt<'ast> {
pub fn new(
data: CommonStmtData<'ast>,
pat: PatKind<'ast>,
ty: Option<SynTyKind<'ast>>,
init: Option<ExprKind<'ast>>,
els: Option<ExprKind<'ast>>,
) -> Self {
Self {
data,
pat,
ty: ty.into(),
init: init.into(),
els: els.into(),
}
}
}

#[repr(C)]
#[derive(Debug)]
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
pub struct ExprStmt<'ast> {
data: CommonStmtData<'ast>,
expr: ExprKind<'ast>,
Expand All @@ -168,15 +145,9 @@ impl<'ast> ExprStmt<'ast> {

impl_stmt_data!(ExprStmt<'ast>, Expr);

#[cfg(feature = "driver-api")]
impl<'ast> ExprStmt<'ast> {
pub fn new(data: CommonStmtData<'ast>, expr: ExprKind<'ast>) -> Self {
Self { data, expr }
}
}

#[repr(C)]
#[derive(Debug)]
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
pub struct ItemStmt<'ast> {
data: CommonStmtData<'ast>,
item: ItemKind<'ast>,
Expand All @@ -189,10 +160,3 @@ impl<'ast> ItemStmt<'ast> {
}

impl_stmt_data!(ItemStmt<'ast>, Item);

#[cfg(feature = "driver-api")]
impl<'ast> ItemStmt<'ast> {
pub fn new(data: CommonStmtData<'ast>, item: ItemKind<'ast>) -> Self {
Self { data, item }
}
}
3 changes: 2 additions & 1 deletion marker_api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#![doc = include_str!("../README.md")]
#![warn(clippy::pedantic)]
#![warn(clippy::exhaustive_enums)]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::trivially_copy_pass_by_ref)]
#![allow(clippy::unused_self)] // `self` is needed to change the behavior later
#![allow(clippy::missing_panics_doc)] // Temporary allow for `todo!`s
#![allow(clippy::new_without_default)] // Not very helpful as `new` is almost always cfged
#![cfg_attr(feature = "driver-api", allow(clippy::used_underscore_binding))] // See: idanarye/rust-typed-builder#113
#![cfg_attr(not(feature = "driver-api"), allow(dead_code))]
#![cfg_attr(not(feature = "driver-api"), warn(clippy::exhaustive_enums))] // See: idanarye/rust-typed-builder#112

pub static MARKER_API_VERSION: &str = env!("CARGO_PKG_VERSION");

Expand Down
6 changes: 3 additions & 3 deletions marker_rustc_driver/src/conversion/marker/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl<'ast, 'tcx> MarkerConverterInner<'ast, 'tcx> {
let lhs = lhs_map.get(id);
#[allow(clippy::unnecessary_unwrap, reason = "if let sadly breaks rustfmt")]
if pat.is_none() && matches!(mutab, rustc_ast::Mutability::Not) && lhs.is_some() {
PatKind::Place(self.alloc(PlacePat::new(data, *lhs.unwrap())))
PatKind::Place(self.alloc(PlacePat::builder().data(data).place(*lhs.unwrap()).build()))
} else {
PatKind::Ident(self.alloc({
IdentPat::new(
Expand Down Expand Up @@ -124,7 +124,7 @@ impl<'ast, 'tcx> MarkerConverterInner<'ast, 'tcx> {
let lit_expr = expr
.try_into()
.unwrap_or_else(|()| panic!("this should be a literal expression {lit:#?}"));
PatKind::Lit(self.alloc(LitPat::new(data, lit_expr)))
PatKind::Lit(self.alloc(LitPat::builder().data(data).lit(lit_expr).build()))
},
hir::PatKind::Range(start, end, kind) => PatKind::Range(self.alloc(RangePat::new(
data,
Expand Down Expand Up @@ -157,6 +157,6 @@ impl<'ast, 'tcx> MarkerConverterInner<'ast, 'tcx> {
#[must_use]
pub fn to_place_pat_from_expr(&self, expr: &hir::Expr<'tcx>) -> PatKind<'ast> {
let data = CommonPatData::new(self.to_span_id(expr.span));
PatKind::Place(self.alloc(PlacePat::new(data, self.to_expr(expr))))
PatKind::Place(self.alloc(PlacePat::builder().data(data).place(self.to_expr(expr)).build()))
}
}
27 changes: 17 additions & 10 deletions marker_rustc_driver/src/conversion/marker/stmts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ impl<'ast, 'tcx> MarkerConverterInner<'ast, 'tcx> {
return Some(*stmt);
}

let data = CommonStmtData::new(self.to_stmt_id(stmt.hir_id), self.to_span_id(stmt.span));
let data = CommonStmtData::builder()
.id(self.to_stmt_id(stmt.hir_id))
.span(self.to_span_id(stmt.span))
.build();
let stmt = match &stmt.kind {
hir::StmtKind::Local(local) => match local.source {
hir::LocalSource::Normal => StmtKind::Let(self.alloc(LetStmt::new(
data,
self.to_pat(local.pat),
local.ty.map(|ty| self.to_syn_ty(ty)),
local.init.map(|init| self.to_expr(init)),
local.els.map(|els| self.to_expr_from_block(els)),
))),
hir::LocalSource::Normal => StmtKind::Let(
self.alloc(
LetStmt::builder()
.data(data)
.pat(self.to_pat(local.pat))
.ty(local.ty.map(|ty| self.to_syn_ty(ty)))
.init(local.init.map(|init| self.to_expr(init)))
.els(local.els.map(|els| self.to_expr_from_block(els)))
.build(),
),
),
hir::LocalSource::AssignDesugar(_) => {
unreachable!("this will be handled by the block expr wrapping the desugar")
},
Expand All @@ -31,10 +38,10 @@ impl<'ast, 'tcx> MarkerConverterInner<'ast, 'tcx> {
},
hir::StmtKind::Item(item) => {
let item = self.to_item_from_id(*item)?;
StmtKind::Item(self.alloc(ItemStmt::new(data, item)))
StmtKind::Item(self.alloc(ItemStmt::builder().data(data).item(item).build()))
},
hir::StmtKind::Expr(expr) | hir::StmtKind::Semi(expr) => {
StmtKind::Expr(self.alloc(ExprStmt::new(data, self.to_expr(expr))))
StmtKind::Expr(self.alloc(ExprStmt::builder().data(data).expr(self.to_expr(expr)).build()))
},
};

Expand Down

0 comments on commit 3ed653e

Please sign in to comment.