Skip to content

Commit

Permalink
fix: dont report generic implementations for duplicates (PLC-lang#1054)
Browse files Browse the repository at this point in the history
*fix: no longer validate generic dupes
  • Loading branch information
mhasel authored Dec 26, 2023
1 parent aa577c2 commit a140882
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
9 changes: 0 additions & 9 deletions src/codegen/generators/expression_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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());
}

Expand Down
18 changes: 14 additions & 4 deletions src/validation/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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."),
);
}
Expand All @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions src/validation/tests/duplicates_validation_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: ANY_NUM> : 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit a140882

Please sign in to comment.