Skip to content

Commit

Permalink
Fixes panic on unwrapping in type_check_trait_implementation. (#6434)
Browse files Browse the repository at this point in the history
## Description

This PR fixes panic on unwrapping by only performing the required
behavior with the unwrapped value when it is available.

Fixes #6336

## 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: Sophie Dankel <[email protected]>
Co-authored-by: Joshua Batty <[email protected]>
  • Loading branch information
3 people authored Aug 23, 2024
1 parent 3bceb0a commit 4f08020
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
38 changes: 22 additions & 16 deletions sway-core/src/semantic_analysis/ast_node/declaration/impl_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,22 +831,28 @@ fn type_check_trait_implementation(
None,
),
};
trait_type_mapping.extend(&TypeSubstMap::from_type_parameters_and_type_arguments(
vec![type_engine.insert(
engines,
old_type_decl_info1,
type_decl.name.span().source_id(),
)],
vec![type_decl.ty.clone().unwrap().type_id],
));
trait_type_mapping.extend(&TypeSubstMap::from_type_parameters_and_type_arguments(
vec![type_engine.insert(
engines,
old_type_decl_info2,
type_decl.name.span().source_id(),
)],
vec![type_decl.ty.clone().unwrap().type_id],
));
if let Some(type_arg) = type_decl.ty.clone() {
trait_type_mapping.extend(
&TypeSubstMap::from_type_parameters_and_type_arguments(
vec![type_engine.insert(
engines,
old_type_decl_info1,
type_decl.name.span().source_id(),
)],
vec![type_arg.type_id],
),
);
trait_type_mapping.extend(
&TypeSubstMap::from_type_parameters_and_type_arguments(
vec![type_engine.insert(
engines,
old_type_decl_info2,
type_decl.name.span().source_id(),
)],
vec![type_arg.type_id],
),
);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = "associated_type_not_in_trait"
source = "member"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "associated_type_not_in_trait"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
script;
trait Trait{}
struct Struct0{}
impl Trait for Struct0{type u;const u=0;}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "fail"

# check: $()Type "u" is not a part of trait "Trait"'s interface surface.

0 comments on commit 4f08020

Please sign in to comment.