Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error messages #15

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions crates/rue-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn main() {
let line = line + 1;
let col = col + 1;

eprintln!("{} at {line}:{col}", error.kind());
eprintln!("Error: {} ({line}:{col})", error.kind());
}

if args.analyze {
Expand Down Expand Up @@ -71,10 +71,10 @@ fn print_diagnostics(source: &str, diagnostics: &[Diagnostic]) -> bool {
match error.kind() {
DiagnosticKind::Error(kind) => {
has_error = true;
eprintln!("error: {kind} at {line}:{col}");
eprintln!("Error: {kind} ({line}:{col})");
}
DiagnosticKind::Warning(kind) => {
eprintln!("warning: {kind} at {line}:{col}");
eprintln!("Warning: {kind} ({line}:{col})");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rue-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ clvmr = "0.6.1"
id-arena = "2.2.1"
indexmap = "2.2.6"
rowan = "0.15.15"
thiserror = "1.0.58"
num-traits = "0.2.18"
num-iter = "0.1.44"
num-bigint = "0.4.4"
log = "0.4.21"
hex = "0.4.3"
indoc = "2.0.5"
22 changes: 8 additions & 14 deletions crates/rue-compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ impl<'a> Compiler<'a> {
}
}

fn detect_cycle(&mut self, type_id: TypeId, text_range: TextRange) -> bool {
if self.db.is_cyclic(type_id) {
self.db.error(ErrorKind::RecursiveTypeAlias, text_range);
true
} else {
false
fn symbol_name(&self, symbol_id: SymbolId) -> Option<String> {
for &scope_id in self.scope_stack.iter().rev() {
if let Some(name) = self.db.scope(scope_id).symbol_name(symbol_id) {
return Some(name.to_string());
}
}
None
}

fn type_name(&self, ty: TypeId) -> String {
Expand Down Expand Up @@ -236,10 +236,7 @@ impl<'a> Compiler<'a> {

if !comparison.is_assignable() {
self.db.error(
ErrorKind::TypeMismatch {
expected: self.type_name(to),
found: self.type_name(from),
},
ErrorKind::TypeMismatch(self.type_name(from), self.type_name(to)),
range,
);
}
Expand All @@ -255,10 +252,7 @@ impl<'a> Compiler<'a> {

if !comparison.is_castable() {
self.db.error(
ErrorKind::CastMismatch {
expected: self.type_name(to),
found: self.type_name(from),
},
ErrorKind::CastMismatch(self.type_name(from), self.type_name(to)),
range,
);
}
Expand Down
26 changes: 10 additions & 16 deletions crates/rue-compiler/src/compiler/expr/field_access_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ impl Compiler<'_> {
.extend_guard_path(old_value, GuardPathItem::Field(field_name.to_string()))
} else {
self.db.error(
ErrorKind::UndefinedField {
field: field_name.to_string(),
ty: self.type_name(old_value.type_id),
},
ErrorKind::UnknownField(field_name.to_string()),
field_name.text_range(),
);
return self.unknown();
Expand All @@ -74,10 +71,7 @@ impl Compiler<'_> {
.extend_guard_path(old_value, GuardPathItem::Field(field_name.to_string()))
} else {
self.db.error(
ErrorKind::UndefinedField {
field: field_name.to_string(),
ty: self.type_name(old_value.type_id),
},
ErrorKind::UnknownField(field_name.to_string()),
field_name.text_range(),
);
return self.unknown();
Expand All @@ -94,10 +88,10 @@ impl Compiler<'_> {
}
_ => {
self.db.error(
ErrorKind::InvalidFieldAccess {
field: field_name.to_string(),
ty: self.type_name(old_value.type_id),
},
ErrorKind::InvalidFieldAccess(
field_name.to_string(),
self.type_name(old_value.type_id),
),
field_name.text_range(),
);
return self.unknown();
Expand All @@ -122,10 +116,10 @@ impl Compiler<'_> {
}
_ => {
self.db.error(
ErrorKind::InvalidFieldAccess {
field: field_name.to_string(),
ty: self.type_name(old_value.type_id),
},
ErrorKind::InvalidFieldAccess(
field_name.to_string(),
self.type_name(old_value.type_id),
),
field_name.text_range(),
);
return self.unknown();
Expand Down
31 changes: 12 additions & 19 deletions crates/rue-compiler/src/compiler/expr/function_call_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ impl Compiler<'_> {
Rest::Nil => {
if args.len() != function.param_types.len() {
self.db.error(
ErrorKind::ArgumentMismatch {
expected: function.param_types.len(),
found: args.len(),
},
ErrorKind::ArgumentMismatch(args.len(), function.param_types.len()),
ast.syntax().text_range(),
);
}
Expand All @@ -132,10 +129,7 @@ impl Compiler<'_> {
&& args.len() != function.param_types.len() - 1
{
self.db.error(
ErrorKind::ArgumentMismatchOptional {
expected: function.param_types.len(),
found: args.len(),
},
ErrorKind::ArgumentMismatchOptional(args.len(), function.param_types.len()),
ast.syntax().text_range(),
);
}
Expand All @@ -148,19 +142,16 @@ impl Compiler<'_> {
{
if args.len() < function.param_types.len() - 1 {
self.db.error(
ErrorKind::ArgumentMismatchSpread {
expected: function.param_types.len(),
found: args.len(),
},
ErrorKind::ArgumentMismatchSpread(
args.len(),
function.param_types.len(),
),
ast.syntax().text_range(),
);
}
} else if args.len() != function.param_types.len() {
self.db.error(
ErrorKind::ArgumentMismatch {
expected: function.param_types.len(),
found: args.len(),
},
ErrorKind::ArgumentMismatch(args.len(), function.param_types.len()),
ast.syntax().text_range(),
);
}
Expand All @@ -175,7 +166,7 @@ impl Compiler<'_> {
if last && spread {
if function.rest != Rest::Spread {
self.db.error(
ErrorKind::DisallowedSpread,
ErrorKind::UnsupportedFunctionSpread,
ast_args[i].syntax().text_range(),
);
} else if i >= function.param_types.len() - 1 {
Expand All @@ -188,8 +179,10 @@ impl Compiler<'_> {
{
self.type_check(arg, inner_list_type, ast_args[i].syntax().text_range());
} else if i == function.param_types.len() - 1 && !spread {
self.db
.error(ErrorKind::RequiredSpread, ast_args[i].syntax().text_range());
self.db.error(
ErrorKind::RequiredFunctionSpread,
ast_args[i].syntax().text_range(),
);
}
} else if i < function.param_types.len() {
let param_type = function.param_types[i];
Expand Down
5 changes: 1 addition & 4 deletions crates/rue-compiler/src/compiler/expr/guard_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@ impl Compiler<'_> {
}
_ => {
self.db.error(
ErrorKind::UnsupportedTypeGuard {
from: self.type_name(from),
to: self.type_name(to),
},
ErrorKind::UnsupportedTypeGuard(self.type_name(from), self.type_name(to)),
text_range,
);
None
Expand Down
2 changes: 1 addition & 1 deletion crates/rue-compiler/src/compiler/expr/index_access_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Compiler<'_> {

let Type::List(item_type) = self.db.ty(value.type_id).clone() else {
self.db.error(
ErrorKind::IndexAccess(self.type_name(value.type_id)),
ErrorKind::InvalidIndexAccess(self.type_name(value.type_id)),
index_access.expr().unwrap().syntax().text_range(),
);
return self.unknown();
Expand Down
17 changes: 4 additions & 13 deletions crates/rue-compiler/src/compiler/expr/initializer_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ impl Compiler<'_> {
match ty.map(|ty| self.db.ty(ty)).cloned() {
Some(Type::Struct(struct_type)) => {
let hir_id = self.compile_initializer_fields(
ty.unwrap(),
&struct_type.fields,
struct_type.rest,
initializer.fields(),
Expand All @@ -35,7 +34,6 @@ impl Compiler<'_> {
Some(Type::EnumVariant(enum_variant)) => {
if let Some(fields) = enum_variant.fields {
let fields_hir_id = self.compile_initializer_fields(
ty.unwrap(),
&fields,
enum_variant.rest,
initializer.fields(),
Expand All @@ -52,7 +50,7 @@ impl Compiler<'_> {
}
} else {
self.db.error(
ErrorKind::EnumVariantWithoutFields,
ErrorKind::InvalidEnumVariantInitializer(self.type_name(ty.unwrap())),
initializer.path().unwrap().syntax().text_range(),
);
self.unknown()
Expand All @@ -71,7 +69,6 @@ impl Compiler<'_> {

fn compile_initializer_fields(
&mut self,
struct_type: TypeId,
struct_fields: &IndexMap<String, TypeId>,
rest: Rest,
initializer_fields: Vec<InitializerField>,
Expand Down Expand Up @@ -103,15 +100,12 @@ impl Compiler<'_> {
// Insert the field if it exists and hasn't already been assigned.
if specified_fields.contains_key(name.text()) {
self.db.error(
ErrorKind::DuplicateField(name.to_string()),
ErrorKind::DuplicateInitializerField(name.to_string()),
name.text_range(),
);
} else if !struct_fields.contains_key(name.text()) {
self.db.error(
ErrorKind::UndefinedField {
field: name.to_string(),
ty: self.type_name(struct_type),
},
ErrorKind::UnknownInitializerField(name.to_string()),
name.text_range(),
);
} else {
Expand All @@ -134,10 +128,7 @@ impl Compiler<'_> {

if !missing_fields.is_empty() {
self.db.error(
ErrorKind::MissingFields {
fields: missing_fields,
ty: self.type_name(struct_type),
},
ErrorKind::MissingInitializerFields(missing_fields),
text_range,
);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rue-compiler/src/compiler/expr/pair_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Compiler<'_> {
// Compile the rest expression, if present.
// It's a parser error if not, so it's fine to return unknown.
let rest = pair
.first()
.rest()
.map(|expr| {
let value = self.compile_expr(&expr, rest);
self.type_check(
Expand Down
16 changes: 12 additions & 4 deletions crates/rue-compiler/src/compiler/expr/path_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ impl Compiler<'_> {
return self.unknown();
};

let mut last_ident = idents[0].to_string();

for (i, name) in idents.iter().enumerate().skip(1) {
let Some(next_item) =
self.resolve_next_path(item, name, PathKind::Symbol, i == idents.len() - 1)
else {
return self.unknown();
};
last_ident = name.to_string();
item = next_item;
}

Expand All @@ -34,23 +37,28 @@ impl Compiler<'_> {
PathItem::Type(type_id) => {
if let Type::EnumVariant(variant_type) = self.db.ty(type_id).clone() {
if variant_type.fields.is_some() {
self.db.error(ErrorKind::EnumVariantWithFields, text_range);
self.db.error(
ErrorKind::InvalidEnumVariantReference(self.type_name(type_id)),
text_range,
);
}
return Value::new(variant_type.discriminant, type_id);
}
self.db.error(ErrorKind::ExpectedSymbolPath, text_range);
self.db
.error(ErrorKind::ExpectedSymbolPath(last_ident), text_range);
return self.unknown();
}
};

if matches!(self.db.symbol(symbol_id), Symbol::Module(..)) {
self.db.error(ErrorKind::ModuleReference, text_range);
self.db
.error(ErrorKind::ModuleReference(last_ident), text_range);
return self.unknown();
}

if !self.is_callee && matches!(self.db.symbol(symbol_id), Symbol::InlineFunction(..)) {
self.db
.error(ErrorKind::InlineFunctionReference, text_range);
.error(ErrorKind::InlineFunctionReference(last_ident), text_range);
return self.unknown();
}

Expand Down
Loading