Skip to content

Commit

Permalink
Merge pull request #796 from bnoordhuis/fix731-more
Browse files Browse the repository at this point in the history
allow implicit cast from &const to ?&const &const
  • Loading branch information
andrewrk committed Feb 28, 2018
2 parents 439621e + 9aa65c0 commit 0f449a3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8830,7 +8830,8 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
}
} else if (wanted_child_type->id == TypeTableEntryIdPointer &&
wanted_child_type->data.pointer.is_const &&
is_container(actual_type)) {
(actual_type->id == TypeTableEntryIdPointer || is_container(actual_type)))
{
IrInstruction *cast1 = ir_analyze_cast(ira, source_instr, wanted_child_type, value);
if (type_is_invalid(cast1->value.type))
return ira->codegen->invalid_instruction;
Expand Down
31 changes: 31 additions & 0 deletions test/cases/cast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,37 @@ const Enum = enum {
}
};

test "implicitly cast indirect pointer to maybe-indirect pointer" {
const S = struct {
const Self = this;
x: u8,
fn constConst(p: &const &const Self) u8 {
return (*p).x;
}
fn maybeConstConst(p: ?&const &const Self) u8 {
return (*??p).x;
}
fn constConstConst(p: &const &const &const Self) u8 {
return (**p).x;
}
fn maybeConstConstConst(p: ?&const &const &const Self) u8 {
return (**??p).x;
}
};
const s = S { .x = 42 };
const p = &s;
const q = &p;
const r = &q;
assert(42 == S.constConst(p));
assert(42 == S.constConst(q));
assert(42 == S.maybeConstConst(p));
assert(42 == S.maybeConstConst(q));
assert(42 == S.constConstConst(q));
assert(42 == S.constConstConst(r));
assert(42 == S.maybeConstConstConst(q));
assert(42 == S.maybeConstConstConst(r));
}

test "explicit cast from integer to error type" {
testCastIntToErr(error.ItBroke);
comptime testCastIntToErr(error.ItBroke);
Expand Down

0 comments on commit 0f449a3

Please sign in to comment.