Skip to content

Commit bbf0a75

Browse files
authored
Merge pull request #1611 from taichi-ishitani/refactor_type_check_for_generic_arg
Referctor type check for generic argument
2 parents 6fdb653 + 4ba05b1 commit bbf0a75

File tree

9 files changed

+422
-171
lines changed

9 files changed

+422
-171
lines changed

crates/analyzer/src/handlers/check_type.rs

+253-149
Large diffs are not rendered by default.

crates/analyzer/src/symbol_path.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,8 @@ impl From<&syntax_tree::BooleanLiteral> for GenericSymbolPath {
580580
impl From<&syntax_tree::WithGenericArgumentItem> for GenericSymbolPath {
581581
fn from(value: &syntax_tree::WithGenericArgumentItem) -> Self {
582582
match value {
583-
syntax_tree::WithGenericArgumentItem::ScopedIdentifier(x) => {
584-
x.scoped_identifier.as_ref().into()
583+
syntax_tree::WithGenericArgumentItem::ExpressionIdentifier(x) => {
584+
x.expression_identifier.as_ref().into()
585585
}
586586
syntax_tree::WithGenericArgumentItem::FixedType(x) => x.fixed_type.as_ref().into(),
587587
syntax_tree::WithGenericArgumentItem::Number(x) => x.number.as_ref().into(),
@@ -661,6 +661,26 @@ impl From<&syntax_tree::ScopedIdentifier> for GenericSymbolPath {
661661
}
662662
}
663663

664+
impl From<&syntax_tree::ExpressionIdentifier> for GenericSymbolPath {
665+
fn from(value: &syntax_tree::ExpressionIdentifier) -> Self {
666+
let mut path: GenericSymbolPath = value.scoped_identifier.as_ref().into();
667+
668+
for base in value
669+
.expression_identifier_list0
670+
.iter()
671+
.map(|x| x.identifier.identifier_token.token)
672+
{
673+
path.paths.push(GenericSymbol {
674+
base,
675+
arguments: vec![],
676+
});
677+
}
678+
679+
path.range = value.into();
680+
path
681+
}
682+
}
683+
664684
impl fmt::Display for GenericSymbolPath {
665685
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
666686
let mut text = String::new();

crates/analyzer/src/tests.rs

+121-1
Original file line numberDiff line numberDiff line change
@@ -2641,7 +2641,15 @@ fn mismatch_type() {
26412641
struct Foo {
26422642
foo: logic,
26432643
}
2644+
struct Bar {
2645+
bar: logic,
2646+
}
2647+
const BAR: Bar = Bar'{ bar: 0 };
2648+
26442649
function Func::<foo: Foo> {}
2650+
always_comb {
2651+
Func::<BAR>();
2652+
}
26452653
}
26462654
"#;
26472655

@@ -2653,7 +2661,15 @@ fn mismatch_type() {
26532661
union Foo {
26542662
foo: logic,
26552663
}
2664+
union Bar {
2665+
bar: logic,
2666+
}
2667+
const BAR: Bar = Bar'{ bar: 0 };
2668+
26562669
function Func::<foo: Foo> {}
2670+
always_comb {
2671+
Func::<BAR>();
2672+
}
26572673
}
26582674
"#;
26592675

@@ -2665,7 +2681,6 @@ fn mismatch_type() {
26652681
enum Foo {
26662682
FOO
26672683
}
2668-
26692684
enum Bar {
26702685
BAR
26712686
}
@@ -2680,6 +2695,26 @@ fn mismatch_type() {
26802695
let errors = analyze(code);
26812696
assert!(matches!(errors[0], AnalyzerError::MismatchType { .. }));
26822697

2698+
let code = r#"
2699+
module ModuleA {
2700+
enum Foo {
2701+
FOO
2702+
}
2703+
enum Bar {
2704+
BAR
2705+
}
2706+
const BAR: Bar = Bar::BAR;
2707+
2708+
function Func::<foo: Foo> {}
2709+
always_comb {
2710+
Func::<BAR>();
2711+
}
2712+
}
2713+
"#;
2714+
2715+
let errors = analyze(code);
2716+
assert!(matches!(errors[0], AnalyzerError::MismatchType { .. }));
2717+
26832718
let code = r#"
26842719
proto module ProtoModuleA;
26852720
module ModuleB::<M: ProtoModuleA> {
@@ -5581,6 +5616,91 @@ fn unresolvable_generic_argument() {
55815616
errors[0],
55825617
AnalyzerError::UnresolvableGenericArgument { .. }
55835618
));
5619+
5620+
let code = r#"
5621+
package Pkg {
5622+
function Func::<T: type> {}
5623+
}
5624+
module ModuleA {
5625+
type MyType = logic;
5626+
always_comb {
5627+
Pkg::Func::<MyType>();
5628+
}
5629+
}
5630+
"#;
5631+
5632+
let errors = analyze(code);
5633+
// This pattern also causes CyclicTypeDependency error
5634+
assert!(
5635+
errors
5636+
.iter()
5637+
.any(|e| matches!(e, AnalyzerError::UnresolvableGenericArgument { .. }))
5638+
);
5639+
5640+
let code = r#"
5641+
package Pkg {
5642+
function Func::<V: u32> {}
5643+
}
5644+
module ModuleA {
5645+
const V: u32 = 0;
5646+
always_comb {
5647+
Pkg::Func::<V>();
5648+
}
5649+
}
5650+
"#;
5651+
5652+
let errors = analyze(code);
5653+
// This pattern also causes CyclicTypeDependency error
5654+
assert!(
5655+
errors
5656+
.iter()
5657+
.any(|e| matches!(e, AnalyzerError::UnresolvableGenericArgument { .. }))
5658+
);
5659+
5660+
let code = r#"
5661+
module ModuleA {
5662+
struct Foo {
5663+
foo: logic,
5664+
}
5665+
let foo: Foo = Foo'{ foo: 0 };
5666+
5667+
function Func::<foo: Foo> {}
5668+
always_comb {
5669+
Func::<foo>();
5670+
}
5671+
}
5672+
"#;
5673+
5674+
let errors = analyze(code);
5675+
assert!(matches!(
5676+
errors[0],
5677+
AnalyzerError::UnresolvableGenericArgument { .. }
5678+
));
5679+
5680+
let code = r#"
5681+
package PkgA {
5682+
struct Baz {
5683+
baz: logic,
5684+
}
5685+
struct Bar {
5686+
bar: Baz
5687+
}
5688+
function Func::<baz: Baz> {}
5689+
}
5690+
package PkgB {
5691+
import PkgA::*;
5692+
const FOO: Bar = Bar'{ bar: Baz'{ baz: 1 } };
5693+
}
5694+
module ModuleA {
5695+
import PkgB::*;
5696+
always_comb {
5697+
PkgA::Func::<PkgB::FOO.bar>();
5698+
}
5699+
}
5700+
"#;
5701+
5702+
let errors = analyze(code);
5703+
assert!(errors.is_empty());
55845704
}
55855705

55865706
#[test]

crates/parser/src/generated/veryl-exp.par

+1-1
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@
876876
/* 859 */ WithGenericArgumentListList /* Vec<T>::New */: ;
877877
/* 860 */ WithGenericArgumentListOpt /* Option<T>::Some */: Comma;
878878
/* 861 */ WithGenericArgumentListOpt /* Option<T>::None */: ;
879-
/* 862 */ WithGenericArgumentItem: ScopedIdentifier;
879+
/* 862 */ WithGenericArgumentItem: ExpressionIdentifier;
880880
/* 863 */ WithGenericArgumentItem: FixedType;
881881
/* 864 */ WithGenericArgumentItem: Number;
882882
/* 865 */ WithGenericArgumentItem: BooleanLiteral;

crates/parser/src/generated/veryl_grammar_trait.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -4651,13 +4651,13 @@ pub struct GenericProtoBoundFixedType {
46514651
///
46524652
/// Type derived for production 862
46534653
///
4654-
/// `WithGenericArgumentItem: ScopedIdentifier;`
4654+
/// `WithGenericArgumentItem: ExpressionIdentifier;`
46554655
///
46564656
#[allow(dead_code)]
46574657
#[derive(Builder, Debug, Clone)]
46584658
#[builder(crate = "parol_runtime::derive_builder")]
4659-
pub struct WithGenericArgumentItemScopedIdentifier {
4660-
pub scoped_identifier: Box<ScopedIdentifier>,
4659+
pub struct WithGenericArgumentItemExpressionIdentifier {
4660+
pub expression_identifier: Box<ExpressionIdentifier>,
46614661
}
46624662

46634663
///
@@ -13687,7 +13687,7 @@ pub struct WithGenericArgument {
1368713687
#[allow(dead_code)]
1368813688
#[derive(Debug, Clone)]
1368913689
pub enum WithGenericArgumentItem {
13690-
ScopedIdentifier(WithGenericArgumentItemScopedIdentifier),
13690+
ExpressionIdentifier(WithGenericArgumentItemExpressionIdentifier),
1369113691
FixedType(WithGenericArgumentItemFixedType),
1369213692
Number(WithGenericArgumentItemNumber),
1369313693
BooleanLiteral(WithGenericArgumentItemBooleanLiteral),
@@ -34057,21 +34057,22 @@ impl<'t, 'u> VerylGrammarAuto<'t, 'u> {
3405734057

3405834058
/// Semantic action for production 862:
3405934059
///
34060-
/// `WithGenericArgumentItem: ScopedIdentifier;`
34060+
/// `WithGenericArgumentItem: ExpressionIdentifier;`
3406134061
///
3406234062
#[parol_runtime::function_name::named]
3406334063
fn with_generic_argument_item_0(
3406434064
&mut self,
34065-
_scoped_identifier: &ParseTreeType<'t>,
34065+
_expression_identifier: &ParseTreeType<'t>,
3406634066
) -> Result<()> {
3406734067
let context = function_name!();
3406834068
trace!("{}", self.trace_item_stack(context));
34069-
let scoped_identifier = pop_item!(self, scoped_identifier, ScopedIdentifier, context);
34070-
let with_generic_argument_item_0_built = WithGenericArgumentItemScopedIdentifier {
34071-
scoped_identifier: Box::new(scoped_identifier),
34069+
let expression_identifier =
34070+
pop_item!(self, expression_identifier, ExpressionIdentifier, context);
34071+
let with_generic_argument_item_0_built = WithGenericArgumentItemExpressionIdentifier {
34072+
expression_identifier: Box::new(expression_identifier),
3407234073
};
3407334074
let with_generic_argument_item_0_built =
34074-
WithGenericArgumentItem::ScopedIdentifier(with_generic_argument_item_0_built);
34075+
WithGenericArgumentItem::ExpressionIdentifier(with_generic_argument_item_0_built);
3407534076
// Calling user action here
3407634077
self.user_grammar
3407734078
.with_generic_argument_item(&with_generic_argument_item_0_built)?;

crates/parser/src/generated/veryl_parser.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -22768,7 +22768,7 @@ pub const LOOKAHEAD_AUTOMATA: &[LookaheadDFA; 750] = &[
2276822768
Trans(23, 26, 33, -1),
2276922769
Trans(23, 27, 33, -1),
2277022770
Trans(23, 31, 60, -1),
22771-
Trans(23, 32, 61, -1),
22771+
Trans(23, 32, 71, -1),
2277222772
Trans(23, 33, 36, -1),
2277322773
Trans(23, 34, 36, -1),
2277422774
Trans(23, 35, 34, -1),
@@ -22779,7 +22779,7 @@ pub const LOOKAHEAD_AUTOMATA: &[LookaheadDFA; 750] = &[
2277922779
Trans(23, 42, 145, -1),
2278022780
Trans(23, 43, 36, -1),
2278122781
Trans(23, 44, 72, -1),
22782-
Trans(23, 45, 62, -1),
22782+
Trans(23, 45, 73, -1),
2278322783
Trans(23, 46, 146, -1),
2278422784
Trans(23, 47, 64, -1),
2278522785
Trans(23, 48, 65, -1),
@@ -31493,11 +31493,17 @@ pub const LOOKAHEAD_AUTOMATA: &[LookaheadDFA; 750] = &[
3149331493
Trans(4, 5, 3, 858),
3149431494
Trans(4, 30, 3, 858),
3149531495
Trans(4, 32, 3, 858),
31496+
Trans(4, 35, 3, 858),
31497+
Trans(4, 38, 3, 858),
31498+
Trans(4, 43, 3, 858),
3149631499
Trans(4, 45, 3, 858),
3149731500
Trans(5, 5, 3, 858),
3149831501
Trans(5, 29, 3, 858),
3149931502
Trans(5, 30, 3, 858),
3150031503
Trans(5, 32, 3, 858),
31504+
Trans(5, 35, 3, 858),
31505+
Trans(5, 38, 3, 858),
31506+
Trans(5, 43, 3, 858),
3150131507
Trans(5, 45, 3, 858),
3150231508
Trans(6, 7, 3, 858),
3150331509
Trans(6, 8, 3, 858),
@@ -37330,10 +37336,10 @@ pub const PRODUCTIONS: &[Production; 1096] = &[
3733037336
lhs: 731,
3733137337
production: &[],
3733237338
},
37333-
// 862 - WithGenericArgumentItem: ScopedIdentifier;
37339+
// 862 - WithGenericArgumentItem: ExpressionIdentifier;
3733437340
Production {
3733537341
lhs: 728,
37336-
production: &[ParseType::N(613)],
37342+
production: &[ParseType::N(219)],
3733737343
},
3733837344
// 863 - WithGenericArgumentItem: FixedType;
3733937345
Production {

crates/parser/src/token_range.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ impl_token_range!(WithGenericArgument, colon_colon_l_angle, r_angle);
11251125
impl_token_range_list!(WithGenericArgumentList, WithGenericArgumentItem);
11261126
impl_token_range_enum!(
11271127
WithGenericArgumentItem,
1128-
scoped_identifier,
1128+
expression_identifier,
11291129
fixed_type,
11301130
number,
11311131
boolean_literal

crates/parser/src/veryl_walker.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2663,8 +2663,8 @@ pub trait VerylWalker {
26632663
fn with_generic_argument_item(&mut self, arg: &WithGenericArgumentItem) {
26642664
before!(self, with_generic_argument_item, arg);
26652665
match arg {
2666-
WithGenericArgumentItem::ScopedIdentifier(x) => {
2667-
self.scoped_identifier(&x.scoped_identifier);
2666+
WithGenericArgumentItem::ExpressionIdentifier(x) => {
2667+
self.expression_identifier(&x.expression_identifier);
26682668
}
26692669
WithGenericArgumentItem::FixedType(x) => {
26702670
self.fixed_type(&x.fixed_type);

crates/parser/veryl.par

+1-1
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ WithGenericArgument: ColonColonLAngle %push(Generic) [ WithGenericArgumentList ]
795795

796796
WithGenericArgumentList: WithGenericArgumentItem { Comma WithGenericArgumentItem } [ Comma ];
797797

798-
WithGenericArgumentItem: ScopedIdentifier
798+
WithGenericArgumentItem: ExpressionIdentifier
799799
| FixedType
800800
| Number
801801
| BooleanLiteral

0 commit comments

Comments
 (0)