Skip to content

Commit

Permalink
Fixes unreachable macro crashing compiler. (#6362)
Browse files Browse the repository at this point in the history
## Description

In case of a bad input given to the lexer, it is possible to generate an
AST with invalid ImplItem tuples.

This fix replaces the unreachable macro with an CompileError::Internal.
This allows the user to see the lexer errors and fix them which will
also address the new errors.

Fixes #6339.

## Checklist

- [x] I have linked to any relevant issues.
- [x] 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.
- [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.

Co-authored-by: IGI-111 <[email protected]>
Co-authored-by: João Matos <[email protected]>
  • Loading branch information
3 people authored Aug 7, 2024
1 parent a6f1ebe commit 2332025
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 2 deletions.
14 changes: 12 additions & 2 deletions sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,12 @@ impl TyImplSelfOrTrait {
(ImplItem::Type(_type_decl), TyTraitItem::Type(_decl_ref)) => {
// Already processed.
}
_ => unreachable!(),
_ => {
handler.emit_err(CompileError::Internal(
"Unexpected ImplItem tuple.",
Span::dummy(),
));
}
}
}

Expand Down Expand Up @@ -542,7 +547,12 @@ impl TyImplSelfOrTrait {
(ImplItem::Type(_type_decl), TyTraitItem::Type(_decl_ref)) => {
// Already processed.
}
_ => unreachable!(),
_ => {
handler.emit_err(CompileError::Internal(
"Unexpected ImplItem tuple.",
Span::dummy(),
));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[[package]]
name = "core"
source = "path+from-root-082ED3C1A64D1BB4"

[[package]]
name = "lexer_errors"
source = "member"
dependencies = ["core"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "lexer_errors"
implicit-std = false

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

pub struct R {
a: u32,
}

impl R {
pub fn from_str(new: i32) {
p() {
:Script => {
c = ZERO_B256;
), }
}

const OFFSET = 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
category = "fail"

# check: $()error
# check: $()mismatched delimiters

# check: $()error
# check: $()impl R {
# nextln: $()unclosed delimiter

0 comments on commit 2332025

Please sign in to comment.