better error messages on field access

This commit is contained in:
Techatrix 2023-01-28 19:01:49 +01:00
parent 9cde2495b2
commit 3c7e9e13ce
2 changed files with 22 additions and 19 deletions

View File

@ -441,7 +441,7 @@ pub fn interpret(
// Logic to find identifiers in accessible scopes // Logic to find identifiers in accessible scopes
if (interpreter.huntItDown(namespace, identifier, options)) |decl_index| { if (interpreter.huntItDown(namespace, identifier, options)) |decl_index| {
const decl = interpreter.ip.getDecl(decl_index); const decl = interpreter.ip.getDecl(decl_index);
if(decl.ty == .none) return InterpretResult{ .nothing = {} }; if (decl.ty == .none) return InterpretResult{ .nothing = {} };
return InterpretResult{ .value = Value{ return InterpretResult{ .value = Value{
.interpreter = interpreter, .interpreter = interpreter,
.node_idx = node_idx, .node_idx = node_idx,
@ -613,20 +613,23 @@ pub fn interpret(
else => false, else => false,
}; };
if (can_have_fields) { const accessed_ty = if (inner_lhs == .simple and inner_lhs.simple == .type) irv.val else irv.ty;
try interpreter.recordError( if (accessed_ty != .none) {
node_idx, if (can_have_fields) {
"undeclared_identifier", try interpreter.recordError(
"`{}` has no member '{s}'", node_idx,
.{ irv.ty.fmtType(interpreter.ip), field_name }, "undeclared_identifier",
); "`{}` has no member '{s}'",
} else { .{ accessed_ty.fmtType(interpreter.ip), field_name },
try interpreter.recordError( );
node_idx, } else {
"invalid_field_access", try interpreter.recordError(
"`{}` does not support field access", node_idx,
.{irv.ty.fmtType(interpreter.ip)}, "invalid_field_access",
); "`{}` does not support field access",
.{accessed_ty.fmtType(interpreter.ip)},
);
}
} }
return error.InvalidOperation; return error.InvalidOperation;
}, },
@ -866,7 +869,7 @@ pub fn interpret(
if (value.ty != type_type or value.ty == .none) return error.InvalidBuiltin; if (value.ty != type_type or value.ty == .none) return error.InvalidBuiltin;
if (interpreter.ip.indexToKey(field_name.ty) != .pointer_type) return error.InvalidBuiltin; // Check if it's a []const u8 if (interpreter.ip.indexToKey(field_name.ty) != .pointer_type) return error.InvalidBuiltin; // Check if it's a []const u8
if (value.val == .none) return error.InvalidBuiltin; if (value.val == .none) return error.InvalidBuiltin;
const value_namespace = interpreter.ip.indexToKey(value.val).getNamespace(interpreter.ip); const value_namespace = interpreter.ip.indexToKey(value.val).getNamespace(interpreter.ip);
if (value_namespace == .none) return error.InvalidBuiltin; if (value_namespace == .none) return error.InvalidBuiltin;
@ -949,7 +952,7 @@ pub fn interpret(
if (func.name_token == null) return InterpretResult{ .nothing = {} }; if (func.name_token == null) return InterpretResult{ .nothing = {} };
const name = offsets.tokenToSlice(tree, func.name_token.?); const name = offsets.tokenToSlice(tree, func.name_token.?);
// TODO: Resolve function type // TODO: Resolve function type
const type_type = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .type }); const type_type = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .type });

View File

@ -2677,9 +2677,9 @@ fn optionalPtrTy(
} }
} }
/// will panic in debug mode or in tests else will return `value` /// will panic in during testing else will return `value`
inline fn panicOrElse(message: []const u8, value: anytype) @TypeOf(value) { inline fn panicOrElse(message: []const u8, value: anytype) @TypeOf(value) {
if (builtin.is_test or builtin.mode == .Debug) { if (builtin.is_test) {
@panic(message); @panic(message);
} }
return value; return value;