Code review feedback

This commit is contained in:
Micha Reiser 2023-05-13 23:29:12 +02:00
parent a913c339f6
commit be93244938
No known key found for this signature in database
9 changed files with 56 additions and 52 deletions

View file

@ -17,7 +17,7 @@ members = [
[workspace.dependencies]
rustpython-ast = { path = "ast", default-features = false }
rustpython-parser-core = { path = "core", default-features = false }
rustpython-parser-core = { path = "core" }
rustpython-literal = { path = "literal" }
ahash = "0.7.6"

View file

@ -288,7 +288,7 @@ class StructVisitor(EmitVisitor):
def simple_sum(self, sum, name, depth):
rust_name = rust_type_name(name)
self.emit_attrs(depth)
self.emit("#[derive(is_macro::Is, Copy)]", depth)
self.emit("#[derive(is_macro::Is, Copy, Hash, Eq)]", depth)
self.emit(f"pub enum {rust_name} {{", depth)
for variant in sum.types:
self.emit(f"{variant.name},", depth + 1)
@ -1067,7 +1067,6 @@ def write_visitor_def(mod, type_info, f):
def write_ranged_def(mod, type_info, f):
f.write("use crate::Ranged;")
RangedDefVisitor(f, type_info).visit(mod)

View file

@ -911,20 +911,20 @@ pub enum Expr<R = TextRange> {
Slice(ExprSlice<R>),
}
#[derive(Clone, Debug, PartialEq, is_macro::Is, Copy)]
#[derive(Clone, Debug, PartialEq, is_macro::Is, Copy, Hash, Eq)]
pub enum ExprContext {
Load,
Store,
Del,
}
#[derive(Clone, Debug, PartialEq, is_macro::Is, Copy)]
#[derive(Clone, Debug, PartialEq, is_macro::Is, Copy, Hash, Eq)]
pub enum Boolop {
And,
Or,
}
#[derive(Clone, Debug, PartialEq, is_macro::Is, Copy)]
#[derive(Clone, Debug, PartialEq, is_macro::Is, Copy, Hash, Eq)]
pub enum Operator {
Add,
Sub,
@ -941,7 +941,7 @@ pub enum Operator {
FloorDiv,
}
#[derive(Clone, Debug, PartialEq, is_macro::Is, Copy)]
#[derive(Clone, Debug, PartialEq, is_macro::Is, Copy, Hash, Eq)]
pub enum Unaryop {
Invert,
Not,
@ -949,7 +949,7 @@ pub enum Unaryop {
USub,
}
#[derive(Clone, Debug, PartialEq, is_macro::Is, Copy)]
#[derive(Clone, Debug, PartialEq, is_macro::Is, Copy, Hash, Eq)]
pub enum Cmpop {
Eq,
NotEq,

View file

@ -1,6 +1,5 @@
// File automatically generated by ast/asdl_rs.py.
use crate::Ranged;
#[cfg(feature = "all-nodes-with-ranges")]
impl Ranged for crate::generic::ModModule<TextRange> {
fn range(&self) -> TextRange {

View file

@ -55,7 +55,11 @@ impl<R> Expr<R> {
}
}
#[cfg(target_arch = "x86_64")]
const_assert_eq!(std::mem::size_of::<Expr>(), 72);
#[cfg(target_arch = "x86_64")]
const_assert_eq!(std::mem::size_of::<Stmt>(), 136);
#[cfg(target_arch = "x86_64")]
const_assert_eq!(std::mem::size_of::<Pattern>(), 96);
#[cfg(target_arch = "x86_64")]
const_assert_eq!(std::mem::size_of::<Excepthandler>(), 64);

View file

@ -16,9 +16,8 @@ mod unparse;
pub use builtin::*;
pub use generic::*;
pub use ranged::{EmptyRange, Ranged};
pub use rustpython_parser_core::{text_size, ConversionFlag};
use std::fmt::{Debug, Display, Formatter};
use std::marker::PhantomData;
pub type Suite<R = TextRange> = Vec<Stmt<R>>;
@ -44,46 +43,9 @@ pub use rustpython_parser_core::source_code;
#[cfg(feature = "visitor")]
pub use visitor::Visitor;
pub trait Ranged {
fn range(&self) -> TextRange;
fn start(&self) -> TextSize {
self.range().start()
}
fn end(&self) -> TextSize {
self.range().end()
}
}
#[derive(Eq, PartialEq, Hash, Copy, Clone)]
pub struct EmptyRange<R> {
phantom: PhantomData<R>,
}
impl<R> Display for EmptyRange<R> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str("()")
}
}
impl<R> Debug for EmptyRange<R> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(self, f)
}
}
impl<R> Default for EmptyRange<R> {
fn default() -> Self {
EmptyRange {
phantom: PhantomData,
}
}
}
#[cfg(feature = "constant-optimization")]
mod optimizer;
#[cfg(feature = "constant-optimization")]
pub use optimizer::ConstantOptimizer;
use rustpython_parser_core::text_size::{TextRange, TextSize};
use rustpython_parser_core::text_size::TextRange;

View file

@ -1,4 +1,44 @@
use crate::text_size::TextRange;
use crate::text_size::{TextRange, TextSize};
use std::fmt::{Debug, Display, Formatter};
use std::marker::PhantomData;
pub use crate::builtin::*;
pub trait Ranged {
fn range(&self) -> TextRange;
fn start(&self) -> TextSize {
self.range().start()
}
fn end(&self) -> TextSize {
self.range().end()
}
}
#[derive(Eq, PartialEq, Hash, Copy, Clone)]
pub struct EmptyRange<R> {
phantom: PhantomData<R>,
}
impl<R> Display for EmptyRange<R> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str("()")
}
}
impl<R> Debug for EmptyRange<R> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(self, f)
}
}
impl<R> Default for EmptyRange<R> {
fn default() -> Self {
EmptyRange {
phantom: PhantomData,
}
}
}
include!("gen/ranged.rs");

View file

@ -16,4 +16,4 @@ serde = { version = "1.0.133", optional = true, default-features = false, featur
[features]
default = []
location = ["ruff_source_location"]
location = ["dep:ruff_source_location"]

View file

@ -10,7 +10,7 @@ edition = "2021"
[features]
default = ["location"]
location = ["rustpython-ast/location"]
location = ["rustpython-ast/location", "rustpython-parser-core/location"]
serde = ["dep:serde", "rustpython-parser-core/serde"]
all-nodes-with-ranges = ["rustpython-ast/all-nodes-with-ranges"]