From f10be0f13e7ae4ff84e39a2f971046d80b22f4d5 Mon Sep 17 00:00:00 2001 From: peefy Date: Tue, 12 Sep 2023 12:50:36 +0800 Subject: [PATCH] feat: enhance config attr type checking --- kclvm/sema/src/resolver/config.rs | 7 +++++++ test/grammar/types/config/config_ty_0/main.k | 14 ++++++++++++++ .../grammar/types/config/config_ty_0/stdout.golden | 7 +++++++ test/grammar/types/config/config_ty_1/main.k | 14 ++++++++++++++ .../grammar/types/config/config_ty_1/stdout.golden | 7 +++++++ test/grammar/types/config/config_ty_2/main.k | 12 ++++++++++++ .../grammar/types/config/config_ty_2/stdout.golden | 9 +++++++++ test/grammar/types/config/config_ty_3/main.k | 12 ++++++++++++ .../grammar/types/config/config_ty_3/stdout.golden | 8 ++++++++ 9 files changed, 90 insertions(+) create mode 100644 test/grammar/types/config/config_ty_0/main.k create mode 100644 test/grammar/types/config/config_ty_0/stdout.golden create mode 100644 test/grammar/types/config/config_ty_1/main.k create mode 100644 test/grammar/types/config/config_ty_1/stdout.golden create mode 100644 test/grammar/types/config/config_ty_2/main.k create mode 100644 test/grammar/types/config/config_ty_2/stdout.golden create mode 100644 test/grammar/types/config/config_ty_3/main.k create mode 100644 test/grammar/types/config/config_ty_3/stdout.golden diff --git a/kclvm/sema/src/resolver/config.rs b/kclvm/sema/src/resolver/config.rs index d3960ec50..14a99f94c 100644 --- a/kclvm/sema/src/resolver/config.rs +++ b/kclvm/sema/src/resolver/config.rs @@ -478,6 +478,13 @@ impl<'ctx> Resolver<'ctx> { doc: None, }, ); + attrs.insert( + string_lit.value.clone(), + Attr { + ty: val_ty.clone(), + range: key.get_span_pos(), + }, + ); } key_types.push(key_ty); val_types.push(val_ty.clone()); diff --git a/test/grammar/types/config/config_ty_0/main.k b/test/grammar/types/config/config_ty_0/main.k new file mode 100644 index 000000000..377e25e8d --- /dev/null +++ b/test/grammar/types/config/config_ty_0/main.k @@ -0,0 +1,14 @@ +config: {str:} = { + "key" = [ + { + key = "value" + } + ] + "values": [ + "foo" + "bar" + ] +} + +if "foo" in config.values: + a = 1 diff --git a/test/grammar/types/config/config_ty_0/stdout.golden b/test/grammar/types/config/config_ty_0/stdout.golden new file mode 100644 index 000000000..5868ff014 --- /dev/null +++ b/test/grammar/types/config/config_ty_0/stdout.golden @@ -0,0 +1,7 @@ +config: + key: + - key: value + values: + - foo + - bar +a: 1 diff --git a/test/grammar/types/config/config_ty_1/main.k b/test/grammar/types/config/config_ty_1/main.k new file mode 100644 index 000000000..ea3920300 --- /dev/null +++ b/test/grammar/types/config/config_ty_1/main.k @@ -0,0 +1,14 @@ +config: {str:} = { + key = [ + { + key = "value" + } + ] + values: [ + "foo" + "bar" + ] +} + +if "foo" in config.values: + a = 1 diff --git a/test/grammar/types/config/config_ty_1/stdout.golden b/test/grammar/types/config/config_ty_1/stdout.golden new file mode 100644 index 000000000..5868ff014 --- /dev/null +++ b/test/grammar/types/config/config_ty_1/stdout.golden @@ -0,0 +1,7 @@ +config: + key: + - key: value + values: + - foo + - bar +a: 1 diff --git a/test/grammar/types/config/config_ty_2/main.k b/test/grammar/types/config/config_ty_2/main.k new file mode 100644 index 000000000..f93e476a1 --- /dev/null +++ b/test/grammar/types/config/config_ty_2/main.k @@ -0,0 +1,12 @@ +config: {str:} = { + "key" = [ + { + key = "value" + } + ] + "values" = [ + "foo" + "bar" + ] +} +values: [str] = config.values diff --git a/test/grammar/types/config/config_ty_2/stdout.golden b/test/grammar/types/config/config_ty_2/stdout.golden new file mode 100644 index 000000000..e5349a781 --- /dev/null +++ b/test/grammar/types/config/config_ty_2/stdout.golden @@ -0,0 +1,9 @@ +config: + key: + - key: value + values: + - foo + - bar +values: +- foo +- bar diff --git a/test/grammar/types/config/config_ty_3/main.k b/test/grammar/types/config/config_ty_3/main.k new file mode 100644 index 000000000..7e1a11194 --- /dev/null +++ b/test/grammar/types/config/config_ty_3/main.k @@ -0,0 +1,12 @@ +config: {str:} = { + "map" = [ + { + key = "value" + } + ] + "values" = [ + "foo" + "bar" + ] +} +$map: [{str:str}] = config.map diff --git a/test/grammar/types/config/config_ty_3/stdout.golden b/test/grammar/types/config/config_ty_3/stdout.golden new file mode 100644 index 000000000..cf7c1c5e8 --- /dev/null +++ b/test/grammar/types/config/config_ty_3/stdout.golden @@ -0,0 +1,8 @@ +config: + map: + - key: value + values: + - foo + - bar +map: +- key: value