authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-02-27 23:55:03-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2018-02-27 23:55:03-05:00
log0f449a3ec180f710f8b54023d2c3b3dffcce5ec8
treedc68cb68c5fcdc634f5ccbd8d7f7577ffaed37bf
parent439621e44a68b436f958a84fcdb0bdac83613aea
parent9aa65c0e8e6e4135dcc04bcb388d1fa38c6d10f6
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #796 from bnoordhuis/fix731-more

allow implicit cast from &const to ?&const &const

2 files changed, 33 insertions(+), 1 deletions(-)

src/ir.cpp+2-1
......@@ -8830,7 +8830,8 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
88308830 }
88318831 } else if (wanted_child_type->id == TypeTableEntryIdPointer &&
88328832 wanted_child_type->data.pointer.is_const &&
8833 is_container(actual_type)) {
8833 (actual_type->id == TypeTableEntryIdPointer || is_container(actual_type)))
8834 {
88348835 IrInstruction *cast1 = ir_analyze_cast(ira, source_instr, wanted_child_type, value);
88358836 if (type_is_invalid(cast1->value.type))
88368837 return ira->codegen->invalid_instruction;
test/cases/cast.zig+31
......@@ -103,6 +103,37 @@ const Enum = enum {
103103 }
104104};
105105
106test "implicitly cast indirect pointer to maybe-indirect pointer" {
107 const S = struct {
108 const Self = this;
109 x: u8,
110 fn constConst(p: &const &const Self) u8 {
111 return (*p).x;
112 }
113 fn maybeConstConst(p: ?&const &const Self) u8 {
114 return (*??p).x;
115 }
116 fn constConstConst(p: &const &const &const Self) u8 {
117 return (**p).x;
118 }
119 fn maybeConstConstConst(p: ?&const &const &const Self) u8 {
120 return (**??p).x;
121 }
122 };
123 const s = S { .x = 42 };
124 const p = &s;
125 const q = &p;
126 const r = &q;
127 assert(42 == S.constConst(p));
128 assert(42 == S.constConst(q));
129 assert(42 == S.maybeConstConst(p));
130 assert(42 == S.maybeConstConst(q));
131 assert(42 == S.constConstConst(q));
132 assert(42 == S.constConstConst(r));
133 assert(42 == S.maybeConstConstConst(q));
134 assert(42 == S.maybeConstConstConst(r));
135}
136
106137test "explicit cast from integer to error type" {
107138 testCastIntToErr(error.ItBroke);
108139 comptime testCastIntToErr(error.ItBroke);