Skip to content

Commit 73fc458

Browse files
committed
refactor: Remove complex nested context objects in linter converter
Removed the complex nested context objects in linter converter (`pos_context`, `dim_list_state`, `expr_state`, etc) that required explicit lifelines and were implementing Deref trait. Introduced instead a `ConvertibleIn` trait which accepts the extra information as a second parameter.
1 parent 1715362 commit 73fc458

30 files changed

Lines changed: 288 additions & 381 deletions
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
mod context;
2-
mod pos_context;
32
mod program_rules;
43
mod traits;
54
mod types;
65

76
pub use self::context::*;
8-
pub use self::pos_context::*;
97
pub use self::traits::*;
108
pub use self::types::*;

rusty_linter/src/converter/common/pos_context.rs

Lines changed: 0 additions & 35 deletions
This file was deleted.

rusty_linter/src/converter/common/program_rules.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use crate::converter::common::Context;
22
use crate::converter::common::Convertible;
3-
use crate::converter::common::PosContext;
3+
use crate::converter::common::ConvertibleIn;
44
use crate::core::IntoQualified;
55
use crate::core::LintErrorPos;
66
use crate::core::SubprogramName;
77
use crate::names::ImplicitVars;
8-
use rusty_common::{AtPos, HasPos, Positioned};
8+
use rusty_common::Position;
9+
use rusty_common::{AtPos, Positioned};
910
use rusty_parser::{
1011
DimVar, FunctionImplementation, GlobalStatement, GlobalStatementPos, Program, Statement,
1112
Statements, SubImplementation,
@@ -37,8 +38,12 @@ impl Convertible for Program {
3738
}
3839
}
3940

40-
impl<'a> Convertible<PosContext<'a>, Vec<GlobalStatementPos>> for GlobalStatement {
41-
fn convert(self, ctx: &mut PosContext<'a>) -> Result<Vec<GlobalStatementPos>, LintErrorPos> {
41+
impl ConvertibleIn<Position, Vec<GlobalStatementPos>> for GlobalStatement {
42+
fn convert_in(
43+
self,
44+
ctx: &mut Context,
45+
pos: Position,
46+
) -> Result<Vec<GlobalStatementPos>, LintErrorPos> {
4247
match self {
4348
Self::DefType(def_type) => {
4449
ctx.resolver.set(&def_type);
@@ -48,17 +53,17 @@ impl<'a> Convertible<PosContext<'a>, Vec<GlobalStatementPos>> for GlobalStatemen
4853
| Self::SubDeclaration(_, _)
4954
| Self::UserDefinedType(_) => Ok(vec![]),
5055
Self::FunctionImplementation(f) => on_function_implementation(f, ctx)
51-
.map(|f| vec![Self::FunctionImplementation(f).at_pos(ctx.pos())]),
56+
.map(|f| vec![Self::FunctionImplementation(f).at_pos(pos.clone())]),
5257
Self::SubImplementation(s) => on_sub_implementation(s, ctx)
53-
.map(|s| vec![Self::SubImplementation(s).at_pos(ctx.pos())]),
54-
Self::Statement(s) => on_statement(s, ctx),
58+
.map(|s| vec![Self::SubImplementation(s).at_pos(pos.clone())]),
59+
Self::Statement(s) => on_statement(s, ctx, pos.clone()),
5560
}
5661
}
5762
}
5863

5964
fn on_function_implementation(
6065
function_implementation: FunctionImplementation,
61-
ctx: &mut PosContext,
66+
ctx: &mut Context,
6267
) -> Result<FunctionImplementation, LintErrorPos> {
6368
let FunctionImplementation {
6469
name: Positioned {
@@ -92,7 +97,7 @@ fn on_function_implementation(
9297

9398
fn on_sub_implementation(
9499
sub_implementation: SubImplementation,
95-
ctx: &mut PosContext,
100+
ctx: &mut Context,
96101
) -> Result<SubImplementation, LintErrorPos> {
97102
let SubImplementation {
98103
name,
@@ -146,11 +151,12 @@ fn convert_block_hoisting_implicit_vars_and_pop_name_scope(
146151

147152
fn on_statement(
148153
statement: Statement,
149-
ctx: &mut PosContext,
154+
ctx: &mut Context,
155+
pos: Position,
150156
) -> Result<Vec<GlobalStatementPos>, LintErrorPos> {
151157
// a statement might be converted into multiple statements due to implicit vars
152-
let statements = vec![statement.at_pos(ctx.pos())];
153-
let statements = statements.convert(ctx)?;
158+
let statements = vec![statement.at_pos(pos)];
159+
let statements: Statements = statements.convert(ctx)?;
154160
Ok(statements
155161
.into_iter()
156162
.map(|statement_pos| statement_pos.map(GlobalStatement::Statement))

rusty_linter/src/converter/common/traits.rs

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,75 @@
11
use crate::converter::common::Context;
22
use crate::core::LintErrorPos;
33

4-
pub trait Convertible<C = Context, O = Self>: Sized {
5-
fn convert(self, ctx: &mut C) -> Result<O, LintErrorPos>;
6-
7-
fn convert_in<'a, ParentContext, U>(
8-
self,
9-
parent_ctx: &'a mut ParentContext,
10-
value: U,
11-
) -> Result<O, LintErrorPos>
12-
where
13-
C: FromParentContext<'a, ParentContext, U>,
14-
{
15-
let mut child_state = C::create_from_parent_context(parent_ctx, value);
16-
self.convert(&mut child_state)
4+
/// Convert from the current type into the target type O.
5+
/// By default, O is the same as the current type.
6+
pub trait Convertible<O = Self>: Sized {
7+
fn convert(self, ctx: &mut Context) -> Result<O, LintErrorPos>;
8+
}
9+
10+
// Blanket implementation for Option
11+
12+
impl<T, O> Convertible<Option<O>> for Option<T>
13+
where
14+
T: Convertible<O>,
15+
{
16+
fn convert(self, ctx: &mut Context) -> Result<Option<O>, LintErrorPos> {
17+
match self {
18+
Some(t) => t.convert(ctx).map(Some),
19+
None => Ok(None),
20+
}
21+
}
22+
}
23+
24+
// Blanket implementation for Vec
25+
26+
impl<T, O> Convertible<Vec<O>> for Vec<T>
27+
where
28+
T: Convertible<O>,
29+
{
30+
fn convert(self, ctx: &mut Context) -> Result<Vec<O>, LintErrorPos> {
31+
self.into_iter().map(|t| t.convert(ctx)).collect()
1732
}
33+
}
34+
35+
/// Convert from the current type into the target type O,
36+
/// using additional information in the value U.
37+
/// By default, O is the same as the current type.
38+
pub trait ConvertibleIn<U, O = Self>: Sized {
39+
fn convert_in(self, ctx: &mut Context, value: U) -> Result<O, LintErrorPos>;
1840

19-
fn convert_in_default<'a, ParentContext, U>(
20-
self,
21-
parent_ctx: &'a mut ParentContext,
22-
) -> Result<O, LintErrorPos>
41+
fn convert_in_default(self, ctx: &mut Context) -> Result<O, LintErrorPos>
2342
where
24-
C: FromParentContext<'a, ParentContext, U>,
2543
U: Default,
2644
{
27-
self.convert_in(parent_ctx, U::default())
45+
self.convert_in(ctx, U::default())
2846
}
2947
}
3048

31-
impl<C, T> Convertible<C> for Option<T>
49+
// Blanket implementation for Option
50+
51+
impl<U, T, O> ConvertibleIn<U, Option<O>> for Option<T>
3252
where
33-
T: Convertible<C, T>,
53+
T: ConvertibleIn<U, O>,
3454
{
35-
fn convert(self, ctx: &mut C) -> Result<Self, LintErrorPos> {
55+
fn convert_in(self, ctx: &mut Context, extra: U) -> Result<Option<O>, LintErrorPos> {
3656
match self {
37-
Some(t) => t.convert(ctx).map(Some),
57+
Some(t) => t.convert_in(ctx, extra).map(Some),
3858
None => Ok(None),
3959
}
4060
}
4161
}
4262

43-
impl<C, T> Convertible<C> for Vec<T>
63+
// Blanket implementation for Vec
64+
65+
impl<U, T, O> ConvertibleIn<U, Vec<O>> for Vec<T>
4466
where
45-
T: Convertible<C, T>,
67+
T: ConvertibleIn<U, O>,
68+
U: Clone,
4669
{
47-
fn convert(self, ctx: &mut C) -> Result<Self, LintErrorPos> {
48-
self.into_iter().map(|t| t.convert(ctx)).collect()
70+
fn convert_in(self, ctx: &mut Context, extra: U) -> Result<Vec<O>, LintErrorPos> {
71+
self.into_iter()
72+
.map(|t| t.convert_in(ctx, extra.clone()))
73+
.collect()
4974
}
5075
}
51-
52-
pub trait FromParentContext<'a, T, U> {
53-
fn create_from_parent_context(parent: &'a mut T, value: U) -> Self;
54-
}

rusty_linter/src/converter/common/types.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Contains simple types and type aliases.
2+
use rusty_common::{Position, Positioned};
23

34
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
45
pub enum DimContext {
@@ -10,6 +11,12 @@ pub enum DimContext {
1011
Redim,
1112
}
1213

14+
pub struct DimNameState {
15+
pub dim_context: DimContext,
16+
pub shared: bool,
17+
pub pos: Position,
18+
}
19+
1320
/// Indicates the context in which an expression is being resolved.
1421
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
1522
pub enum ExprContext {
@@ -26,3 +33,5 @@ pub enum ExprContext {
2633
/// Used in resolving left-side of property expressions
2734
ResolvingPropertyOwner,
2835
}
36+
37+
pub type ExprContextPos = Positioned<ExprContext>;

rusty_linter/src/converter/dim_rules/array_dimension.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::converter::common::Context;
22
use crate::converter::common::Convertible;
3+
use crate::converter::common::ConvertibleIn;
34
use crate::core::LintErrorPos;
45
use rusty_parser::ArrayDimension;
56

rusty_linter/src/converter/dim_rules/dim_list_state.rs

Lines changed: 0 additions & 39 deletions
This file was deleted.

rusty_linter/src/converter/dim_rules/dim_name_state.rs

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)