Skip to content

Commit 697cfa7

Browse files
authored
ast: allow using aliased types in interface method implementations (provide backwards compatibility to ui, during the migration of code from gx to gg) (#25106)
1 parent a1b131c commit 697cfa7

3 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎vlib/v/ast/table.v‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ pub fn (t &Table) is_same_method(f &Fn, func &Fn) string {
218218

219219
for i in 0 .. f.params.len {
220220
// don't check receiver for `.typ`
221-
has_unexpected_type := i > 0 && f.params[i].typ != func.params[i].typ
221+
has_unexpected_type := i > 0
222+
&& t.unaliased_type(f.params[i].typ) != t.unaliased_type(func.params[i].typ)
222223
// temporary hack for JS ifaces
223224
lsym := t.sym(f.params[i].typ)
224225
rsym := t.sym(func.params[i].typ)

‎vlib/v/pref/pref.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ pub mut:
239239
cache_manager vcache.CacheManager
240240
gc_mode GarbageCollectionMode = .unknown // .no_gc, .boehm, .boehm_leak, ...
241241
assert_failure_mode AssertFailureMode // whether to call abort() or print_backtrace() after an assertion failure
242-
message_limit int = 150 // the maximum amount of warnings/errors/notices that will be accumulated
242+
message_limit int = 200 // the maximum amount of warnings/errors/notices that will be accumulated
243243
nofloat bool // for low level code, like kernels: replaces f32 with u32 and f64 with u64
244244
use_coroutines bool // experimental coroutines
245245
fast_math bool // -fast-math will pass either -ffast-math or /fp:fast (for msvc) to the C backend
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// This tests, whether type aliased values, can be used in place of their non aliased versions,
2+
// when implementing interfaces. This is important for allowing type aliases in one module,
3+
// to be used as substitutes to keep backwards compatibility, when migrating functionality
4+
// from one module to another, which was the case, when deprecating `gx` in favor of moving
5+
// its functionality into `gg`, while still keeping old code using `gx` compilable for a while,
6+
// by making `gx` import `gg` and declare a few shallow type aliases to the new `gg` types,
7+
// so that older `gx` importers and their CIs (like the `ui` module), can continue to work
8+
// (with just deprecation notices), without forcing an immediate change.
9+
struct Param1 {
10+
x int
11+
}
12+
13+
struct Param2 {
14+
y f32
15+
}
16+
17+
type AliasParam1 = Param1
18+
type AliasParam2 = Param2
19+
20+
interface MyInterface {
21+
method1(p1 Param1, p2 Param2) f32
22+
}
23+
24+
struct ImplDirect {
25+
v int
26+
}
27+
28+
fn (i ImplDirect) method1(p1 Param1, p2 Param2) f32 {
29+
println(i)
30+
println(p1)
31+
println(p2)
32+
return i.v + p1.x + p2.y
33+
}
34+
35+
struct ImplWithAliasedParams {
36+
v int
37+
}
38+
39+
fn (i ImplWithAliasedParams) method1(p1 AliasParam1, p2 AliasParam2) f32 {
40+
println(i)
41+
println(p1)
42+
println(p2)
43+
return i.v + p1.x + p2.y
44+
}
45+
46+
fn test_interface_method_can_be_called_with_aliased_type_values() {
47+
isdirect := MyInterface(ImplDirect{1000})
48+
isalias := MyInterface(ImplWithAliasedParams{2000})
49+
50+
assert isdirect.method1(AliasParam1{123}, AliasParam2{1.1}) == 1124.1
51+
assert isalias.method1(AliasParam1{456}, AliasParam2{2.2}) == 2458.2
52+
}

0 commit comments

Comments
 (0)