Skip to content
Open
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
13 changes: 13 additions & 0 deletions src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,5 +621,18 @@ pub fn lowering_error_to_report(error: LoweringError) -> Report<'static, FileSpa
.with_help(format!("Implement the trait {:?} for the passed parameter type.", error.trait_name));
report.finish()
}
LoweringError::InvalidSelfArgument { span, path } => {
let path = path.display().to_string();
let filespan = FileSpan::new(path, span.from..span.to);
Report::build(ReportKind::Error, filespan.clone())
.with_code("InvalidSelfArgument")
.with_label(
Label::new(filespan)
.with_message("`self` parameter must be the first parameter in method")
.with_color(colors.next()),
)
.with_message("`self` parameter must be the first parameter in method")
.finish()
}
}
}
2 changes: 2 additions & 0 deletions src/ir/lowering/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ pub enum LoweringError {
},
#[error("missing variant")]
MissingVariant(Box<MissingVariantError>),
#[error("`self` parameter must be the first parameter in method")]
InvalidSelfArgument { span: Span, path: PathBuf },
}

#[derive(Debug, Clone)]
Expand Down
13 changes: 13 additions & 0 deletions src/ir/lowering/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
expressions::FnCallOp,
functions::{FunctionDecl, FunctionDef},
statements::{self, LetStmtTarget},
types::TypeDescriptor,
},
ir::{
BasicBlock, ConcreteIntrinsic, Function, Local, LocalKind, Operand, Place, Span,
Expand Down Expand Up @@ -167,6 +168,18 @@ pub(crate) fn lower_func(
});
}

// If this is a method then validate that `self` is the first parameter
if method_of.is_some() {
for param in func.decl.params.iter().skip(1) {
if let TypeDescriptor::SelfType { span, .. } = param.r#type {
return Err(LoweringError::InvalidSelfArgument {
span,
path: fn_builder.get_file_path().clone(),
});
}
}
}

fn_builder.ret_local = fn_builder.body.locals.len();
fn_builder.body.locals.push(Local::new(
None,
Expand Down
15 changes: 15 additions & 0 deletions tests/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,18 @@ fn missing_variant() {
error
);
}

#[test]
fn invalid_self_arg() {
let (source, name) = (
include_str!("invalid_programs/invalid_self_arg.con"),
"invalid_programs/invalid_self_arg.con",
);
let error = check_invalid_program(source, name);

assert!(
matches!(&error, LoweringError::InvalidSelfArgument { .. }),
"{:#?}",
error
);
}
7 changes: 7 additions & 0 deletions tests/invalid_programs/invalid_self_arg.con
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod Test {
struct A {}

impl A {
fn x(a: u32, &self) {}
}
}
Loading