From a140882b9faa7f814f5f63bd043efd37caa64746 Mon Sep 17 00:00:00 2001 From: Michael <78988079+mhasel@users.noreply.github.com> Date: Tue, 26 Dec 2023 13:15:22 +0100 Subject: [PATCH] fix: dont report generic implementations for duplicates (#1054) *fix: no longer validate generic dupes --- .../generators/expression_generator.rs | 9 ------ src/validation/global.rs | 18 ++++++++--- .../tests/duplicates_validation_test.rs | 32 +++++++++++++++++++ ...ation_test__duplicate_pous_validation.snap | 2 +- 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/codegen/generators/expression_generator.rs b/src/codegen/generators/expression_generator.rs index cfaf1f9be9..3b7f34588a 100644 --- a/src/codegen/generators/expression_generator.rs +++ b/src/codegen/generators/expression_generator.rs @@ -334,7 +334,6 @@ impl<'ink, 'b> ExpressionCodeGenerator<'ink, 'b> { let reference = cast_if_needed!(self, target_type, self.get_type_hint_for(index)?, reference, None) .into_int_value(); - // let reference = reference.into_int_value(); //Multiply by the bitwitdh if access.get_bit_width() > 1 { let bitwidth = @@ -430,14 +429,6 @@ impl<'ink, 'b> ExpressionCodeGenerator<'ink, 'b> { // if the function is builtin, generate a basic value enum for it if let Some(builtin) = self.index.get_builtin_function(implementation_name) { // adr, ref, etc. - // let parameters_list = if let Some(StatementAnnotation::ReplacementAst { statement }) = - // self.annotations.get(operator) - // { - // statement.get_as_list() - // } else { - // parameters_list - // }; - return builtin.codegen(self, parameters_list.as_slice(), operator.get_location()); } diff --git a/src/validation/global.rs b/src/validation/global.rs index aef55d6c56..6672334fde 100644 --- a/src/validation/global.rs +++ b/src/validation/global.rs @@ -153,22 +153,27 @@ impl GlobalValidator { .unwrap_or(false) }) .map(|it| (it.get_name(), &it.source_location)); - let all_prgs_and_funcs = index + let all_prgs = index .get_pous() .values() .filter(|p| { matches!( p, PouIndexEntry::Program { .. } - | PouIndexEntry::Function { .. } | PouIndexEntry::Method { .. } | PouIndexEntry::Action { .. } ) }) .map(|it| (it.get_name(), it.get_location())); + let all_funcs = index + .get_pous() + .values() + .filter(|p| p.is_function() && !p.is_generic()) + .map(|it| (it.get_name(), it.get_location())); + self.check_uniqueness_of_cluster( - all_fb_instances.chain(all_prgs_and_funcs), + all_fb_instances.chain(all_prgs).chain(all_funcs), Some("Ambiguous callable symbol."), ); } @@ -186,7 +191,12 @@ impl GlobalValidator { .entries() .filter(|(_, entries_per_name)| entries_per_name.iter().filter(only_toplevel_pous).count() > 1) .map(|(name, pous)| { - (name.as_str(), pous.iter().filter(only_toplevel_pous).map(|p| p.get_location())) + ( + name.as_str(), + pous.iter() + .filter(|p| only_toplevel_pous(p) && !p.is_generic()) + .map(|p| p.get_location()), + ) }); for (name, cluster) in pou_clusters { diff --git a/src/validation/tests/duplicates_validation_test.rs b/src/validation/tests/duplicates_validation_test.rs index a4305485b6..2c20be45da 100644 --- a/src/validation/tests/duplicates_validation_test.rs +++ b/src/validation/tests/duplicates_validation_test.rs @@ -484,6 +484,38 @@ fn duplicate_with_generic() { assert_eq!(diagnostics, vec![]); } +#[test] +fn generics_with_duplicate_symbol_dont_err() { + // GIVEN a builtin function with the signature + // FUNCTION ADD : T + // VAR_INPUT + // args: {sized} T...; + // END_VAR + // END_FUNCTION + + // WHEN it is indexed and validated with other generic functions with the same name + let diagnostics = parse_and_validate( + r#" + FUNCTION ADD < T1: ANY, T2: ANY >: T1 + VAR_INPUT + IN1: T1; + IN2: T2; + END_VAR + END_FUNCTION + + FUNCTION ADD < K: ANY, V: ANY > : K + VAR_INPUT + IN1: K; + IN2: V; + END_VAR + END_FUNCTION + "#, + ); + + // THEN there should be no duplication diagnostics + assert_eq!(diagnostics, vec![]); +} + // #[test] // fn duplicate_with_generic_ir() { // // GIVEN several files with calls to a generic function diff --git a/src/validation/tests/snapshots/rusty__validation__tests__duplicates_validation_test__duplicate_pous_validation.snap b/src/validation/tests/snapshots/rusty__validation__tests__duplicates_validation_test__duplicate_pous_validation.snap index 32f0d03973..31edd25c4d 100644 --- a/src/validation/tests/snapshots/rusty__validation__tests__duplicates_validation_test__duplicate_pous_validation.snap +++ b/src/validation/tests/snapshots/rusty__validation__tests__duplicates_validation_test__duplicate_pous_validation.snap @@ -2,8 +2,8 @@ source: src/validation/tests/duplicates_validation_test.rs expression: res --- -SyntaxError { message: "foo: Ambiguous callable symbol.", range: [SourceLocation { span: Range(TextLocation { line: 1, column: 24, offset: 25 }..TextLocation { line: 1, column: 27, offset: 28 }) }, SourceLocation { span: Range(TextLocation { line: 3, column: 24, offset: 74 }..TextLocation { line: 3, column: 27, offset: 77 }) }], err_no: duplicate_symbol } SyntaxError { message: "foo: Ambiguous callable symbol.", range: [SourceLocation { span: Range(TextLocation { line: 3, column: 24, offset: 74 }..TextLocation { line: 3, column: 27, offset: 77 }) }, SourceLocation { span: Range(TextLocation { line: 1, column: 24, offset: 25 }..TextLocation { line: 1, column: 27, offset: 28 }) }], err_no: duplicate_symbol } +SyntaxError { message: "foo: Ambiguous callable symbol.", range: [SourceLocation { span: Range(TextLocation { line: 1, column: 24, offset: 25 }..TextLocation { line: 1, column: 27, offset: 28 }) }, SourceLocation { span: Range(TextLocation { line: 3, column: 24, offset: 74 }..TextLocation { line: 3, column: 27, offset: 77 }) }], err_no: duplicate_symbol } SyntaxError { message: "foo: Duplicate symbol.", range: [SourceLocation { span: Range(TextLocation { line: 1, column: 24, offset: 25 }..TextLocation { line: 1, column: 27, offset: 28 }) }, SourceLocation { span: Range(TextLocation { line: 3, column: 24, offset: 74 }..TextLocation { line: 3, column: 27, offset: 77 }) }, SourceLocation { span: Range(TextLocation { line: 5, column: 24, offset: 116 }..TextLocation { line: 5, column: 27, offset: 119 }) }], err_no: duplicate_symbol } SyntaxError { message: "foo: Duplicate symbol.", range: [SourceLocation { span: Range(TextLocation { line: 3, column: 24, offset: 74 }..TextLocation { line: 3, column: 27, offset: 77 }) }, SourceLocation { span: Range(TextLocation { line: 1, column: 24, offset: 25 }..TextLocation { line: 1, column: 27, offset: 28 }) }, SourceLocation { span: Range(TextLocation { line: 5, column: 24, offset: 116 }..TextLocation { line: 5, column: 27, offset: 119 }) }], err_no: duplicate_symbol } SyntaxError { message: "foo: Duplicate symbol.", range: [SourceLocation { span: Range(TextLocation { line: 5, column: 24, offset: 116 }..TextLocation { line: 5, column: 27, offset: 119 }) }, SourceLocation { span: Range(TextLocation { line: 1, column: 24, offset: 25 }..TextLocation { line: 1, column: 27, offset: 28 }) }, SourceLocation { span: Range(TextLocation { line: 3, column: 24, offset: 74 }..TextLocation { line: 3, column: 27, offset: 77 }) }], err_no: duplicate_symbol }