-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Fix shadowing struct types by struct member names #10908
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| pragma abicoder v2; | ||
|
|
||
| contract C { | ||
| enum EnumType {A, B, C} | ||
|
|
||
| struct StructType { | ||
| uint x; | ||
| } | ||
|
|
||
| error E1(StructType StructType); | ||
| error E2(EnumType StructType, StructType EnumType); | ||
|
|
||
| function f() public { | ||
| revert E1({StructType: StructType(42)}); | ||
| } | ||
|
|
||
| function g() public { | ||
| revert E2({EnumType: StructType(42), StructType: EnumType.B}); | ||
| } | ||
| } | ||
| // ==== | ||
| // compileToEwasm: also | ||
| // compileViaYul: also | ||
| // ---- | ||
| // f() -> FAILURE, hex"33a54193", hex"000000000000000000000000000000000000000000000000000000000000002a" | ||
| // g() -> FAILURE, hex"374b9387", hex"0000000000000000000000000000000000000000000000000000000000000001", hex"000000000000000000000000000000000000000000000000000000000000002a" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| contract C { | ||
| struct S { | ||
| uint x; | ||
| } | ||
|
|
||
| enum E {E, S, C, a, f} | ||
|
|
||
| uint a; | ||
|
|
||
| function f() public pure {} | ||
| } | ||
| // ---- | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this issue a compiler error? Because enum member shadows type name?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, because shadowing is legal. It's only when you shadow type names used later within the same scope that it's an error: {
Type1 Type2 = 1;
Type2 x = 2;
}You cannot do that in an enum. I added this test mostly for completeness. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| contract C { | ||
| error E(int bytes, bytes x); | ||
| } | ||
| // ---- | ||
| // ParserError 2314: (29-34): Expected ',' but got 'bytes' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| contract C { | ||
| enum EnumType {A, B, C} | ||
|
|
||
| struct StructType { | ||
| uint x; | ||
| } | ||
|
|
||
| error E1(StructType StructType); | ||
cameel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| error E2(EnumType EnumType); | ||
| error E3(EnumType StructType, StructType EnumType); | ||
| } | ||
| // ---- | ||
cameel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| contract C { | ||
| enum EnumType {A, B, C} | ||
|
|
||
| struct StructType { | ||
| uint x; | ||
| } | ||
|
|
||
| event E1(StructType StructType); | ||
| event E2(EnumType EnumType); | ||
| event E3(EnumType StructType, StructType EnumType); | ||
| event E4(StructType indexed StructType) anonymous; | ||
| } | ||
| // ---- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| interface I { | ||
| function f(uint I) external; // OK | ||
| } | ||
|
|
||
| library L { | ||
| function f(uint L) public pure {} // warning | ||
| } | ||
|
|
||
| abstract contract A { | ||
| function f(uint A) public pure {} // warning | ||
| function g(uint A) public virtual; // OK | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this "OK"? Shadowing still exists right?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #10908 (comment). Having a parameter named like a type inside a function declaration that has no body cannot break anything so we do not consider that shadowing. It's clear what it's referring to. You don't want to warn about it and annoy the user. When there is a body, on the other hand, warning makes sense. |
||
| } | ||
|
|
||
| contract C { | ||
| function f(uint C) public pure {} // warning | ||
| } | ||
| // ---- | ||
| // Warning 2519: (91-97): This declaration shadows an existing declaration. | ||
| // Warning 2519: (168-174): This declaration shadows an existing declaration. | ||
| // Warning 2519: (283-289): This declaration shadows an existing declaration. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| contract C { | ||
| function f(uint f) pure public {} | ||
| } | ||
| // ---- | ||
| // Warning 2519: (28-34): This declaration shadows an existing declaration. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| contract C { | ||
| enum EnumType {A, B, C} | ||
|
|
||
| struct StructType { | ||
| uint x; | ||
| } | ||
|
|
||
| function f(StructType memory StructType) external {} | ||
| function g(EnumType EnumType) external {} | ||
| function h(EnumType StructType, StructType memory EnumType) external {} | ||
|
|
||
| function z(EnumType e) external returns (uint EnumType) {} | ||
| } | ||
| // ---- | ||
| // Warning 2519: (104-132): This declaration shadows an existing declaration. | ||
| // Warning 2519: (161-178): This declaration shadows an existing declaration. | ||
| // Warning 2519: (207-226): This declaration shadows an existing declaration. | ||
| // Warning 2519: (228-254): This declaration shadows an existing declaration. | ||
| // Warning 2519: (314-327): This declaration shadows an existing declaration. | ||
| // TypeError 5172: (104-114): Name has to refer to a struct, enum or contract. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| library C { | ||
| enum EnumType {A, B, C} | ||
|
|
||
| struct StructType { | ||
| uint x; | ||
| } | ||
|
|
||
| function f1(function (StructType memory StructType) external f) external {} | ||
| function f2(function (EnumType EnumType) external f) external {} | ||
| function f3(function (EnumType StructType, StructType memory EnumType) external f) external {} | ||
| } | ||
| // ---- | ||
| // Warning 6162: (114-142): Naming function type parameters is deprecated. | ||
| // Warning 6162: (194-211): Naming function type parameters is deprecated. | ||
| // Warning 6162: (263-282): Naming function type parameters is deprecated. | ||
| // Warning 6162: (284-310): Naming function type parameters is deprecated. | ||
| // Warning 2519: (114-142): This declaration shadows an existing declaration. | ||
| // Warning 2519: (194-211): This declaration shadows an existing declaration. | ||
| // Warning 2519: (263-282): This declaration shadows an existing declaration. | ||
| // Warning 2519: (284-310): This declaration shadows an existing declaration. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| contract C { | ||
| enum EnumType {A, B, C} | ||
|
|
||
| struct StructType { | ||
| uint x; | ||
| } | ||
|
|
||
| function f() external returns (StructType memory StructType) {} | ||
| function g() external returns (EnumType EnumType) {} | ||
| function h() external returns (EnumType StructType, StructType memory EnumType) {} | ||
|
|
||
| function z(uint EnumType) external returns (EnumType e) {} | ||
| } | ||
| // ---- | ||
| // Warning 2519: (124-152): This declaration shadows an existing declaration. | ||
| // Warning 2519: (192-209): This declaration shadows an existing declaration. | ||
| // Warning 2519: (249-268): This declaration shadows an existing declaration. | ||
| // Warning 2519: (270-296): This declaration shadows an existing declaration. | ||
| // Warning 2519: (317-330): This declaration shadows an existing declaration. | ||
| // TypeError 5172: (124-134): Name has to refer to a struct, enum or contract. | ||
|
Comment on lines
+1
to
+20
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| contract C { | ||
| enum EnumType {A, B, C} | ||
|
|
||
| struct StructType { | ||
| uint x; | ||
| } | ||
|
|
||
| function (StructType memory StructType) external ext1; | ||
| function (EnumType EnumType) external ext2; | ||
| function (EnumType StructType, StructType memory EnumType) external ext3; | ||
| } | ||
| // ---- | ||
| // Warning 6162: (103-131): Naming function type parameters is deprecated. | ||
| // Warning 6162: (162-179): Naming function type parameters is deprecated. | ||
| // Warning 6162: (210-229): Naming function type parameters is deprecated. | ||
| // Warning 6162: (231-257): Naming function type parameters is deprecated. | ||
| // Warning 2519: (103-131): This declaration shadows an existing declaration. | ||
| // Warning 2519: (162-179): This declaration shadows an existing declaration. | ||
| // Warning 2519: (210-229): This declaration shadows an existing declaration. | ||
| // Warning 2519: (231-257): This declaration shadows an existing declaration. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| contract C { | ||
| enum EnumType {A, B, C} | ||
|
|
||
| struct StructType { | ||
| uint x; | ||
| } | ||
|
|
||
| function () external returns (StructType memory StructType) ext1; | ||
| function () external returns (EnumType EnumType) ext2; | ||
| function () external returns (EnumType StructType, StructType memory EnumType) ext3; | ||
| } | ||
| // ---- | ||
| // SyntaxError 7304: (123-151): Return parameters in function types may not be named. | ||
| // SyntaxError 7304: (193-210): Return parameters in function types may not be named. | ||
| // SyntaxError 7304: (252-271): Return parameters in function types may not be named. | ||
| // SyntaxError 7304: (273-299): Return parameters in function types may not be named. | ||
| // Warning 2519: (123-151): This declaration shadows an existing declaration. | ||
| // Warning 2519: (193-210): This declaration shadows an existing declaration. | ||
| // Warning 2519: (252-271): This declaration shadows an existing declaration. | ||
| // Warning 2519: (273-299): This declaration shadows an existing declaration. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,4 +3,4 @@ contract C { | |
| function f(function(S memory) external) public {} | ||
| } | ||
| // ---- | ||
| // TypeError 5172: (25-26): Name has to refer to a struct, enum or contract. | ||
| // DeclarationError 7920: (25-26): Identifier not found or not unique. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't type error the correct error to output here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be, but then it would be the only type error in |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| contract C { | ||
| enum E {a, b, c} | ||
| struct S {E X; uint E;} | ||
| struct T {E T; uint E;} | ||
| struct U {E E;} | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this test pass?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to my understanding, yes, because we allow struct members to have same names as types. |
||
Uh oh!
There was an error while loading. Please reload this page.