Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Big API Module Migration #268

Merged
merged 12 commits into from
Oct 4, 2023
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ See the [v0.3.0 milestone] for a full list of all changes.
[#260]: https://github.com/rust-marker/marker/pull/260
[#263]: https://github.com/rust-marker/marker/pull/263
[#265]: https://github.com/rust-marker/marker/pull/265
[#268]: https://github.com/rust-marker/marker/pull/268

### Added
- [#232]: Add scope config for visitors and `for_each_expr` to `marker_utils`
Expand All @@ -56,6 +57,13 @@ See the [v0.3.0 milestone] for a full list of all changes.
- [#263]: Updated the [`ui_test`](https://crates.io/crates/ui_test) used by `marker_uitest` from `v0.11.7` to `v0.21.2`
- [#260]: Moved `AstContext::{body, item, lint_level_at}` to the new `AstMap` struct accessible via `MarkerContext::ast()`
- [#265]: Removed the `CallableData` trait
- [#268]: Moved semantic types and generics to the new `marker_api::sem` module
- [#268]: Moved common items, like IDs, to the new `marker_api::common` module
- [#268]: Removed the `Sem` and `Syn` prefix from types and generics
- [#268]: `marker_api::prelude` no longer contains the semantic and syntactic `TyKind` enums
- [#268]: `marker_api::prelude` now imports the `sem` and `ast` names
- [#268]: The `marker_api::ast` module has been flattened
- [#268]: The `marker_api::lint` and `marker_api::interface` are now private

### Internal

Expand Down
6 changes: 3 additions & 3 deletions marker_adapter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ mod map;
pub use map::*;

use marker_api::{
ast::{ty::SemTyKind, ExpnId, ExprId, SpanId, SymbolId},
common::{ExpnId, ExprId, SpanId, SymbolId},
context::{MarkerContextCallbacks, MarkerContextData},
diagnostic::Diagnostic,
ffi::{self, FfiOption},
Expand Down Expand Up @@ -144,7 +144,7 @@ pub trait MarkerContextDriver<'ast> {

fn resolve_ty_ids(&'ast self, path: &str) -> &'ast [TyDefId];

fn expr_ty(&'ast self, expr: ExprId) -> SemTyKind<'ast>;
fn expr_ty(&'ast self, expr: ExprId) -> marker_api::sem::TyKind<'ast>;
fn span(&'ast self, owner: SpanId) -> &'ast Span<'ast>;
fn span_snippet(&'ast self, span: &Span<'_>) -> Option<&'ast str>;
fn span_source(&'ast self, span: &Span<'_>) -> SpanSource<'ast>;
Expand All @@ -167,7 +167,7 @@ extern "C" fn resolve_ty_ids<'ast>(

// False positive because `SemTyKind` is non-exhaustive
#[allow(improper_ctypes_definitions)]
extern "C" fn expr_ty<'ast>(data: &'ast MarkerContextData, expr: ExprId) -> SemTyKind<'ast> {
extern "C" fn expr_ty<'ast>(data: &'ast MarkerContextData, expr: ExprId) -> marker_api::sem::TyKind<'ast> {
unsafe { as_driver(data) }.expr_ty(expr)
}

Expand Down
12 changes: 6 additions & 6 deletions marker_adapter/src/context/map.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use marker_api::{
ast::item::{EnumVariant, Field},
ast::{EnumVariant, ItemField},
common::Level,
context::{AstMap, AstMapCallbacks, AstMapData},
ffi,
lint::Level,
prelude::*,
};

Expand Down Expand Up @@ -37,8 +37,8 @@ impl<'ast> AstMapWrapper<'ast> {
pub trait AstMapDriver<'ast> {
fn item(&'ast self, id: ItemId) -> Option<ItemKind<'ast>>;
fn variant(&'ast self, id: VariantId) -> Option<&'ast EnumVariant<'ast>>;
fn field(&'ast self, id: FieldId) -> Option<&'ast Field<'ast>>;
fn body(&'ast self, id: BodyId) -> &'ast Body<'ast>;
fn field(&'ast self, id: FieldId) -> Option<&'ast ItemField<'ast>>;
fn body(&'ast self, id: BodyId) -> &'ast ast::Body<'ast>;
fn stmt(&'ast self, id: StmtId) -> StmtKind<'ast>;
fn expr(&'ast self, id: ExprId) -> ExprKind<'ast>;

Expand All @@ -52,10 +52,10 @@ extern "C" fn item<'ast>(data: &'ast AstMapData, id: ItemId) -> ffi::FfiOption<I
extern "C" fn variant<'ast>(data: &'ast AstMapData, id: VariantId) -> ffi::FfiOption<&'ast EnumVariant<'ast>> {
unsafe { as_driver(data) }.variant(id).into()
}
extern "C" fn field<'ast>(data: &'ast AstMapData, id: FieldId) -> ffi::FfiOption<&'ast Field<'ast>> {
extern "C" fn field<'ast>(data: &'ast AstMapData, id: FieldId) -> ffi::FfiOption<&'ast ItemField<'ast>> {
unsafe { as_driver(data) }.field(id).into()
}
extern "C" fn body<'ast>(data: &'ast AstMapData, id: BodyId) -> &'ast Body<'ast> {
extern "C" fn body<'ast>(data: &'ast AstMapData, id: BodyId) -> &'ast ast::Body<'ast> {
unsafe { as_driver(data) }.body(id)
}
#[allow(improper_ctypes_definitions)] // FP because `StmtKind` is non-exhaustive
Expand Down
9 changes: 2 additions & 7 deletions marker_adapter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ pub use loader::LintCrateInfo;

use loader::LintCrateRegistry;
use marker_api::{
ast::{
expr::ExprKind,
item::{Body, EnumVariant, Field, ItemKind},
stmt::StmtKind,
Crate,
},
ast::{Body, Crate, EnumVariant, ExprKind, ItemField, ItemKind, StmtKind},
context::MarkerContext,
LintPass, LintPassInfo,
};
Expand Down Expand Up @@ -85,7 +80,7 @@ impl Visitor<()> for AdapterInner {
ControlFlow::Continue(())
}

fn visit_field<'ast>(&mut self, cx: &'ast MarkerContext<'ast>, field: &'ast Field<'ast>) -> ControlFlow<()> {
fn visit_field<'ast>(&mut self, cx: &'ast MarkerContext<'ast>, field: &'ast ItemField<'ast>) -> ControlFlow<()> {
self.external_lint_crates.check_field(cx, field);
ControlFlow::Continue(())
}
Expand Down
14 changes: 7 additions & 7 deletions marker_adapter/src/loader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::error::prelude::*;
use camino::Utf8PathBuf;
use libloading::Library;
use marker_api::{interface::LintCrateBindings, MarkerContext};
use marker_api::{LintCrateBindings, MarkerContext};
use marker_api::{LintPass, LintPassInfo, MARKER_API_VERSION};

use super::LINT_CRATES_ENV;
Expand Down Expand Up @@ -82,13 +82,13 @@ impl LintPass for LintCrateRegistry {
panic!("`registered_lints` should not be called on `LintCrateRegistry`");
}

fn check_item<'ast>(&mut self, cx: &'ast MarkerContext<'ast>, item: marker_api::ast::item::ItemKind<'ast>) {
fn check_item<'ast>(&mut self, cx: &'ast MarkerContext<'ast>, item: marker_api::ast::ItemKind<'ast>) {
for lp in &self.passes {
(lp.bindings.check_item)(cx, item);
}
}

fn check_field<'ast>(&mut self, cx: &'ast MarkerContext<'ast>, field: &'ast marker_api::ast::item::Field<'ast>) {
fn check_field<'ast>(&mut self, cx: &'ast MarkerContext<'ast>, field: &'ast marker_api::ast::ItemField<'ast>) {
for lp in &self.passes {
(lp.bindings.check_field)(cx, field);
}
Expand All @@ -97,26 +97,26 @@ impl LintPass for LintCrateRegistry {
fn check_variant<'ast>(
&mut self,
cx: &'ast MarkerContext<'ast>,
variant: &'ast marker_api::ast::item::EnumVariant<'ast>,
variant: &'ast marker_api::ast::EnumVariant<'ast>,
) {
for lp in &self.passes {
(lp.bindings.check_variant)(cx, variant);
}
}

fn check_body<'ast>(&mut self, cx: &'ast MarkerContext<'ast>, body: &'ast marker_api::ast::item::Body<'ast>) {
fn check_body<'ast>(&mut self, cx: &'ast MarkerContext<'ast>, body: &'ast marker_api::ast::Body<'ast>) {
for lp in &self.passes {
(lp.bindings.check_body)(cx, body);
}
}

fn check_stmt<'ast>(&mut self, cx: &'ast MarkerContext<'ast>, stmt: marker_api::ast::stmt::StmtKind<'ast>) {
fn check_stmt<'ast>(&mut self, cx: &'ast MarkerContext<'ast>, stmt: marker_api::ast::StmtKind<'ast>) {
for lp in &self.passes {
(lp.bindings.check_stmt)(cx, stmt);
}
}

fn check_expr<'ast>(&mut self, cx: &'ast MarkerContext<'ast>, expr: marker_api::ast::expr::ExprKind<'ast>) {
fn check_expr<'ast>(&mut self, cx: &'ast MarkerContext<'ast>, expr: marker_api::ast::ExprKind<'ast>) {
for lp in &self.passes {
(lp.bindings.check_expr)(cx, expr);
}
Expand Down
26 changes: 16 additions & 10 deletions marker_api/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
//! A module containing the AST of Marker, which is the main syntactic
//! representation of the written code.

mod common;
mod expr;
mod generic;
mod item;
mod pat;
mod stmt;
mod ty;
pub use common::*;
pub use expr::*;
pub use generic::*;
pub use item::*;
pub use pat::*;
pub use stmt::*;
pub use ty::*;

use crate::ffi::FfiSlice;

use self::item::ItemKind;

pub mod expr;
pub mod generic;
pub mod item;
pub mod pat;
pub mod stmt;
pub mod ty;
use crate::{common::CrateId, ffi::FfiSlice};

#[derive(Debug)]
pub struct Crate<'ast> {
Expand Down
124 changes: 6 additions & 118 deletions marker_api/src/ast/common.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,22 @@
mod id;
pub use id::*;
mod ast_path;
pub use ast_path::*;

use std::{fmt::Debug, marker::PhantomData};
use std::fmt::Debug;

use super::generic::SynGenericArgs;
use crate::common::ItemId;

#[non_exhaustive]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Edition {
Edition2015,
Edition2018,
Edition2021,
}

#[non_exhaustive]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Abi {
/// This is the default of the current driver, the actual ABI can vary between
/// implementations. In general this means that the user has not selected a
/// specific ABI.
Default,
C,
/// FIXME: Remove this variant. See
/// <https://doc.rust-lang.org/nightly/nightly-rustc/rustc_target/spec/abi/enum.Abi.html>
Other,
}
use super::generic::GenericArgs;

#[repr(C)]
#[derive(Debug)]
pub struct TraitRef<'ast> {
item_id: ItemId,
generics: SynGenericArgs<'ast>,
generics: GenericArgs<'ast>,
}

#[cfg(feature = "driver-api")]
impl<'ast> TraitRef<'ast> {
pub fn new(item_id: ItemId, generics: SynGenericArgs<'ast>) -> Self {
pub fn new(item_id: ItemId, generics: GenericArgs<'ast>) -> Self {
Self { item_id, generics }
}
}
Expand All @@ -47,98 +26,7 @@ impl<'ast> TraitRef<'ast> {
self.item_id
}

pub fn generics(&self) -> &SynGenericArgs<'ast> {
pub fn generics(&self) -> &GenericArgs<'ast> {
&self.generics
}
}

#[repr(C)]
#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Mutability {
/// The object is mutable
Mut,
/// The object is unmutable
Unmut,
}

impl Mutability {
#[must_use]
pub fn is_mut(&self) -> bool {
matches!(self, Self::Mut)
}
}

#[repr(C)]
#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Safety {
Safe,
Unsafe,
}

impl Safety {
#[must_use]
pub fn is_unsafe(&self) -> bool {
matches!(self, Self::Unsafe)
}
}

#[repr(C)]
#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Constness {
Const,
NotConst,
}

impl Constness {
#[must_use]
pub fn is_const(&self) -> bool {
matches!(self, Self::Const)
}
}

#[repr(C)]
#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Syncness {
Sync,
Async,
}

impl Syncness {
#[must_use]
pub fn is_sync(&self) -> bool {
matches!(self, Self::Sync)
}

#[must_use]
pub fn is_async(&self) -> bool {
matches!(self, Self::Async)
}
}

/// The semantic equivalent of a [`ConstExpr`][super::expr::ConstExpr], at least
/// theoretically. This part of the API is sadly not done yet, so this is just a
/// placeholder.
///
/// See: rust-marker/marker#179
#[repr(C)]
pub struct ConstValue<'ast> {
_lifetime: PhantomData<&'ast ()>,
}

impl<'ast> Debug for ConstValue<'ast> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ConstValue {{ /* WIP: See rust-marker/marker#179 */}}")
.finish()
}
}

#[cfg(feature = "driver-api")]
impl<'ast> ConstValue<'ast> {
pub fn new() -> Self {
Self { _lifetime: PhantomData }
}
}
Loading
Loading