Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/topic/bbannier/issue-1981'
Browse files Browse the repository at this point in the history
  • Loading branch information
bbannier committed Mar 6, 2025
2 parents c9ed1a1 + 2f672a0 commit 7d30d55
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
14 changes: 14 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
1.13.0-dev.115 | 2025-03-06 12:00:38 +0100

* GH-1981: Reject function overloads which only differ in default'd parameters. (Benjamin Bannier, Corelight)

Resolution of calls to functions who's signatures differ only in
defaulted parameters behaved non-intuitively in that when given with
less than the maximum possible number of parameters we resolved to the
overload with most parameters.

With this patch we reject such overloads where the only difference is in
defaulted parameters.

Closes #1981.

1.13.0-dev.113 | 2025-03-03 17:39:36 +0100

* GH-1983: Audit links in README. (Benjamin Bannier, Corelight)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.13.0-dev.113
1.13.0-dev.115
4 changes: 1 addition & 3 deletions hilti/toolchain/include/ast/types/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ inline bool areEquivalent(Function* f1, Function* f2) {
}

/** Returns true if the provided function types can be valid overloads of each other. */
inline bool isValidOverload(Function* f1, Function* f2) {
return type::same(f1->result(), f2->result()) || ! areEquivalent(f1->parameters(), f2->parameters());
}
bool isValidOverload(Function* f1, Function* f2);

} // namespace hilti::type
19 changes: 18 additions & 1 deletion hilti/toolchain/src/ast/types/function.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright (c) 2020-now by the Zeek Project. See LICENSE for details.

#include <hilti/ast/types/function.h>
#include "hilti/ast/types/function.h"

#include <algorithm>
#include <iterator>

using namespace hilti;

Expand All @@ -23,3 +26,17 @@ bool type::Function::isResolved(node::CycleDetector* cd) const {

return true;
}

bool type::isValidOverload(Function* f1, Function* f2) {
const auto& params1 = f1->parameters();
const auto& params2 = f2->parameters();

auto non_defaulted = [](const node::Set<function::Parameter>& p) {
node::Set<function::Parameter> r;
std::copy_if(p.begin(), p.end(), std::back_inserter(r), [](function::Parameter* p) { return ! p->default_(); });
return r;
};

return type::same(f1->result(), f2->result()) ||
(! areEquivalent(params1, params2) && ! areEquivalent(non_defaulted(params1), non_defaulted(params2)));
}
3 changes: 3 additions & 0 deletions tests/Baseline/hilti.validation.duplicate-5/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
[error] <...>/duplicate.hlt:12:1-14:1: 'fun' is not a valid overload; previous definition in <...>/duplicate.hlt:8:1-10:1
[error] hiltic: aborting after errors
20 changes: 20 additions & 0 deletions tests/hilti/validation/duplicate.hlt
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,23 @@ global s2 = succeed(1);
# Force failure to get output
assert s1 == s2: "%d == %d" % (s1, s2);
}

# @TEST-START-NEXT

module foo {

import hilti;

# Overloads which only differ in defaulted parameters are not allowed. This is
# roughly in line with the overloads C++ allows.
function uint<8> fun(uint<8> a) {
return uint8(1);
}

function uint<16> fun(uint<8> a, uint<8> b = uint8(0)) {
return uint16(2);
}

hilti::print(fun(uint8(1)));

}

0 comments on commit 7d30d55

Please sign in to comment.