Skip to content

Commit b009aca

Browse files
jmc-88andrewrk
authored andcommitted
src: return a null-terminated slice
1 parent 08e5daa commit b009aca

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/stage1/ir.cpp

+6-14
Original file line numberDiff line numberDiff line change
@@ -24261,12 +24261,6 @@ static Stage1AirInst *ir_analyze_instruction_src(IrAnalyze *ira, Stage1ZirInstSr
2426124261
return ira->codegen->invalid_inst_gen;
2426224262
}
2426324263

24264-
ZigType *u8_ptr = get_pointer_to_type_extra2(
24265-
ira->codegen, ira->codegen->builtin_types.entry_u8,
24266-
true, false, PtrLenUnknown,
24267-
0, 0, 0, false, VECTOR_INDEX_NONE, nullptr, ira->codegen->intern.for_zero_byte());
24268-
ZigType *u8_slice = get_slice_type(ira->codegen, u8_ptr);
24269-
2427024264
ZigType *source_location_type = get_builtin_type(ira->codegen, "SourceLocation");
2427124265
if (type_resolve(ira->codegen, source_location_type, ResolveStatusSizeKnown)) {
2427224266
zig_unreachable();
@@ -24286,18 +24280,16 @@ static Stage1AirInst *ir_analyze_instruction_src(IrAnalyze *ira, Stage1ZirInstSr
2428624280
ZigType *import = instruction->base.source_node->owner;
2428724281
RootStruct *root_struct = import->data.structure.root_struct;
2428824282
Buf *path = root_struct->path;
24289-
ZigValue *file_name = create_const_str_lit(ira->codegen, path)->data.x_ptr.data.ref.pointee;
24290-
init_const_slice(ira->codegen, fields[0], file_name, 0, buf_len(path), true, nullptr);
24291-
fields[0]->type = u8_slice;
24283+
fields[0] = create_sentineled_str_lit(
24284+
ira->codegen, path,
24285+
ira->codegen->intern.for_zero_byte());
2429224286

2429324287
// fn_name: [:0]const u8
2429424288
ensure_field_index(source_location_type, "fn_name", 1);
2429524289
fields[1]->special = ConstValSpecialStatic;
24296-
24297-
ZigValue *fn_name = create_const_str_lit(ira->codegen, &fn_entry->symbol_name)->data.x_ptr.data.ref.pointee;
24298-
init_const_slice(ira->codegen, fields[1], fn_name, 0, buf_len(&fn_entry->symbol_name), true, nullptr);
24299-
fields[1]->type = u8_slice;
24300-
24290+
fields[1] = create_sentineled_str_lit(
24291+
ira->codegen, &fn_entry->symbol_name,
24292+
ira->codegen->intern.for_zero_byte());
2430124293

2430224294
TokenLoc tok_loc = root_struct->token_locs[instruction->base.source_node->main_token];
2430324295

test/behavior/bugs/3779.zig

+15
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,18 @@ test "@embedFile() returns a string literal" {
4040
try std.testing.expectEqualStrings(expected_contents, actual_contents);
4141
try std.testing.expectEqualStrings(expected_contents, ptr_actual_contents[0..actual_contents.len]);
4242
}
43+
44+
fn testFnForSrc() std.builtin.SourceLocation {
45+
return @src();
46+
}
47+
48+
test "@src() returns a struct containing 0-terminated string slices" {
49+
const src = testFnForSrc();
50+
try std.testing.expectEqual([:0]const u8, @TypeOf(src.file));
51+
try std.testing.expect(std.mem.endsWith(u8, src.file, "3779.zig"));
52+
try std.testing.expectEqual([:0]const u8, @TypeOf(src.fn_name));
53+
try std.testing.expect(std.mem.endsWith(u8, src.fn_name, "testFnForSrc"));
54+
55+
const ptr_src_file: [*:0]const u8 = src.file;
56+
const ptr_src_fn_name: [*:0]const u8 = src.fn_name;
57+
}

0 commit comments

Comments
 (0)