Skip to content

Commit

Permalink
Add tests for recursive const definitions and associated consts usa…
Browse files Browse the repository at this point in the history
…ges (#6545)

## Description

This PR adds additional tests for constants. Those test cases are
important for the implementation of #6351 which will completely
restructure compilation of constants.

Tests for recursive `const` definitions should also be considered before
we start implementing `const fn` and `const trait`. In particular, we
want to better track and provide precise error messages in case of
unintended attempt to recursively define a constant in a complex
situation that includes `const fn` and `const trait`.

Related to #6534, #6537, #6538, #6539, #6540, #6543, and #6544.

## Checklist

- [x] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
  • Loading branch information
ironcev authored Sep 14, 2024
1 parent 7ab1565 commit e1a05fc
Show file tree
Hide file tree
Showing 48 changed files with 353 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
script;
library;

fn main() -> u64 {
const X;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ category = "fail"

# check: $()const X;
# nextln: $()Constant requires expression.

# check: $()1 error.
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ category = "fail"

# check: $()const X;
# nextln: $()Constant requires expression.

# check: $()1 error.
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ category = "fail"
# check: $()Function d is recursive via e and f, which is unsupported at this time.
# check: $()Function e is recursive via f and d, which is unsupported at this time.
# check: $()Function f is recursive via d and e, which is unsupported at this time.

# check: $()6 errors.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = "recursive_const_associated"
source = "member"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
name = "recursive_const_associated"
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
library;

struct S {}

// TODO: Uncomment this code, and enable this test and add checks once https://github.com/FuelLabs/sway/issues/6537 is fixed.
impl S {
// const A: u8 = Self::B;
// const B: u8 = Self::A;

// const X: u8 = Self::Y;
// const Y: u8 = Self::Z;
// const Z: u8 = Self::X;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
category = "disabled" # TODO: Enable this test and add checks once https://github.com/FuelLabs/sway/issues/6537 is fixed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = "recursive_const_associated_over_associated_function"
source = "member"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
name = "recursive_const_associated_over_associated_function"
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
library;

struct S {}

impl S {
fn assoc() -> u8 {
Self::S_ASSOC
}
}

impl S {
const S_ASSOC: u8 = Self::assoc();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
category = "fail"

#check: $()Self::S_ASSOC
#nextln: $()Could not find symbol "S_ASSOC" in this scope.

#check: $()const S_ASSOC: u8 = Self::assoc();
#nextln: $()No method named "assoc" found for type "S".

#check: $()2 errors.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = "recursive_const_associated_over_module_function"
source = "member"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
name = "recursive_const_associated_over_module_function"
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
library;

struct S {}

fn mod_fn() -> u8 {
S::S_ASSOC
}

impl S {
const S_ASSOC: u8 = mod_fn();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
category = "disabled" # TODO: Enable this failing test and write checks once https://github.com/FuelLabs/sway/issues/6539 is fixed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = "recursive_const_module"
source = "member"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
name = "recursive_const_module"
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
library;

pub const A: u8 = B;
pub const B: u8 = A;

pub const X: u8 = Y;
pub const Y: u8 = Z;
pub const Z: u8 = X;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
category = "fail" # TODO: Adjust these checks once https://github.com/FuelLabs/sway/issues/6534 is fixed.

# check: $()Type B is recursive via A, which is unsupported at this time.
# check: $()Type A is recursive via B, which is unsupported at this time.

# check: $()Type Y is recursive via Z and X, which is unsupported at this time.
# check: $()Type Z is recursive via X and Y, which is unsupported at this time.
# check: $()Type X is recursive via Y and Z, which is unsupported at this time.

# check: $()5 errors.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = "recursive_const_module_over_associated_const"
source = "member"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
name = "recursive_const_module_over_associated_const"
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
library;

struct S {}

impl S {
const S_ASSOC: u8 = MOD_CONST;
}

const MOD_CONST: u8 = S::S_ASSOC;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
category = "fail"

#check: $()error
#check: $()const MOD_CONST: u8 = S::S_ASSOC;
#nextln: $()Could not find symbol "S_ASSOC" in this scope.

#check: $()error
#check: $()const MOD_CONST: u8 = S::S_ASSOC;
#nextln: $()Could not evaluate initializer to a const declaration.

#check: $()2 errors.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = "recursive_const_module_over_associated_function"
source = "member"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
name = "recursive_const_module_over_associated_function"
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
library;

struct S {}

impl S {
fn assoc() -> u8 {
MOD_CONST
}
}

const MOD_CONST: u8 = S::assoc();
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
category = "fail"

#check: $()const MOD_CONST: u8 = S::assoc();
#nextln: $()No method named "assoc" found for type "S".

#check: $()const MOD_CONST: u8 = S::assoc();
#nextln: $()Could not evaluate initializer to a const declaration.

#check: $()2 errors.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = "recursive_const_module_over_module_function"
source = "member"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
name = "recursive_const_module_over_module_function"
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library;

fn mod_fn() -> u8 {
MOD_CONST
}

const MOD_CONST: u8 = mod_fn();
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
category = "fail"

#check: $()error
#check: $()const MOD_CONST: u8 = mod_fn();
#nextln: $()Could not find symbol "mod_fn" in this scope.

#check: $()error
#check: $()const MOD_CONST: u8 = mod_fn();
#nextln: $()Could not evaluate initializer to a const declaration.

#check: $()2 errors.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = "recursive_const_stack_overflow"
source = "member"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
name = "recursive_const_stack_overflow"
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This test proves that https://github.com/FuelLabs/sway/issues/6540 is fixed.

library;

pub const MOD_FN: u8 = mod_fn();

fn mod_fn() -> u8 {
MOD_FN
}

struct S {}

impl S {
const S_ASSOC: u8 = MOD_CONST;
}

const MOD_CONST: u8 = S::S_ASSOC;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
category = "disabled" # TODO: Enable this failing test and write checks once https://github.com/FuelLabs/sway/issues/6540 is fixed.
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
script;
library;

enum E {
Eins: bool,
Zwei: u64,
Drei: E,
}

fn main() {

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
category = "fail"

# check: $()recursive types are not supported

# check: $()1 error.
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
script;
library;

struct S {
Eins: bool,
Zwei: u64,
Drei: S,
}

fn main() {

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
category = "fail"

# check: $()recursive types are not supported

# check: $()1 error.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
script;
library;

enum E {
Eins: F,
Expand Down Expand Up @@ -39,7 +39,3 @@ enum Y {
struct Z {
five: X,
}

fn main() {

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ category = "fail"
# check: $()Type Y is recursive via Z and X, which is unsupported at this time.
# check: $()Type Z is recursive via X and Y, which is unsupported at this time.
# check: $()Type X is recursive via Y and Z, which is unsupported at this time.

# check: $()10 errors.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
script;
library;

enum MyOption<T> {
Some: T,
Expand All @@ -13,5 +13,5 @@ fn bar<V>(value: V) -> MyOption<V> {
}

fn main() {
let x = bar(false);
let _ = bar(false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ category = "fail"
# nextln: $()expected: V
# nextln: $()found: MyOption<V>.
# nextln: $()Implicit return must match up with block's type.

# check: $()2 errors.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = "associated_const_in_decls_of_other_constants"
source = "member"
dependencies = ["std"]

[[package]]
name = "core"
source = "path+from-root-8CB91B60FB2568E4"

[[package]]
name = "std"
source = "path+from-root-8CB91B60FB2568E4"
dependencies = ["core"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "associated_const_in_decls_of_other_constants"
implicit-std = false


[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
Loading

0 comments on commit e1a05fc

Please sign in to comment.