Skip to content

Commit f55fdc0

Browse files
committedDec 14, 2017
fix const and volatile qualifiers being dropped sometimes
in the expression `&const a.b`, the const (and/or volatile) qualifiers would be incorrectly dropped. closes #655
·
0.15.10.2.0
1 parent 84619ab commit f55fdc0

File tree

8 files changed

+39
-12
lines changed

8 files changed

+39
-12
lines changed
 

‎doc/docgen.zig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const State = enum {
4242

4343
// TODO look for code segments
4444

45-
fn gen(in: &io.InStream, out: &const io.OutStream) {
45+
fn gen(in: &io.InStream, out: &io.OutStream) {
4646
var state = State.Start;
4747
while (true) {
4848
const byte = in.readByte() %% |err| {

‎src/ir.cpp‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ static IrInstruction *ir_build_ptr_type_of(IrBuilder *irb, Scope *scope, AstNode
10251025
ptr_type_of_instruction->bit_offset_start = bit_offset_start;
10261026
ptr_type_of_instruction->bit_offset_end = bit_offset_end;
10271027

1028-
ir_ref_instruction(align_value, irb->current_basic_block);
1028+
if (align_value) ir_ref_instruction(align_value, irb->current_basic_block);
10291029
ir_ref_instruction(child_type, irb->current_basic_block);
10301030

10311031
return &ptr_type_of_instruction->base;
@@ -4897,13 +4897,18 @@ static IrInstruction *ir_gen_address_of(IrBuilder *irb, Scope *scope, AstNode *n
48974897
AstNode *expr_node = node->data.addr_of_expr.op_expr;
48984898
AstNode *align_expr = node->data.addr_of_expr.align_expr;
48994899

4900-
if (align_expr == nullptr) {
4900+
if (align_expr == nullptr && !is_const && !is_volatile) {
49014901
return ir_gen_node_extra(irb, expr_node, scope, make_lval_addr(is_const, is_volatile));
49024902
}
49034903

4904-
IrInstruction *align_value = ir_gen_node(irb, align_expr, scope);
4905-
if (align_value == irb->codegen->invalid_instruction)
4906-
return align_value;
4904+
IrInstruction *align_value;
4905+
if (align_expr != nullptr) {
4906+
align_value = ir_gen_node(irb, align_expr, scope);
4907+
if (align_value == irb->codegen->invalid_instruction)
4908+
return align_value;
4909+
} else {
4910+
align_value = nullptr;
4911+
}
49074912

49084913
IrInstruction *child_type = ir_gen_node(irb, expr_node, scope);
49094914
if (child_type == irb->codegen->invalid_instruction)
@@ -15959,8 +15964,12 @@ static TypeTableEntry *ir_analyze_instruction_ptr_type_of(IrAnalyze *ira, IrInst
1595915964
return ira->codegen->builtin_types.entry_invalid;
1596015965

1596115966
uint32_t align_bytes;
15962-
if (!ir_resolve_align(ira, instruction->align_value->other, &align_bytes))
15963-
return ira->codegen->builtin_types.entry_invalid;
15967+
if (instruction->align_value != nullptr) {
15968+
if (!ir_resolve_align(ira, instruction->align_value->other, &align_bytes))
15969+
return ira->codegen->builtin_types.entry_invalid;
15970+
} else {
15971+
align_bytes = get_abi_alignment(ira->codegen, child_type);
15972+
}
1596415973

1596515974
ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
1596615975
out_val->data.x_type = get_pointer_to_type_extra(ira->codegen, child_type,

‎src/ir_print.cpp‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,8 +886,12 @@ static void ir_print_can_implicit_cast(IrPrint *irp, IrInstructionCanImplicitCas
886886
}
887887

888888
static void ir_print_ptr_type_of(IrPrint *irp, IrInstructionPtrTypeOf *instruction) {
889-
fprintf(irp->f, "&align ");
890-
ir_print_other_instruction(irp, instruction->align_value);
889+
fprintf(irp->f, "&");
890+
if (instruction->align_value != nullptr) {
891+
fprintf(irp->f, "align(");
892+
ir_print_other_instruction(irp, instruction->align_value);
893+
fprintf(irp->f, ")");
894+
}
891895
const char *const_str = instruction->is_const ? "const " : "";
892896
const char *volatile_str = instruction->is_volatile ? "volatile " : "";
893897
fprintf(irp->f, ":%" PRIu32 ":%" PRIu32 " %s%s", instruction->bit_offset_start, instruction->bit_offset_end,

‎std/base64.zig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ pub const Base64DecoderWithIgnore = struct {
193193
/// Decoding more data than can fit in dest results in error.OutputTooSmall. See also ::calcSizeUpperBound.
194194
/// Returns the number of bytes writen to dest.
195195
pub fn decode(decoder_with_ignore: &const Base64DecoderWithIgnore, dest: []u8, source: []const u8) -> %usize {
196-
const decoder = &const decoder_with_ignore.decoder;
196+
const decoder = &decoder_with_ignore.decoder;
197197

198198
var src_cursor: usize = 0;
199199
var dest_cursor: usize = 0;

‎test/behavior.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ comptime {
77
_ = @import("cases/bitcast.zig");
88
_ = @import("cases/bool.zig");
99
_ = @import("cases/bugs/394.zig");
10+
_ = @import("cases/bugs/655.zig");
1011
_ = @import("cases/cast.zig");
1112
_ = @import("cases/const_slice_child.zig");
1213
_ = @import("cases/defer.zig");

‎test/cases/bugs/655.zig‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const std = @import("std");
2+
const other_file = @import("655_other_file.zig");
3+
4+
test "function with &const parameter with type dereferenced by namespace" {
5+
const x: other_file.Integer = 1234;
6+
comptime std.debug.assert(@typeOf(&x) == &const other_file.Integer);
7+
foo(x);
8+
}
9+
10+
fn foo(x: &const other_file.Integer) {
11+
std.debug.assert(*x == 1234);
12+
}

‎test/cases/bugs/655_other_file.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub const Integer = u32;

‎test/cases/misc.zig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ test "@typeName" {
504504

505505
test "volatile load and store" {
506506
var number: i32 = 1234;
507-
const ptr = &volatile number;
507+
const ptr = (&volatile i32)(&number);
508508
*ptr += 1;
509509
assert(*ptr == 1235);
510510
}

0 commit comments

Comments
 (0)
Please sign in to comment.