Skip to content

Commit

Permalink
fail const eval if return_type is error (#6023)
Browse files Browse the repository at this point in the history
## Description

This PR is part of #5727.
Fix the issue of trying to solve expressions that were typed as
`ErrorRecovery` inside `const_eval`.

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] 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.
- [x] 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
xunilrj authored May 17, 2024
1 parent 2ec4332 commit 76860b8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sway-core/src/ir_generation/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ fn const_eval_typed_expr(
expr: &ty::TyExpression,
allow_configurables: bool,
) -> Result<Option<Constant>, ConstEvalError> {
if let TypeInfo::ErrorRecovery(_) = &*lookup.engines.te().get(expr.return_type) {
return Err(ConstEvalError::CannotBeEvaluatedToConst {
span: expr.span.clone(),
});
}

Ok(match &expr.expression {
ty::TyExpressionVariant::Literal(Literal::Numeric(n)) => {
let implied_lit = match &*lookup.engines.te().get(expr.return_type) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[[package]]
name = "configurables_undefined_var"
source = "member"
dependencies = ["core"]

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

[dependencies]
core = { path = "../../../../../../sway-lib-core" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
script;

configurable {
VALUE: u64 = DOES_NOT_EXIST,
}

fn main() {
const CONSTANT: u64 = VALUE;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "fail"

#check: $()Variable "DOES_NOT_EXIST" does not exist in this scope.

0 comments on commit 76860b8

Please sign in to comment.