Merge pull request #12044 from Vexu/stage2-compile-errors
Sema: add detailed error notes to `coerceInMemoryAllowed`
This commit is contained in:
@@ -49,6 +49,10 @@ pub const Loc = struct {
|
||||
column: usize,
|
||||
/// Does not include the trailing newline.
|
||||
source_line: []const u8,
|
||||
|
||||
pub fn eql(a: Loc, b: Loc) bool {
|
||||
return a.line == b.line and a.column == b.column and std.mem.eql(u8, a.source_line, b.source_line);
|
||||
}
|
||||
};
|
||||
|
||||
pub fn findLineColumn(source: []const u8, byte_offset: usize) Loc {
|
||||
|
||||
@@ -526,6 +526,9 @@ pub const AllErrors = struct {
|
||||
Message.HashContext,
|
||||
std.hash_map.default_max_load_percentage,
|
||||
).init(allocator);
|
||||
const err_source = try module_err_msg.src_loc.file_scope.getSource(module.gpa);
|
||||
const err_byte_offset = try module_err_msg.src_loc.byteOffset(module.gpa);
|
||||
const err_loc = std.zig.findLineColumn(err_source.bytes, err_byte_offset);
|
||||
|
||||
for (module_err_msg.notes) |module_note| {
|
||||
const source = try module_note.src_loc.file_scope.getSource(module.gpa);
|
||||
@@ -540,7 +543,7 @@ pub const AllErrors = struct {
|
||||
.byte_offset = byte_offset,
|
||||
.line = @intCast(u32, loc.line),
|
||||
.column = @intCast(u32, loc.column),
|
||||
.source_line = try allocator.dupe(u8, loc.source_line),
|
||||
.source_line = if (err_loc.eql(loc)) null else try allocator.dupe(u8, loc.source_line),
|
||||
},
|
||||
};
|
||||
const gop = try seen_notes.getOrPut(note);
|
||||
@@ -558,19 +561,16 @@ pub const AllErrors = struct {
|
||||
});
|
||||
return;
|
||||
}
|
||||
const source = try module_err_msg.src_loc.file_scope.getSource(module.gpa);
|
||||
const byte_offset = try module_err_msg.src_loc.byteOffset(module.gpa);
|
||||
const loc = std.zig.findLineColumn(source.bytes, byte_offset);
|
||||
const file_path = try module_err_msg.src_loc.file_scope.fullPath(allocator);
|
||||
try errors.append(.{
|
||||
.src = .{
|
||||
.src_path = file_path,
|
||||
.msg = try allocator.dupe(u8, module_err_msg.msg),
|
||||
.byte_offset = byte_offset,
|
||||
.line = @intCast(u32, loc.line),
|
||||
.column = @intCast(u32, loc.column),
|
||||
.byte_offset = err_byte_offset,
|
||||
.line = @intCast(u32, err_loc.line),
|
||||
.column = @intCast(u32, err_loc.column),
|
||||
.notes = notes_buf[0..note_i],
|
||||
.source_line = try allocator.dupe(u8, loc.source_line),
|
||||
.source_line = try allocator.dupe(u8, err_loc.source_line),
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -593,6 +593,16 @@ pub const AllErrors = struct {
|
||||
while (item_i < items_len) : (item_i += 1) {
|
||||
const item = file.zir.extraData(Zir.Inst.CompileErrors.Item, extra_index);
|
||||
extra_index = item.end;
|
||||
const err_byte_offset = blk: {
|
||||
const token_starts = file.tree.tokens.items(.start);
|
||||
if (item.data.node != 0) {
|
||||
const main_tokens = file.tree.nodes.items(.main_token);
|
||||
const main_token = main_tokens[item.data.node];
|
||||
break :blk token_starts[main_token];
|
||||
}
|
||||
break :blk token_starts[item.data.token] + item.data.byte_offset;
|
||||
};
|
||||
const err_loc = std.zig.findLineColumn(file.source, err_byte_offset);
|
||||
|
||||
var notes: []Message = &[0]Message{};
|
||||
if (item.data.notes != 0) {
|
||||
@@ -621,33 +631,22 @@ pub const AllErrors = struct {
|
||||
.line = @intCast(u32, loc.line),
|
||||
.column = @intCast(u32, loc.column),
|
||||
.notes = &.{}, // TODO rework this function to be recursive
|
||||
.source_line = try arena.dupe(u8, loc.source_line),
|
||||
.source_line = if (loc.eql(err_loc)) null else try arena.dupe(u8, loc.source_line),
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const msg = file.zir.nullTerminatedString(item.data.msg);
|
||||
const byte_offset = blk: {
|
||||
const token_starts = file.tree.tokens.items(.start);
|
||||
if (item.data.node != 0) {
|
||||
const main_tokens = file.tree.nodes.items(.main_token);
|
||||
const main_token = main_tokens[item.data.node];
|
||||
break :blk token_starts[main_token];
|
||||
}
|
||||
break :blk token_starts[item.data.token] + item.data.byte_offset;
|
||||
};
|
||||
const loc = std.zig.findLineColumn(file.source, byte_offset);
|
||||
|
||||
try errors.append(.{
|
||||
.src = .{
|
||||
.src_path = try file.fullPath(arena),
|
||||
.msg = try arena.dupe(u8, msg),
|
||||
.byte_offset = byte_offset,
|
||||
.line = @intCast(u32, loc.line),
|
||||
.column = @intCast(u32, loc.column),
|
||||
.byte_offset = err_byte_offset,
|
||||
.line = @intCast(u32, err_loc.line),
|
||||
.column = @intCast(u32, err_loc.column),
|
||||
.notes = notes,
|
||||
.source_line = try arena.dupe(u8, loc.source_line),
|
||||
.source_line = try arena.dupe(u8, err_loc.source_line),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
779
src/Sema.zig
779
src/Sema.zig
File diff suppressed because it is too large
Load Diff
@@ -2,4 +2,5 @@ pub export fn main() noreturn {}
|
||||
|
||||
// error
|
||||
//
|
||||
// :1:32: error: expected type 'noreturn', found 'void'
|
||||
// :1:32: error: function declared 'noreturn' returns
|
||||
// :1:22: note: 'noreturn' declared here
|
||||
|
||||
@@ -8,3 +8,5 @@ export fn entry() usize { return @sizeOf(@TypeOf(&foo)); }
|
||||
// target=native
|
||||
//
|
||||
// :3:30: error: expected type '*const i32', found '*const comptime_int'
|
||||
// :3:30: note: pointer type child 'comptime_int' cannot cast into pointer type child 'i32'
|
||||
// :3:10: note: function return type declared here
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
pub fn main() void {
|
||||
pub export fn entry() void {
|
||||
var a: ?*anyopaque = undefined;
|
||||
a = @as(?usize, null);
|
||||
}
|
||||
|
||||
// error
|
||||
// output_mode=Exe
|
||||
// backend=stage2,llvm
|
||||
// target=x86_64-linux,x86_64-macos
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :3:21: error: expected type '*anyopaque', found '?usize'
|
||||
// :3:21: error: expected type '?*anyopaque', found '?usize'
|
||||
// :3:21: note: optional type child 'usize' cannot cast into optional type child '*anyopaque'
|
||||
|
||||
@@ -15,7 +15,9 @@ export fn entry() void {
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:11:27: error: cannot convert optional to payload type. consider using `.?`, `orelse`, or `if`. expected type 'u8', found '?u8'
|
||||
// :11:27: error: expected type 'u8', found '?u8'
|
||||
// :11:27: note: cannot convert optional to payload type
|
||||
// :11:27: note: consider using `.?`, `orelse`, or `if`
|
||||
@@ -7,8 +7,8 @@ export fn entry(byte: u8) void {
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:4:22: error: expected type '*[1]i32', found '*const i32'
|
||||
// tmp.zig:4:22: note: cast discards const qualifier
|
||||
// :4:22: error: expected type '*[1]i32', found '*const i32'
|
||||
// :4:22: note: cast discards const qualifier
|
||||
@@ -0,0 +1,26 @@
|
||||
pub const fnty1 = ?*const fn (i8) void;
|
||||
pub const fnty2 = ?*const fn (u64) void;
|
||||
export fn entry1() void {
|
||||
var a: fnty1 = undefined;
|
||||
var b: fnty2 = undefined;
|
||||
a = b;
|
||||
}
|
||||
|
||||
pub const fnty3 = ?*const fn (u63) void;
|
||||
export fn entry2() void {
|
||||
var a: fnty3 = undefined;
|
||||
var b: fnty2 = undefined;
|
||||
a = b;
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :6:9: error: expected type '?*const fn(i8) void', found '?*const fn(u64) void'
|
||||
// :6:9: note: pointer type child 'fn(u64) void' cannot cast into pointer type child 'fn(i8) void'
|
||||
// :6:9: note: parameter 0 'u64' cannot cast into 'i8'
|
||||
// :6:9: note: unsigned 64-bit int cannot represent all possible signed 8-bit values
|
||||
// :13:9: error: expected type '?*const fn(u63) void', found '?*const fn(u64) void'
|
||||
// :13:9: note: pointer type child 'fn(u64) void' cannot cast into pointer type child 'fn(u63) void'
|
||||
// :13:9: note: parameter 0 'u64' cannot cast into 'u63'
|
||||
@@ -0,0 +1,15 @@
|
||||
const SmallErrorSet = error{A};
|
||||
export fn entry() void {
|
||||
var x: SmallErrorSet!i32 = foo();
|
||||
_ = x;
|
||||
}
|
||||
fn foo() anyerror!i32 {
|
||||
return error.B;
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :3:35: error: expected type 'error{A}!i32', found 'anyerror!i32'
|
||||
// :3:35: note: global error set cannot cast into a smaller set
|
||||
@@ -8,8 +8,8 @@ fn foo() anyerror {
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:3:31: error: expected type 'SmallErrorSet', found 'anyerror'
|
||||
// tmp.zig:3:31: note: cannot cast global error set into smaller set
|
||||
// :3:31: error: expected type 'error{A}', found 'anyerror'
|
||||
// :3:31: note: global error set cannot cast into a smaller set
|
||||
@@ -19,3 +19,5 @@ export fn entry() usize { return @sizeOf(@TypeOf(&foo)); }
|
||||
// target=native
|
||||
//
|
||||
// :8:15: error: expected type '*const u3', found '*align(0:3:1) const u3'
|
||||
// :8:15: note: pointer host size '1' cannot cast into pointer host size '0'
|
||||
// :8:15: note: pointer bit offset '3' cannot cast into pointer bit offset '0'
|
||||
|
||||
@@ -9,4 +9,5 @@ fn foo() !void {
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :2:12: error: error is discarded. consider using `try`, `catch`, or `if`
|
||||
// :2:12: error: error is discarded
|
||||
// :2:12: note: consider using `try`, `catch`, or `if`
|
||||
|
||||
@@ -11,3 +11,4 @@ export fn entry() void {
|
||||
// target=native
|
||||
//
|
||||
// :5:28: error: expected type '*anyopaque', found '**u32'
|
||||
// :5:28: note: pointer type child '*u32' cannot cast into pointer type child 'anyopaque'
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
fn do_the_thing(func: *const fn (arg: i32) void) void { _ = func; }
|
||||
fn bar(arg: bool) void { _ = arg; }
|
||||
export fn entry() void {
|
||||
do_the_thing(bar);
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :4:17: error: expected type '*const fn(i32) void', found '*const fn(bool) void'
|
||||
// :4:17: note: pointer type child 'fn(bool) void' cannot cast into pointer type child 'fn(i32) void'
|
||||
// :4:17: note: parameter 0 'bool' cannot cast into 'i32'
|
||||
@@ -7,4 +7,5 @@ fn bar() anyerror!i32 { return 0; }
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :2:14: error: error is ignored. consider using `try`, `catch`, or `if`
|
||||
// :2:14: error: error is ignored
|
||||
// :2:14: note: consider using `try`, `catch`, or `if`
|
||||
|
||||
@@ -17,6 +17,9 @@ fn bad() anyerror!void {
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :2:24: error: error is ignored. consider using `try`, `catch`, or `if`
|
||||
// :6:25: error: error is ignored. consider using `try`, `catch`, or `if`
|
||||
// :10:25: error: error is ignored. consider using `try`, `catch`, or `if`
|
||||
// :2:24: error: error is ignored
|
||||
// :2:24: note: consider using `try`, `catch`, or `if`
|
||||
// :6:25: error: error is ignored
|
||||
// :6:25: note: consider using `try`, `catch`, or `if`
|
||||
// :10:25: error: error is ignored
|
||||
// :10:25: note: consider using `try`, `catch`, or `if`
|
||||
|
||||
@@ -9,8 +9,8 @@ fn foo(set1: Set1) void {
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:7:19: error: expected type 'Set2', found 'Set1'
|
||||
// tmp.zig:1:23: note: 'error.B' not a member of destination error set
|
||||
// :7:19: error: expected type 'error{A,C}', found 'error{A,B}'
|
||||
// :7:19: note: 'error.B' not a member of destination error set
|
||||
@@ -0,0 +1,15 @@
|
||||
const std = @import("std");
|
||||
export fn entry1() void {
|
||||
_ = @as([*c]u8, @as(u65, std.math.maxInt(u65)));
|
||||
}
|
||||
export fn entry2() void {
|
||||
_ = @as([*c]u8, std.math.maxInt(u65));
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :3:21: error: expected type '[*c]u8', found 'u65'
|
||||
// :3:21: note: unsigned 64-bit int cannot represent all possible unsigned 65-bit values
|
||||
// :6:36: error: expected type '[*c]u8', found 'comptime_int'
|
||||
@@ -0,0 +1,26 @@
|
||||
export fn entry() void {
|
||||
var slice: []const u8 = "aoeu";
|
||||
const opt_many_ptr: [*]const u8 = slice.ptr;
|
||||
var ptr_opt_many_ptr = &opt_many_ptr;
|
||||
var c_ptr: [*c]const [*c]const u8 = ptr_opt_many_ptr;
|
||||
ptr_opt_many_ptr = c_ptr;
|
||||
}
|
||||
export fn entry2() void {
|
||||
var buf: [4]u8 = "aoeu".*;
|
||||
var slice: []u8 = &buf;
|
||||
var opt_many_ptr: [*]u8 = slice.ptr;
|
||||
var ptr_opt_many_ptr = &opt_many_ptr;
|
||||
var c_ptr: [*c][*c]const u8 = ptr_opt_many_ptr;
|
||||
_ = c_ptr;
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :6:24: error: expected type '*const [*]const u8', found '[*c]const [*c]const u8'
|
||||
// :6:24: note: pointer type child '[*c]const u8' cannot cast into pointer type child '[*]const u8'
|
||||
// :6:24: note: '[*c]const u8' could have null values which are illegal in type '[*]const u8'
|
||||
// :13:35: error: expected type '[*c][*c]const u8', found '*[*]u8'
|
||||
// :13:35: note: pointer type child '[*]u8' cannot cast into pointer type child '[*c]const u8'
|
||||
// :13:35: note: mutable '[*]u8' allows illegal null values stored to type '[*c]const u8'
|
||||
@@ -15,3 +15,4 @@ export fn entry() void {
|
||||
// target=native
|
||||
//
|
||||
// :9:22: error: expected type 'u2', found 'tmp.Small'
|
||||
// :1:15: note: enum declared here
|
||||
|
||||
31
test/cases/compile_errors/incompatible_sentinels.zig
Normal file
31
test/cases/compile_errors/incompatible_sentinels.zig
Normal file
@@ -0,0 +1,31 @@
|
||||
// Note: One of the error messages here is backwards. It would be nice to fix, but that's not
|
||||
// going to stop me from merging this branch which fixes a bunch of other stuff.
|
||||
export fn entry1(ptr: [*:255]u8) [*:0]u8 {
|
||||
return ptr;
|
||||
}
|
||||
export fn entry2(ptr: [*]u8) [*:0]u8 {
|
||||
return ptr;
|
||||
}
|
||||
export fn entry3() void {
|
||||
var array: [2:0]u8 = [_:255]u8{ 1, 2 };
|
||||
_ = array;
|
||||
}
|
||||
export fn entry4() void {
|
||||
var array: [2:0]u8 = [_]u8{ 1, 2 };
|
||||
_ = array;
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :4:12: error: expected type '[*:0]u8', found '[*:255]u8'
|
||||
// :4:12: note: pointer sentinel '255' cannot cast into pointer sentinel '0'
|
||||
// :3:35: note: function return type declared here
|
||||
// :7:12: error: expected type '[*:0]u8', found '[*]u8'
|
||||
// :7:12: note: destination pointer requires '0' sentinel
|
||||
// :6:31: note: function return type declared here
|
||||
// :10:35: error: expected type '[2:0]u8', found '[2:255]u8'
|
||||
// :10:35: note: array sentinel '255' cannot cast into array sentinel '0'
|
||||
// :14:31: error: expected type '[2:0]u8', found '[2]u8'
|
||||
// :14:31: note: destination array requires '0' sentinel
|
||||
@@ -19,3 +19,6 @@
|
||||
// target=native
|
||||
//
|
||||
// :8:16: error: expected type 'tmp.A', found 'tmp.B'
|
||||
// :10:12: note: struct declared here
|
||||
// :4:12: note: struct declared here
|
||||
// :7:11: note: function return type declared here
|
||||
|
||||
@@ -11,3 +11,5 @@ pub fn main() void {
|
||||
// target=x86_64-linux,x86_64-macos
|
||||
//
|
||||
// :2:12: error: expected type '*i32', found '*addrspace(.gs) i32'
|
||||
// :2:12: note: address space 'gs' cannot cast into address space 'generic'
|
||||
// :1:34: note: function return type declared here
|
||||
|
||||
@@ -15,3 +15,4 @@ fn foo(x: usize) void {
|
||||
// target=native
|
||||
//
|
||||
// :9:10: error: expected type 'usize', found 'tmp.E'
|
||||
// :1:11: note: enum declared here
|
||||
|
||||
@@ -11,3 +11,5 @@ pub fn main() void {
|
||||
// target=x86_64-linux,x86_64-macos
|
||||
//
|
||||
// :2:12: error: expected type '*i32', found '*addrspace(.gs) i32'
|
||||
// :2:12: note: address space 'gs' cannot cast into address space 'generic'
|
||||
// :1:34: note: function return type declared here
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
export fn foo() void {
|
||||
var u: ?*anyopaque = null;
|
||||
var v: *anyopaque = undefined;
|
||||
v = u;
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :4:9: error: expected type '*anyopaque', found '?*anyopaque'
|
||||
// :4:9: note: cannot convert optional to payload type
|
||||
// :4:9: note: consider using `.?`, `orelse`, or `if`
|
||||
// :4:9: note: '?*anyopaque' could have null values which are illegal in type '*anyopaque'
|
||||
@@ -17,3 +17,4 @@ const ExpectedVarDeclOrFn = struct {};
|
||||
// target=native
|
||||
//
|
||||
// :4:9: error: expected type '@typeInfo(tmp.Error).Union.tag_type.?', found 'type'
|
||||
// :8:1: note: enum declared here
|
||||
|
||||
@@ -10,3 +10,4 @@ export fn entry() bool {
|
||||
// target=native
|
||||
//
|
||||
// :4:31: error: expected type '*i32', found '*align(1) i32'
|
||||
// :4:31: note: pointer alignment '1' cannot cast into pointer alignment '4'
|
||||
|
||||
@@ -11,3 +11,5 @@ export fn entry2() void {
|
||||
// target=x86_64-linux,x86_64-macos
|
||||
//
|
||||
// :2:12: error: expected type '*addrspace(.fs) i32', found '*addrspace(.gs) i32'
|
||||
// :2:12: note: address space 'gs' cannot cast into address space 'fs'
|
||||
// :1:34: note: function return type declared here
|
||||
|
||||
@@ -11,3 +11,5 @@ pub fn main() void {
|
||||
// target=x86_64-linux,x86_64-macos
|
||||
//
|
||||
// :2:13: error: expected type '*i32', found '*addrspace(.gs) i32'
|
||||
// :2:13: note: address space 'gs' cannot cast into address space 'generic'
|
||||
// :1:35: note: function return type declared here
|
||||
|
||||
@@ -15,7 +15,9 @@ export fn entry() void {
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:12:25: error: cannot convert error union to payload type. consider using `try`, `catch`, or `if`. expected type 'u32', found '@typeInfo(@typeInfo(@TypeOf(get_uval)).Fn.return_type.?).ErrorUnion.error_set!u32'
|
||||
// :12:25: error: expected type 'u32', found '@typeInfo(@typeInfo(@TypeOf(tmp.get_uval)).Fn.return_type.?).ErrorUnion.error_set!u32'
|
||||
// :12:25: note: cannot convert error union to payload type
|
||||
// :12:25: note: consider using `try`, `catch`, or `if`
|
||||
@@ -12,7 +12,9 @@ pub const Container = struct {
|
||||
};
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:3:36: error: cannot convert optional to payload type. consider using `.?`, `orelse`, or `if`. expected type 'i32', found '?i32'
|
||||
// :3:36: error: expected type 'i32', found '?i32'
|
||||
// :3:36: note: cannot convert optional to payload type
|
||||
// :3:36: note: consider using `.?`, `orelse`, or `if`
|
||||
@@ -12,7 +12,9 @@ pub const Container = struct {
|
||||
};
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:3:36: error: cannot convert optional to payload type. consider using `.?`, `orelse`, or `if`. expected type 'i32', found '?i32'
|
||||
// :3:36: error: expected type 'i32', found '?i32'
|
||||
// :3:36: note: cannot convert optional to payload type
|
||||
// :3:36: note: consider using `.?`, `orelse`, or `if`
|
||||
@@ -7,3 +7,4 @@ export fn entry(x: u8, y: u8) u8 {
|
||||
// target=native
|
||||
//
|
||||
// :2:17: error: expected type 'u3', found 'u8'
|
||||
// :2:17: note: unsigned 3-bit int cannot represent all possible unsigned 8-bit values
|
||||
|
||||
13
test/cases/compile_errors/slice_sentinel_mismatch-2.zig
Normal file
13
test/cases/compile_errors/slice_sentinel_mismatch-2.zig
Normal file
@@ -0,0 +1,13 @@
|
||||
fn foo() [:0]u8 {
|
||||
var x: []u8 = undefined;
|
||||
return x;
|
||||
}
|
||||
comptime { _ = foo; }
|
||||
|
||||
// error
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :3:12: error: expected type '[:0]u8', found '[]u8'
|
||||
// :3:12: note: destination pointer requires '0' sentinel
|
||||
// :1:10: note: function return type declared here
|
||||
@@ -1,16 +0,0 @@
|
||||
const SmallErrorSet = error{A};
|
||||
export fn entry() void {
|
||||
var x: SmallErrorSet!i32 = foo();
|
||||
_ = x;
|
||||
}
|
||||
fn foo() anyerror!i32 {
|
||||
return error.B;
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:3:35: error: expected type 'SmallErrorSet!i32', found 'anyerror!i32'
|
||||
// tmp.zig:3:35: note: error set 'anyerror' cannot cast into error set 'SmallErrorSet'
|
||||
// tmp.zig:3:35: note: cannot cast global error set into smaller set
|
||||
@@ -1,12 +0,0 @@
|
||||
fn do_the_thing(func: fn (arg: i32) void) void { _ = func; }
|
||||
fn bar(arg: bool) void { _ = arg; }
|
||||
export fn entry() void {
|
||||
do_the_thing(bar);
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:4:18: error: expected type 'fn(i32) void', found 'fn(bool) void
|
||||
// tmp.zig:4:18: note: parameter 0: 'bool' cannot cast into 'i32'
|
||||
@@ -1,26 +0,0 @@
|
||||
export fn entry() void {
|
||||
var slice: []const u8 = "aoeu";
|
||||
const opt_many_ptr: [*]const u8 = slice.ptr;
|
||||
var ptr_opt_many_ptr = &opt_many_ptr;
|
||||
var c_ptr: [*c]const [*c]const u8 = ptr_opt_many_ptr;
|
||||
ptr_opt_many_ptr = c_ptr;
|
||||
}
|
||||
export fn entry2() void {
|
||||
var buf: [4]u8 = "aoeu".*;
|
||||
var slice: []u8 = &buf;
|
||||
var opt_many_ptr: [*]u8 = slice.ptr;
|
||||
var ptr_opt_many_ptr = &opt_many_ptr;
|
||||
var c_ptr: [*c][*c]const u8 = ptr_opt_many_ptr;
|
||||
_ = c_ptr;
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:6:24: error: expected type '*const [*]const u8', found '[*c]const [*c]const u8'
|
||||
// tmp.zig:6:24: note: pointer type child '[*c]const u8' cannot cast into pointer type child '[*]const u8'
|
||||
// tmp.zig:6:24: note: '[*c]const u8' could have null values which are illegal in type '[*]const u8'
|
||||
// tmp.zig:13:35: error: expected type '[*c][*c]const u8', found '*[*]u8'
|
||||
// tmp.zig:13:35: note: pointer type child '[*]u8' cannot cast into pointer type child '[*c]const u8'
|
||||
// tmp.zig:13:35: note: mutable '[*c]const u8' allows illegal null values stored to type '[*]u8'
|
||||
@@ -1,29 +0,0 @@
|
||||
// Note: One of the error messages here is backwards. It would be nice to fix, but that's not
|
||||
// going to stop me from merging this branch which fixes a bunch of other stuff.
|
||||
export fn entry1(ptr: [*:255]u8) [*:0]u8 {
|
||||
return ptr;
|
||||
}
|
||||
export fn entry2(ptr: [*]u8) [*:0]u8 {
|
||||
return ptr;
|
||||
}
|
||||
export fn entry3() void {
|
||||
var array: [2:0]u8 = [_:255]u8{ 1, 2 };
|
||||
_ = array;
|
||||
}
|
||||
export fn entry4() void {
|
||||
var array: [2:0]u8 = [_]u8{ 1, 2 };
|
||||
_ = array;
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:4:12: error: expected type '[*:0]u8', found '[*:255]u8'
|
||||
// tmp.zig:4:12: note: destination pointer requires a terminating '0' sentinel, but source pointer has a terminating '255' sentinel
|
||||
// tmp.zig:7:12: error: expected type '[*:0]u8', found '[*]u8'
|
||||
// tmp.zig:7:12: note: destination pointer requires a terminating '0' sentinel
|
||||
// tmp.zig:10:35: error: expected type '[2:255]u8', found '[2:0]u8'
|
||||
// tmp.zig:10:35: note: destination array requires a terminating '255' sentinel, but source array has a terminating '0' sentinel
|
||||
// tmp.zig:14:31: error: expected type '[2:0]u8', found '[2]u8'
|
||||
// tmp.zig:14:31: note: destination array requires a terminating '0' sentinel
|
||||
@@ -1,11 +0,0 @@
|
||||
export fn foo() void {
|
||||
var u: ?*anyopaque = null;
|
||||
var v: *anyopaque = undefined;
|
||||
v = u;
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:4:9: error: cannot convert optional to payload type. consider using `.?`, `orelse`, or `if`. expected type '*anyopaque', found '?*anyopaque'
|
||||
@@ -1,12 +0,0 @@
|
||||
fn foo() [:0]u8 {
|
||||
var x: []u8 = undefined;
|
||||
return x;
|
||||
}
|
||||
comptime { _ = foo; }
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:3:12: error: expected type '[:0]u8', found '[]u8'
|
||||
// tmp.zig:3:12: note: destination pointer requires a terminating '0' sentinel
|
||||
@@ -1,13 +0,0 @@
|
||||
fn a(b: fn (*const u8) void) void {
|
||||
b('a');
|
||||
}
|
||||
fn c(d: u8) void {_ = d;}
|
||||
export fn entry() void {
|
||||
a(c);
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// target=native
|
||||
//
|
||||
// tmp.zig:6:7: error: expected type 'fn(*const u8) void', found 'fn(u8) void'
|
||||
@@ -1,15 +0,0 @@
|
||||
pub const fnty1 = ?fn (i8) void;
|
||||
pub const fnty2 = ?fn (u64) void;
|
||||
export fn entry() void {
|
||||
var a: fnty1 = undefined;
|
||||
var b: fnty2 = undefined;
|
||||
a = b;
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// target=native
|
||||
// is_test=1
|
||||
//
|
||||
// tmp.zig:6:9: error: expected type '?fn(i8) void', found '?fn(u64) void'
|
||||
// tmp.zig:6:9: note: optional type child 'fn(u64) void' cannot cast into optional type child 'fn(i8) void'
|
||||
@@ -16,15 +16,17 @@ export fn quux() u32 {
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage1
|
||||
// backend=stage2
|
||||
// target=native
|
||||
// is_test=1
|
||||
//
|
||||
// tmp.zig:2:17: error: expected type 'u32', found 'error{Ohno}'
|
||||
// tmp.zig:1:17: note: function cannot return an error
|
||||
// tmp.zig:8:5: error: expected type 'void', found '@typeInfo(@typeInfo(@TypeOf(bar)).Fn.return_type.?).ErrorUnion.error_set'
|
||||
// tmp.zig:7:17: note: function cannot return an error
|
||||
// tmp.zig:11:15: error: cannot convert error union to payload type. consider using `try`, `catch`, or `if`. expected type 'u32', found '@typeInfo(@typeInfo(@TypeOf(bar)).Fn.return_type.?).ErrorUnion.error_set!u32'
|
||||
// tmp.zig:10:17: note: function cannot return an error
|
||||
// tmp.zig:15:14: error: cannot convert error union to payload type. consider using `try`, `catch`, or `if`. expected type 'u32', found '@typeInfo(@typeInfo(@TypeOf(bar)).Fn.return_type.?).ErrorUnion.error_set!u32'
|
||||
// tmp.zig:14:5: note: cannot store an error in type 'u32'
|
||||
// :2:18: error: expected type 'u32', found 'error{Ohno}'
|
||||
// :1:17: note: function cannot return an error
|
||||
// :8:5: error: expected type 'void', found '@typeInfo(@typeInfo(@TypeOf(tmp.bar)).Fn.return_type.?).ErrorUnion.error_set'
|
||||
// :7:17: note: function cannot return an error
|
||||
// :11:15: error: expected type 'u32', found '@typeInfo(@typeInfo(@TypeOf(tmp.bar)).Fn.return_type.?).ErrorUnion.error_set!u32'
|
||||
// :10:17: note: function cannot return an error
|
||||
// :11:15: note: cannot convert error union to payload type
|
||||
// :11:15: note: consider using `try`, `catch`, or `if`
|
||||
// :15:14: error: expected type 'u32', found '@typeInfo(@typeInfo(@TypeOf(tmp.bar)).Fn.return_type.?).ErrorUnion.error_set!u32'
|
||||
// :15:14: note: cannot convert error union to payload type
|
||||
// :15:14: note: consider using `try`, `catch`, or `if`
|
||||
|
||||
@@ -8,3 +8,4 @@ fn something() anyerror!void { }
|
||||
// target=native
|
||||
//
|
||||
// :2:5: error: expected type 'void', found 'anyerror'
|
||||
// :1:15: note: function cannot return an error
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
fn a(b: *const fn (*const u8) void) void {
|
||||
_ = b;
|
||||
}
|
||||
fn c(d: u8) void {_ = d;}
|
||||
export fn entry() void {
|
||||
a(c);
|
||||
}
|
||||
|
||||
// error
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :6:6: error: expected type '*const fn(*const u8) void', found '*const fn(u8) void'
|
||||
// :6:6: note: pointer type child 'fn(u8) void' cannot cast into pointer type child 'fn(*const u8) void'
|
||||
// :6:6: note: parameter 0 'u8' cannot cast into '*const u8'
|
||||
@@ -10,4 +10,6 @@ export fn main() void {
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :5:22: error: expected type 'fn([*c]u8, ...) callconv(.C) void', found 'fn([*:0]u8, ...) callconv(.C) void'
|
||||
// :5:22: error: expected type '?fn([*c]u8, ...) callconv(.C) void', found 'fn([*:0]u8, ...) callconv(.C) void'
|
||||
// :5:22: note: parameter 0 '[*:0]u8' cannot cast into '[*c]u8'
|
||||
// :5:22: note: '[*c]u8' could have null values which are illegal in type '[*:0]u8'
|
||||
|
||||
@@ -5,4 +5,5 @@ export fn entry() void { a(); }
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :1:18: error: expected type 'noreturn', found 'void'
|
||||
// :1:18: error: function declared 'noreturn' returns
|
||||
// :1:8: note: 'noreturn' declared here
|
||||
|
||||
@@ -8,3 +8,4 @@ export fn f() i32 {
|
||||
// target=native
|
||||
//
|
||||
// :3:12: error: expected type 'i32', found '*const [1:0]u8'
|
||||
// :1:15: note: function return type declared here
|
||||
|
||||
@@ -7,3 +7,4 @@ export fn entry() void {
|
||||
// target=native
|
||||
//
|
||||
// :2:15: error: expected type 'builtin.Type', found 'comptime_int'
|
||||
// :?:?: note: union declared here
|
||||
|
||||
@@ -2,4 +2,5 @@ pub export fn _start() noreturn {}
|
||||
|
||||
// error
|
||||
//
|
||||
// :1:34: error: expected type 'noreturn', found 'void'
|
||||
// :1:34: error: function declared 'noreturn' returns
|
||||
// :1:24: note: 'noreturn' declared here
|
||||
|
||||
@@ -2,4 +2,5 @@ pub export fn main() noreturn {}
|
||||
|
||||
// error
|
||||
//
|
||||
// :1:32: error: expected type 'noreturn', found 'void'
|
||||
// :1:32: error: function declared 'noreturn' returns
|
||||
// :1:22: note: 'noreturn' declared here
|
||||
|
||||
Reference in New Issue
Block a user