Skip to content

Upcast and downcast are switched #2422

Closed
@danaugrs

Description

@danaugrs

Hi! Thanks for building AssemblyScript!
I noticed that "upcast" and "downcast" are switched:

assemblyscript/src/compiler.ts

Lines 3472 to 3501 in 53af0e0

if (fromType.isAssignableTo(toType)) { // downcast or same
assert(toType.isExternalReference || fromType.kind == toType.kind);
this.currentType = toType;
return expr;
}
if (explicit && toType.nonNullableType.isAssignableTo(fromType)) { // upcast
// <Cat | null>(<Animal>maybeCat)
if (toType.isExternalReference) {
this.error(
DiagnosticCode.Not_implemented_0,
reportNode.range,
"ref.cast"
);
this.currentType = toType;
return module.unreachable();
}
assert(fromType.kind == toType.kind);
if (!this.options.noAssert) {
expr = this.makeRuntimeUpcastCheck(expr, fromType, toType, reportNode);
}
this.currentType = toType;
return expr;
}
this.error(
DiagnosticCode.Type_0_is_not_assignable_to_type_1,
reportNode.range, fromType.toString(), toType.toString()
);
this.currentType = toType;
return module.unreachable();
}

"Upcast" should be the straightforward, static case whereas "downcast" should be dynamic and more complex.
No logic changes are needed, just the words need to be switched, especially in the error message below:

this.ensureStaticString("unexpected upcast"),

For context, that error message was pointed out to be wrong in LimeChain/demo-subgraph#39.

I created a PR to fix this issue: #2423.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions