Skip to content

Commit cfe44a7

Browse files
Assume by default that most bindings are const
This fixes the resolved types of pointers to: - char literals, - number literals, - enum literals, - string literals, - error values, - builtin function calls, - and probably more
1 parent 25dbee9 commit cfe44a7

4 files changed

Lines changed: 35 additions & 6 deletions

File tree

src/analysis.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2825,7 +2825,7 @@ fn resolveBindingOfNodeUncached(analyser: *Analyser, node_handle: NodeWithHandle
28252825

28262826
else => return .{
28272827
.type = try analyser.resolveTypeOfNodeUncached(node_handle) orelse return null,
2828-
.is_const = false,
2828+
.is_const = true,
28292829
},
28302830
}
28312831
}

tests/analysis/address_of.zig

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
const char_literal_pointer = &'a';
2+
// ^^^^^^^^^^^^^^^^^^^^ (*const comptime_int)()
3+
4+
const int_literal_pointer = &0;
5+
// ^^^^^^^^^^^^^^^^^^^ (*const comptime_int)()
6+
7+
const enum_literal_pointer = &.foo;
8+
// ^^^^^^^^^^^^^^^^^^^^ (*const @Type(.enum_literal))()
9+
10+
const string_literal_pointer = &"foo";
11+
// ^^^^^^^^^^^^^^^^^^^^^^ (*const *const [3:0]u8)()
12+
13+
// zig fmt: off
14+
const multiline_string_literal_pointer = &
15+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ (*const *const [3:0]u8)()
16+
\\foo
17+
;
18+
// zig fmt: on
19+
20+
const error_value_pointer = &error.Foo;
21+
// ^^^^^^^^^^^^^^^^^^^ (*const error{Foo})()
22+
23+
const builtin_as_0 = &@as(i32, 0);
24+
// ^^^^^^^^^^^^ (*const i32)()
25+
26+
const builtin_as_1 = &@as(StructType, mutable_struct);
27+
// ^^^^^^^^^^^^ (*const StructType)()
28+
129
const StructType = struct {
230
foo: i32,
331
const const_decl: bool = true;

tests/analysis/array.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ var runtime_index: usize = 5;
2020
const some_array: [length]u8 = undefined;
2121
// ^^^^^^^^^^ ([3]u8)()
2222

23+
const some_array_pointer = &[3]u8{ 1, 2, 3 };
24+
// ^^^^^^^^^^^^^^^^^^ (*const [3]u8)()
25+
2326
const some_unsized_array: [unknown_length]u8 = undefined;
2427
// ^^^^^^^^^^^^^^^^^^ ([?]u8)()
2528

@@ -94,9 +97,8 @@ const array_cat_2 = [_]u8{0} ++ [1]u8{1};
9497
const array_cat_3 = [1]u8{0} ++ [1]u8{1};
9598
// ^^^^^^^^^^^ ([2]u8)()
9699

97-
// TODO this should be `*const [5]u8`
98100
const array_cat_4 = &[2]u8{ 0, 1 } ++ &[3]u8{ 2, 3, 4 };
99-
// ^^^^^^^^^^^ (*[5]u8)()
101+
// ^^^^^^^^^^^ (*const [5]u8)()
100102

101103
//
102104
// Array multiplication
@@ -108,9 +110,8 @@ const array_mult_0 = [_]u8{0} ** 2;
108110
const array_mult_1 = [1]u8{0} ** 2;
109111
// ^^^^^^^^^^^^ ([2]u8)()
110112

111-
// TODO this should be `*const [6]u8`
112113
const array_mult_2 = &[3]u8{ 0, 1, 2 } ** 2;
113-
// ^^^^^^^^^^^^ (*[6]u8)()
114+
// ^^^^^^^^^^^^ (*const [6]u8)()
114115

115116
comptime {
116117
// Use @compileLog to verify the expected type with the compiler:

tests/lsp_features/inlay_hints.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ test "var decl" {
243243
\\const foo<@TypeOf(undefined)> = undefined;
244244
, .{ .kind = .Type });
245245
try testInlayHints(
246-
\\const foo<**const [3:0]u8> = &"Bar";
246+
\\const foo<*const *const [3:0]u8> = &"Bar";
247247
, .{ .kind = .Type });
248248
try testInlayHints(
249249
\\const foo: *[]const u8 = &"Bar";

0 commit comments

Comments
 (0)