Skip to content

Commit

Permalink
Only check non-parent monomorphization args (#6549)
Browse files Browse the repository at this point in the history
## Description

This PR fixes #6384

The performance improvement is probably coming from just aggregating
spans when there is an error.

## 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.

---------

Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
  • Loading branch information
xunilrj and JoshuaBatty authored Sep 18, 2024
1 parent 520bfe9 commit e82c72b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
26 changes: 17 additions & 9 deletions sway-core/src/semantic_analysis/type_check_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1700,27 +1700,35 @@ impl<'a> TypeCheckContext<'a> {
0,
)))
}
(num_type_params, num_type_args) => {
let type_arguments_span = type_arguments
.iter()
.map(|x| x.span.clone())
.reduce(|s1: Span, s2: Span| Span::join(s1, &s2))
.unwrap_or_else(|| value.name().span());
(_, num_type_args) => {
// a trait decl is passed the self type parameter and the corresponding argument
// but it would be confusing for the user if the error reporting mechanism
// reported the number of arguments including the implicit self, hence
// we adjust it below
let adjust_for_trait_decl = value.has_self_type_param() as usize;
let num_type_params = num_type_params - adjust_for_trait_decl;
let non_parent_type_params = value
.type_parameters()
.iter()
.filter(|x| !x.is_from_parent)
.count()
- adjust_for_trait_decl;

let num_type_args = num_type_args - adjust_for_trait_decl;
if num_type_params != num_type_args {
if non_parent_type_params != num_type_args {
let type_arguments_span = type_arguments
.iter()
.map(|x| x.span.clone())
.reduce(|s1: Span, s2: Span| Span::join(s1, &s2))
.unwrap_or_else(|| value.name().span());

return Err(handler.emit_err(make_type_arity_mismatch_error(
value.name().clone(),
type_arguments_span,
num_type_args,
num_type_params,
non_parent_type_params,
)));
}

for type_argument in type_arguments.iter_mut() {
type_argument.type_id = self
.resolve(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ impl MyEq for Data3 {
}
}

// This tests it is still possible to call generic methods
// where its parent has constraints
// https://github.com/FuelLabs/sway/issues/6384

trait MyTrait { }
struct S<T> { }

struct M1 {}
impl MyTrait for M1 { }

impl<T> S<T> where T: MyTrait {
fn bar<M>() {
}
}

fn issue_6384() {
let _x = S::<M1>::bar::<M1>();
}

fn main() -> u64 {
let s = Data { x: 42 };
assert(s.contains(42));
Expand Down Expand Up @@ -116,5 +135,7 @@ fn main() -> u64 {
assert(!d4.contains5(Data2 { x: 41, y: Data3 { x: 42 } }));
*/

issue_6384();

10
}

0 comments on commit e82c72b

Please sign in to comment.