Skip to content

Fix type confusion when resolving nested generics #1762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6438,7 +6438,7 @@ export class Compiler extends DiagnosticEmitter {
assert(typeParameterNodes),
typeArgumentNodes,
this.currentFlow.actualFunction.parent,
uniqueMap<string,Type>(this.currentFlow.contextualTypeArguments),
uniqueMap<string,Type>(this.currentFlow.contextualTypeArguments), // don't update
expression
);
}
Expand Down
14 changes: 8 additions & 6 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ export class Resolver extends DiagnosticEmitter {
typeParameterNodes,
typeArgumentNodes,
ctxElement,
ctxTypes = uniqueMap(ctxTypes), // inherit
ctxTypes = uniqueMap(ctxTypes), // update
node,
reportMode
);
Expand Down Expand Up @@ -644,7 +644,7 @@ export class Resolver extends DiagnosticEmitter {
typeArgumentNodes: TypeNode[] | null,
/** Contextual element. */
ctxElement: Element,
/** Contextual types, i.e. `T`. */
/** Contextual types, i.e. `T`. Updated in place with the new set of contextual types. */
ctxTypes: Map<string,Type> = uniqueMap<string,Type>(),
/** Alternative report node in case of empty type arguments. */
alternativeReportNode: Node | null = null,
Expand Down Expand Up @@ -675,18 +675,20 @@ export class Resolver extends DiagnosticEmitter {
return null;
}
var typeArguments = new Array<Type>(maxParameterCount);
var oldCtxTypes = uniqueMap<string,Type>(ctxTypes);
ctxTypes.clear();
for (let i = 0; i < maxParameterCount; ++i) {
let type = i < argumentCount
? this.resolveType( // reports
typeArgumentNodes![i],
ctxElement,
ctxTypes,
oldCtxTypes, // update
reportMode
)
: this.resolveType( // reports
assert(typeParameters[i].defaultType),
ctxElement,
ctxTypes,
uniqueMap<string,Type>(ctxTypes), // don't update
reportMode
);
if (!type) return null;
Expand Down Expand Up @@ -2796,7 +2798,7 @@ export class Resolver extends DiagnosticEmitter {
assert(prototype.typeParameterNodes),
typeArgumentNodes,
ctxElement,
ctxTypes,
ctxTypes, // update
reportNode,
reportMode
);
Expand Down Expand Up @@ -3272,7 +3274,7 @@ export class Resolver extends DiagnosticEmitter {
assert(prototype.typeParameterNodes), // must be present if generic
typeArgumentNodes,
ctxElement,
ctxTypes,
ctxTypes, // update
reportNode,
reportMode
);
Expand Down
8 changes: 8 additions & 0 deletions tests/compiler/issues/1714.optimized.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(module
(memory $0 1)
(data (i32.const 1036) ",")
(data (i32.const 1048) "\01\00\00\00\1c\00\00\00i\00s\00s\00u\00e\00s\00/\001\007\001\004\00.\00t\00s")
(data (i32.const 1084) "\1c")
(data (i32.const 1096) "\01\00\00\00\06\00\00\00i\003\002")
(export "memory" (memory $0))
)
18 changes: 18 additions & 0 deletions tests/compiler/issues/1714.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function a_i64_i32<T,U>(): bool { // T'=U=i64, U'=T=i32
return sizeof<T>() == sizeof<U>();
}

function foo<T,U>(): bool { // T=i32, U=i64
return a_i64_i32<U,T>() == true;
}

assert(foo<i32, i64>() == false);

function bar<T1,T2>(): string { // T1=f64, T2=i32
if (isInteger<T2>()) {
return bar<T2,T1>(); // T2'=T1=f64, T2'=T1=i32
}
return nameof<T1>(); // iff T1 == i32
}

assert(bar<f64, i32>() === "i32");
65 changes: 65 additions & 0 deletions tests/compiler/issues/1714.untouched.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
(module
(type $none_=>_i32 (func (result i32)))
(type $none_=>_none (func))
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
(memory $0 1)
(data (i32.const 12) ",\00\00\00\00\00\00\00\00\00\00\00\01\00\00\00\1c\00\00\00i\00s\00s\00u\00e\00s\00/\001\007\001\004\00.\00t\00s\00")
(data (i32.const 60) "\1c\00\00\00\00\00\00\00\00\00\00\00\01\00\00\00\06\00\00\00i\003\002\00\00\00\00\00\00\00")
(table $0 1 funcref)
(global $~lib/memory/__data_end i32 (i32.const 92))
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 16476))
(global $~lib/memory/__heap_base i32 (i32.const 16476))
(export "memory" (memory $0))
(start $~start)
(func $issues/1714/a_i64_i32<i64,i32> (result i32)
i32.const 8
i32.const 4
i32.eq
)
(func $issues/1714/foo<i32,i64> (result i32)
call $issues/1714/a_i64_i32<i64,i32>
i32.const 1
i32.eq
)
(func $issues/1714/bar<i32,f64> (result i32)
i32.const 0
drop
i32.const 80
)
(func $issues/1714/bar<f64,i32> (result i32)
i32.const 1
drop
call $issues/1714/bar<i32,f64>
return
)
(func $start:issues/1714
call $issues/1714/foo<i32,i64>
i32.const 0
i32.eq
i32.eqz
if
i32.const 0
i32.const 32
i32.const 9
i32.const 1
call $~lib/builtins/abort
unreachable
end
call $issues/1714/bar<f64,i32>
i32.const 80
i32.eq
i32.eqz
if
i32.const 0
i32.const 32
i32.const 18
i32.const 1
call $~lib/builtins/abort
unreachable
end
)
(func $~start
call $start:issues/1714
)
)