-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathcompile_errors.zig
253 lines (225 loc) · 8.37 KB
/
compile_errors.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
const std = @import("std");
const builtin = @import("builtin");
const Cases = @import("src/Cases.zig");
pub fn addCases(ctx: *Cases, b: *std.Build) !void {
// This test is currently disabled because the leading spaces aligning non-initial lines of the
// error message don't play nice with the test runner.
if (false) {
const case = ctx.obj("multiline error message", b.graph.host);
case.addError(
\\comptime {
\\ @compileError("hello\nworld");
\\}
, &[_][]const u8{
\\:2:5: error: hello
\\ world
});
}
// This test is currently disabled because the leading spaces aligning non-initial lines of the
// error message don't play nice with the test runner.
if (false) {
const case = ctx.obj("multiline error message with trailing newline", b.graph.host);
case.addError(
\\comptime {
\\ @compileError(
\\ \\
\\ \\hello!
\\ \\I'm a multiline error message.
\\ \\I hope to be very useful!
\\ \\
\\ \\also I will leave this trailing newline here if you don't mind
\\ \\
\\ );
\\}
, &[_][]const u8{
\\:2:5: error:
\\ hello!
\\ I'm a multiline error message.
\\ I hope to be very useful!
\\
\\ also I will leave this trailing newline here if you don't mind
\\
});
}
{
const case = ctx.obj("missing semicolon at EOF", b.graph.host);
case.addError(
\\const foo = 1
, &[_][]const u8{
\\:1:14: error: expected ';' after declaration
});
}
{
const case = ctx.obj("argument causes error", b.graph.host);
case.addError(
\\pub export fn entry() void {
\\ var lib: @import("b.zig").ElfDynLib = undefined;
\\ _ = lib.lookup(fn () void);
\\}
, &[_][]const u8{
":3:12: error: unable to resolve comptime value",
":3:19: note: call to generic function instantiated with comptime-only return type '?fn () void' is evaluated at comptime",
":2:55: note: return type declared here",
":3:19: note: use '*const fn () void' for a function pointer type",
});
case.addSourceFile("b.zig",
\\pub const ElfDynLib = struct {
\\ pub fn lookup(self: *ElfDynLib, comptime T: type) ?T {
\\ _ = self;
\\ return undefined;
\\ }
\\};
);
}
{
const case = ctx.obj("astgen failure in file struct", b.graph.host);
case.addError(
\\pub export fn entry() void {
\\ _ = (@sizeOf(@import("b.zig")));
\\}
, &[_][]const u8{
":1:1: error: expected type expression, found '+'",
});
case.addSourceFile("b.zig",
\\+
);
}
{
const case = ctx.obj("invalid store to comptime field", b.graph.host);
case.addError(
\\const a = @import("a.zig");
\\
\\export fn entry() void {
\\ _ = a.S.qux(a.S{ .foo = 2, .bar = 2 });
\\}
, &[_][]const u8{
":4:23: error: value stored in comptime field does not match the default value of the field",
":2:25: note: default value set here",
});
case.addSourceFile("a.zig",
\\pub const S = struct {
\\ comptime foo: u32 = 1,
\\ bar: u32,
\\ pub fn qux(x: @This()) void {
\\ _ = x;
\\ }
\\};
);
}
{
const case = ctx.obj("file in multiple modules", b.graph.host);
case.addDepModule("foo", "foo.zig");
case.addError(
\\comptime {
\\ _ = @import("foo");
\\ _ = @import("foo.zig");
\\}
, &[_][]const u8{
":1:1: error: file exists in multiple modules",
":1:1: note: root of module foo",
":3:17: note: imported from module root",
});
case.addSourceFile("foo.zig",
\\const dummy = 0;
);
}
{
const case = ctx.obj("wrong same named struct", b.graph.host);
case.addError(
\\const a = @import("a.zig");
\\const b = @import("b.zig");
\\
\\export fn entry() void {
\\ var a1: a.Foo = undefined;
\\ bar(&a1);
\\}
\\
\\fn bar(_: *b.Foo) void {}
, &[_][]const u8{
":6:9: error: expected type '*b.Foo', found '*a.Foo'",
":6:9: note: pointer type child 'a.Foo' cannot cast into pointer type child 'b.Foo'",
":1:17: note: struct declared here",
":1:17: note: struct declared here",
":9:11: note: parameter type declared here",
});
case.addSourceFile("a.zig",
\\pub const Foo = struct {
\\ x: i32,
\\};
);
case.addSourceFile("b.zig",
\\pub const Foo = struct {
\\ z: f64,
\\};
);
}
{
const case = ctx.obj("non-printable invalid character", b.graph.host);
case.addError("\xff\xfe" ++
\\export fn foo() bool {
\\ return true;
\\}
, &[_][]const u8{
":1:1: error: expected type expression, found 'invalid token'",
});
}
{
const case = ctx.obj("imported generic method call with invalid param", b.graph.host);
case.addError(
\\pub const import = @import("import.zig");
\\
\\export fn callComptimeBoolFunctionWithRuntimeBool(x: bool) void {
\\ import.comptimeBoolFunction(x);
\\}
\\
\\export fn callComptimeAnytypeFunctionWithRuntimeBool(x: bool) void {
\\ import.comptimeAnytypeFunction(x);
\\}
\\
\\export fn callAnytypeFunctionWithRuntimeComptimeOnlyType(x: u32) void {
\\ const S = struct { x: u32, y: type };
\\ import.anytypeFunction(S{ .x = x, .y = u32 });
\\}
, &[_][]const u8{
":4:33: error: unable to resolve comptime value",
":4:33: note: argument to comptime parameter must be comptime-known",
":1:29: note: parameter declared comptime here",
":8:36: error: unable to resolve comptime value",
":8:36: note: argument to comptime parameter must be comptime-known",
":2:32: note: parameter declared comptime here",
":13:32: error: unable to resolve comptime value",
":13:32: note: initializer of comptime-only struct 'tmp.callAnytypeFunctionWithRuntimeComptimeOnlyType.S' must be comptime-known",
":12:35: note: struct requires comptime because of this field",
":12:35: note: types are not available at runtime",
});
case.addSourceFile("import.zig",
\\pub fn comptimeBoolFunction(comptime _: bool) void {}
\\pub fn comptimeAnytypeFunction(comptime _: anytype) void {}
\\pub fn anytypeFunction(_: anytype) void {}
);
}
{
const case = ctx.obj("invalid byte in string", b.graph.host);
case.addError("_ = \"\x01Q\";", &[_][]const u8{
":1:6: error: string literal contains invalid byte: '\\x01'",
});
}
{
const case = ctx.obj("invalid byte in comment", b.graph.host);
case.addError("//\x01Q", &[_][]const u8{
":1:3: error: comment contains invalid byte: '\\x01'",
});
}
{
const case = ctx.obj("control character in character literal", b.graph.host);
case.addError("const c = '\x01';", &[_][]const u8{
":1:12: error: character literal contains invalid byte: '\\x01'",
});
}
{
const case = ctx.obj("invalid byte at start of token", b.graph.host);
case.addError("x = \x00Q", &[_][]const u8{
":1:5: error: expected expression, found 'invalid token'",
});
}
}