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

refactor: sync error with spec. #697

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion kclvm/error/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,19 @@ macro_rules! register_warnings {
// Error messages for EXXXX errors. Each message should start and end with a
// new line.
register_errors! {
// E1XXX Syntax Errors
E1001: ErrorKind::InvalidSyntax, include_str!("./error_codes/E1001.md"),
E1002: ErrorKind::TabError, include_str!("./error_codes/E1002.md"),
E1003: ErrorKind::IndentationError, include_str!("./error_codes/E1003.md"),
E1I37: ErrorKind::IllegalArgumentSyntax, include_str!("./error_codes/E1I37.md"),
// E2XXX Compile Errors
E2G22: ErrorKind::TypeError, include_str!("./error_codes/E2G22.md"),
E2F04: ErrorKind::CannotFindModule, include_str!("./error_codes/E2F04.md"),
E2L23: ErrorKind::CompileError, include_str!("./error_codes/E2L23.md"),
E2A31: ErrorKind::IllegalAttributeError, include_str!("./error_codes/E2A31.md"),
E2L28: ErrorKind::UniqueKeyError, include_str!("./error_codes/E2L28.md"),
E2D34: ErrorKind::IllegalInheritError, include_str!("./error_codes/E2D34.md"),
// E3XXX Runtime Errors
E3M38: ErrorKind::EvaluationError, include_str!("./error_codes/E2D34.md"),
}

Expand All @@ -63,9 +69,12 @@ pub struct Error {

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ErrorKind {
// Syntax Errors
InvalidSyntax,
TabError,
Indentation,
IndentationError,
IllegalArgumentSyntax,
// Compile Errors
CannotFindModule,
RecursiveLoad,
FloatOverflow,
Expand All @@ -83,6 +92,7 @@ pub enum ErrorKind {
ValueError,
KeyError,
AttributeError,
// Runtime Errors
AssertionError,
ImmutableError,
MultiInheritError,
Expand Down Expand Up @@ -129,6 +139,7 @@ pub struct Warning {
// Kind of KCL warning.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum WarningKind {
// Compile Warnings
CompilerWarning,
UnusedImportWarning,
ReimportWarning,
Expand Down
27 changes: 22 additions & 5 deletions kclvm/error/src/error_codes/E1001.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
### InvalidSyntaxError (E1001)

This error indicates that the compiler syntax error has occurred.
KCL will report InvalidSyntaxError when KCL has a syntax error.

Erroneous code example:
The error code of InvalidSyntaxError is E1001.

```kcl,E1001
x = f(
7 ^ -> Expected one of ['all', 'any', 'bin_number', 'dec_number', '**', 'False', 'filter', 'float_number','hex_number', 'lambda', '{', '[', '(', 'long_string', 'not', 'map', '-', '*', 'name', 'None', '~', 'oct_number', '+',')', 'string', 'True', 'Undefined']
For example:

```
a, b = 1, 2 # Multiple assign is illegal in KCL syntax
```

The KCL program will cause the following error message.

```kcl,e1001
error[E1001]: InvalidSyntax
--> /syntax_error/general/multiple_assign/case0/main.k:1:2
|
1 | a, b = 1, 2 # Multiple assign is illegal in KCL syntax
| ^ expected statement
|
```

Possible resolution:

Check and fix KCL syntax errors based on the KCL Language Standard
29 changes: 29 additions & 0 deletions kclvm/error/src/error_codes/E1002.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### KCLTabError (E1002)

KCL will report `KCLTabError` when KCL has a tab and white space syntax error.

In KCL, it is forbidden to mix tabs and four spaces in one indentation block. And we recommend only using white spaces or tabs for indentation in the entire KCL project, don’t mix them.

For example:

```python
schema Person:
name: str # begin with a tab
age: int # begin with four white spaces,
# and four white spaces != tab in the env
```

The KCL program will cause the following error message.

```shell
error[E1001]: InvalidSyntax
--> File /syntax_error/tab/tab_error_0/main.k:6:5
|
3 | age: int = 1
| ^ inconsistent use of tabs and spaces in indentation
|
```

Possible resolution:

- Only use a tab or four white spaces in KCL, do not mix them.
29 changes: 29 additions & 0 deletions kclvm/error/src/error_codes/E1003.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### IndentationError (E1003)

KCL will report `KCLIndentationError` when KCL has an indentation syntax error.

The KCL syntax includes indentation. A tab or four white spaces in KCL represents an indentation. The other cases will be regarded as syntax errors by KCL.

For example:

```python
schema Person:
name: str # a tab or four white spaces is legal.
age: int # three white spaces are illegal
info: str # two white spaces is illegal
```

The KCL program will cause the following error message.

```shell
error[E1001]: InvalidSyntax
--> /syntax_error/indent/indent_error_0/main.k:3:4
|
3 | age: int # three white spaces are illegal
| ^ unindent 3 does not match any outer indentation level
|
```

Possible resolution:

- Only use a tab or four white spaces in the KCL program for indentation.
28 changes: 28 additions & 0 deletions kclvm/error/src/error_codes/E1I37.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
### IllegalArgumentSyntaxError (E1I37)

KCL will report `IllegalArgumentSyntaxError` when KCL has an illegal argument in KCL syntax.

For example:

```python
# Parameters without default values
# must be in front of parameters with default values.
a = option(type="list", default={"key": "value"}, "key1")
```

The KCL program will cause the following error message.

```shell
error[E1001]: InvalidSyntax
--> /option/type_convert_fail_2/main.k:3:57
|
3 | a = option(type="list", default={"key": "value"}, "key1")
| ^ positional argument follows keyword argument
|
```

Possible resolution:

```python
func(input_1, ..., input_n, param_with_key_1 = input_with_key_1, ..., param_with_key_n = input_with_key_n)
```
10 changes: 6 additions & 4 deletions kclvm/error/src/error_codes/E2A31.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ This error indicates that the illegal attribute error has occurred.
Erroneous code example:

```kcl,E2A31
KCL Compile Error[E2A31] : Illegal attribute
1 |x = {None: None}
6 ^ -> Failure
type 'NoneType'
error[E2A31]: IllegalAttributeError
--> /path/to/file.k:1:6
|
1 | x = {None: None}
| ^ A attribute must be string type, got 'NoneType'
|
```
30 changes: 20 additions & 10 deletions kclvm/error/src/error_codes/E2D34.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
### IllegalInheritError (E2D34)

This error indicates that invalid inheritance structure has occurred.
KCL will report `IllegalInheritError` when an illegal inheritance occurs in the schema.

Erroneous code example:
The `ewcode` of `IllegalInheritError` is `E2D34`.

```kcl
For example:

```python
schema FullnameMixin:
fullName = "{} {}".format(firstName, lastName)

schema Scholar(FullnameMixin):
schema Scholar(FullnameMixin): # mixin inheritance is illegal
school: str
```

```kcl,E2D34
KCL Complier Error[E2D34] : Illegal inheritance
---> File /schema/inherit/inherit_mixin_fail/main.k:8:1
8 |schema Scholar(FullnameMixin):
1 ^ -> Failure
mixin inheritance FullnameMixin is prohibited
The KCL program will cause the following error message.

```shell
error[E2D34]: IllegalInheritError
--> /schema/inherit/inherit_mixin_fail/main.k:4:16
|
4 | schema Scholar(FullnameMixin):
| ^ invalid schema inherit object type, expect schema, got 'FullnameMixin'
|
```

Possible resolution:

- Schema supports single inheritance of schema in KCL.
26 changes: 20 additions & 6 deletions kclvm/error/src/error_codes/E2F04.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
## CannotFindModule (E2F04)

This error indicates that the import module is not found.
KCL will report `CannotFindModule` when KCL imports a module that does not exist.

Erroneous code example:
The `ewcode` of `CannotFindModule` is `E2F04`.

```kcl,E2F04
1 |import not_existed_pkg
1 ^^^^^^^^^^^^^^^^^^^^^^ -> Failure
Cannot find the module not_existed_pkg from ./not_existed_pkg
For example:

```python
import .some0.pkg1 as some00 # some0 not found in package

Name1 = some00.Name # some0.pkg1.name
```

The KCL program will cause the following error message.

```shell
error[E2F04]: CannotFindModule
--> import_abs_fail_0/app-main/main.k:1:1
|
1 | import .some0.pkg1 as some00 # some0 not found in package
| Cannot find the module .some0.pkg1
|
```
37 changes: 31 additions & 6 deletions kclvm/error/src/error_codes/E2G22.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
### TypeError (E2G22)

This error indicates that the compiler type error has occurred.
KCL will report `TypeError` when a type error occurs in compiling type check.

Erroneous code example:
The `ewcode` of `TypeError` is `E2G22`.

```kcl,E2G22
1 |a: int = "1"
1 ^ -> got str(1)
expected int, got str(1)
For example:

```python
schema Person:
firstName: str
lastName: int

JohnDoe = Person {
"firstName": "John",
"lastName": "Doe" # Type Error,lastName: int,“Doe” is a string.
}
```

The KCL program will cause the following error message.

```shell
error[E2G22]: TypeError
--> type/type_fail_0/main.k:7:5
|
7 | "lastName": "Doe" # Type Error,lastName: int,“Doe” is a string.
| ^ expected int, got str(Doe)
|

--> type/type_fail_0/main.k:3:5
|
3 | lastName: int
| ^ variable is defined here, its type is int, but got str(Doe)
|
```
34 changes: 34 additions & 0 deletions kclvm/error/src/error_codes/E2H13.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
### UnKnownDecoratorError (E2H13)

KCL will report `UnKnownDecoratorError` when an unknown decorator is used in KCL.

The `ewcode` of `UnKnownDecoratorError` is `E2H13`.

For example:

```python
@err_deprecated # It is an unknown decorator
schema Person:
firstName: str = "John"
lastName: str
name: str

JohnDoe = Person {
name: "deprecated"
}
```

The KCL program will cause the following error message.

```shell
error[E2L23]: CompileError
--> deprecated/unknown_fail_1/main.k:1:2
|
1 | @err_deprecated # This is a error decorator
| ^ UnKnown decorator err_deprecated
|
```

Possible resolution:

- Check whether the decorator exists.
37 changes: 28 additions & 9 deletions kclvm/error/src/error_codes/E2L28.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
### UniqueKeyError (E2L28)

This error indicates that variables with the same name or duplicate definitions.
KCL will report `UniqueKeyError` when duplicate names appear in the KCL code.

Erroneous code example:
The `ewcode` of `UniqueKeyError` is `E2L28`.

```kcl
For example:

```python
schema Person:
name: str = "kcl"
age: int = 1

schema Person:
aa: int

x0 = Person{}
x1 = Person{age:101}
```

```kcl,E2L28
KCL Complier Error[E2L28] : Unique key error
---> File /schema/same_name/main.k:5:1
5 |schema Person:
1 ^ -> Failure
Variable name 'Person' must be unique in package context
The KCL program will cause the following error message.

```shell
error[E2L28]: UniqueKeyError
--> /schema/same_name/main.k:5:8
|
5 | schema Person:
| ^ Unique key error name 'Person'
|

--> /schema/same_name/main.k:1:8
|
1 | schema Person:
| ^ The variable 'Person' is declared here
|
```

Possible resolution:

- Check if the name with error has been used.
Loading