Skip to content

Add syntax error detection routine for global symbol#2903

Merged
youknowone merged 2 commits into
RustPython:masterfrom
Snowapril:fix-syntax-test
Aug 17, 2021
Merged

Add syntax error detection routine for global symbol#2903
youknowone merged 2 commits into
RustPython:masterfrom
Snowapril:fix-syntax-test

Conversation

@Snowapril

@Snowapril Snowapril commented Aug 17, 2021

Copy link
Copy Markdown
Contributor

This revision fixes test_global_parap_err_first test in
test_syntax.py and implement the other syntax errors detection as cpython 3.8 provided for global symbol.

Below syntax error conditions are added.

  • name {} is parameter and global
>>>>> def test(a):
.....   global a
.....
SyntaxError: name 'a' is parameter and global at line 2 column 2
        global a
  • annotated name {} can't be global
>>>>> def test():
.....   a: int
.....   global a
.....
SyntaxError: annotated name 'a' can't be global at line 3 column 2
        global a
  • name {} is assigned to before global description
>>>>> a = 100
>>>>> def test():
.....   a = 10
.....   global a
.....
SyntaxError: name 'a' is assigned to before global declaration at line 3 column 2
        global a
  • name {} is used prior to global declaration
>>>>> a = 10
>>>>> def test():
.....   print(a)
.....   global a
.....
SyntaxError: name 'a' is used prior to global declaration at line 3 column 2
        global a

This commit fixes `test_global_parap_err_first` test in
[test_syntax.py](https://github.com/RustPython/RustPython/blob/master/Lib/test/test_syntax.py#L678) and implement the other syntax errors detection as cpython 3.8 provided for global symbol.

Below syntax error conditions are added.
* name {} is parameter and global
```python
>>>>> def test(a):
.....   global a
.....
SyntaxError: name 'a' is parameter and global at line 2 column 2
        global a
```
* annotated name {} can't be global
```python
>>>>> def test():
.....   a: int
.....   global a
.....
SyntaxError: annotated name 'a' can't be global at line 3 column 2
        global a
```
* name {} is assigned to before global description
```python
>>>>> a = 100
>>>>> def test():
.....   a = 10
.....   global a
.....
SyntaxError: name 'a' is assigned to before global declaration at line 3 column 2
        global a
```
* name {} is used prior to global declaration
```python
>>>>> a = 10
>>>>> def test():
.....   print(a)
.....   global a
.....
SyntaxError: name 'a' is used prior to global declaration at line 3 column 2
        global a
```

Signed-off-by: snowapril <sinjihng@gmail.com>
// Role already set..
match role {
SymbolUsage::Global => {
if !symbol.is_global() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's keep this if statement on the top of these errors. it will decrease hot path cost.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok! I agree with that. I sent v2 code.

add `!symbol.is_global()` condition check before entrying global symbol
errors

This change will decrease hot path cost.

Signed-off-by: snowapril <sinjihng@gmail.com>
@youknowone youknowone merged commit 1d89e63 into RustPython:master Aug 17, 2021
@youknowone youknowone added the z-ca-2021 Tag to track contrubution-academy 2021 label Oct 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

z-ca-2021 Tag to track contrubution-academy 2021

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants