Skip to content

Commit

Permalink
Fixes invalid IR, on impl self with where clause. (FuelLabs#4732)
Browse files Browse the repository at this point in the history
## Description
Methods were missing type_parameters from impl self where clauses.

This caused the trait methods not to be replaced with a valid
implementation.

Fixes FuelLabs#4719

## 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).
- [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
esdrubal authored Jul 3, 2023
1 parent 2e7c20e commit e286571
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 3 deletions.
25 changes: 22 additions & 3 deletions sway-core/src/transform/to_parsed_lang/convert_parse_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn item_to_ast_nodes(
)),
ItemKind::Fn(item_fn) => {
let function_declaration = item_fn_to_function_declaration(
context, handler, engines, item_fn, attributes, None,
context, handler, engines, item_fn, attributes, None, None,
)?;
error_if_self_param_is_not_allowed(
context,
Expand Down Expand Up @@ -463,6 +463,7 @@ fn item_fn_to_function_declaration(
item_fn: ItemFn,
attributes: AttributesMap,
parent_generic_params_opt: Option<GenericParams>,
parent_where_clause_opt: Option<WhereClause>,
) -> Result<FunctionDeclaration, ErrorEmitted> {
let span = item_fn.span();
let return_type = match item_fn.fn_signature.return_type_opt {
Expand Down Expand Up @@ -498,6 +499,7 @@ fn item_fn_to_function_declaration(
item_fn.fn_signature.generics,
parent_generic_params_opt,
item_fn.fn_signature.where_clause_opt.clone(),
parent_where_clause_opt,
)?,
where_clause: item_fn
.fn_signature
Expand Down Expand Up @@ -575,7 +577,7 @@ fn item_trait_to_trait_declaration(
handler,
engines,
item_trait.generics.clone(),
item_trait.where_clause_opt,
item_trait.where_clause_opt.clone(),
)?;
let interface_surface = item_trait
.trait_items
Expand Down Expand Up @@ -616,6 +618,7 @@ fn item_trait_to_trait_declaration(
item_fn.value,
attributes,
item_trait.generics.clone(),
item_trait.where_clause_opt.clone(),
)?))
})
.filter_map_ok(|fn_decl| fn_decl)
Expand Down Expand Up @@ -663,6 +666,7 @@ fn item_impl_to_declaration(
fn_item,
attributes,
item_impl.generic_params_opt.clone(),
item_impl.where_clause_opt.clone(),
)
.map(ImplItem::Fn),
sway_ast::ItemImplItem::Const(const_item) => item_const_to_constant_declaration(
Expand Down Expand Up @@ -806,6 +810,7 @@ fn item_abi_to_abi_declaration(
item_fn.value,
attributes,
None,
None,
)?;
error_if_self_param_is_not_allowed(
context,
Expand Down Expand Up @@ -1029,6 +1034,7 @@ fn generic_params_opt_to_type_parameters(
generic_params_opt,
None,
where_clause_opt,
None,
)
}

Expand All @@ -1039,6 +1045,7 @@ fn generic_params_opt_to_type_parameters_with_parent(
generic_params_opt: Option<GenericParams>,
parent_generic_params_opt: Option<GenericParams>,
where_clause_opt: Option<WhereClause>,
parent_where_clause_opt: Option<WhereClause>,
) -> Result<Vec<TypeParameter>, ErrorEmitted> {
let type_engine = engines.te();

Expand All @@ -1051,6 +1058,15 @@ fn generic_params_opt_to_type_parameters_with_parent(
None => Vec::new(),
};

let parent_trait_constraints = match parent_where_clause_opt {
Some(where_clause) => where_clause
.bounds
.into_iter()
.map(|where_bound| (where_bound.ty_name, where_bound.bounds))
.collect::<Vec<_>>(),
None => Vec::new(),
};

let generics_to_params = |generics: Option<GenericParams>, is_from_parent: bool| match generics
{
Some(generic_params) => generic_params
Expand Down Expand Up @@ -1082,7 +1098,10 @@ fn generic_params_opt_to_type_parameters_with_parent(
let parent_params = generics_to_params(parent_generic_params_opt, true);

let mut errors = Vec::new();
for (ty_name, bounds) in trait_constraints.into_iter() {
for (ty_name, bounds) in trait_constraints
.into_iter()
.chain(parent_trait_constraints)
{
let param_to_edit = if let Some(o) = params
.iter_mut()
.find(|TypeParameter { name_ident, .. }| name_ident.as_str() == ty_name.as_str())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'core'
source = 'path+from-root-AA4B286930BA1707'

[[package]]
name = 'generic_where_in_impl_self'
source = 'member'
dependencies = ['std']

[[package]]
name = 'std'
source = 'path+from-root-AA4B286930BA1707'
dependencies = ['core']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "generic_where_in_impl_self"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"configurables": [],
"functions": [
{
"attributes": null,
"inputs": [],
"name": "main",
"output": {
"name": "",
"type": 0,
"typeArguments": null
}
}
],
"loggedTypes": [],
"messagesTypes": [],
"types": [
{
"components": null,
"type": "bool",
"typeId": 0,
"typeParameters": null
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
script;

use std::assert::*;

trait Trait {
#[inline(never)]
fn method(self) -> u64;
}

impl Trait for u64 {
#[inline(never)]
fn method(self) -> u64{
42
}
}

struct CallTrait<V> {}

#[inline(never)]
fn call_trait<T>(t: T) -> u64 where T: Trait {
t.method()
}

impl<K> CallTrait<K> where K: Trait {
pub fn call_trait(self, key: K) -> u64 {
call_trait(key)
}
}

fn main() -> bool {
let _ = call_trait(1);
let ct = CallTrait::<u64> {};
assert(ct.call_trait(1) == 42);
true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "run"
expected_result = { action = "return", value = 1 }
validate_abi = true

0 comments on commit e286571

Please sign in to comment.