Closed ta3ta1 closed 7 years ago
Using as
operator on float literal also produce same result.
actor Main
new create(env: Env) =>
try
let n = 0.0 as F64
end
We need to fix the crash, probably in a similar way to the solution in #2035.
But from a larger perspective, we should probably add a check to the syntax
pass that looks for a numeric literal on the left side of an as
operator, and gives a compiler error telling you that this isn't how you give the inference hint for a numeric literal - the correct pattern is to use I64(0)
, not 0 as I64
.
actor Main
new create(env: Env) =>
try
if true then
1
else
2
end as I64
end
This also crash. I'll update PR #2041, adding a check to expr pass.
@jemc I'd argue that 0 as I64
should be allowed as a mean to specify a literal type. It's consistent with the explicit array syntax ([as I64: 0; 1; 2]
).
@Praetonus - if we allowed that, would it be an "error-raising" operation?
as
binary operator to sometimes be able to raise an error, and other times not.@jemc That's an interesting point.
One possibility would be to make as
an error-raising operation only when the resulting match would be non-exhaustive (i.e. when the LHS type isn't equivalent to the RHS type). Specifying literal types would probably be the only interesting operation with equivalent types on both sides of an as
, but at least we'd have a consistent rule.
Yeah, that's one option, though it does seem to go against the spirit of #1771, the purpose of which is to make it obvious to the reader from alone syntax where an error may be raised.
Good point, I think that's a compelling argument against that new rule for as
, and by extension against specifying literal types with as
.
Then the correct fix would be to issue a compiler error in expr_as
if the LHS type is a literal type.
Then the correct fix would be to issue a compiler error in expr_as if the LHS type is a literal type.
Agreed - we need to fix it there so that it covers all cases. However, I do think the change in #2041 would be a good additional measure, to provide a more helpful error message for the common, confusing case.
That is, I think we should do both - that's what I was getting at with this comment: https://github.com/ponylang/ponyc/issues/2037#issuecomment-315088733
When compiling following code,
compiler crashes and shows stacktrace.
Expected Result: No crash at least. I'm not sure what should be done.
Compile version: debug build from 2ca91f9ca30454070926124d4d84a23959c497ee