Skip to content

Commit 5a762b9

Browse files
committed
gccrs: Add error diag for self params on plain functions
self params dont have a type unless used within impl blocks. Rustc as far as I can tell in this senario generics a synthetic param of type Self in this senario so that it keeps consistent error diagnostic for number of parameters but the logic for what the parameter typpe should be seems unclear. Fixes #3592 gcc/rust/ChangeLog: * hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): add error diagnostic gcc/testsuite/ChangeLog: * rust/compile/issue-3592.rs: New test. Signed-off-by: Philip Herron <[email protected]>
1 parent 7e44c93 commit 5a762b9

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

gcc/rust/hir/rust-ast-lower-item.cc

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,39 @@ ASTLoweringItem::visit (AST::Function &function)
418418
std::vector<HIR::FunctionParam> function_params;
419419
function_params.reserve (function.get_function_params ().size ());
420420

421+
auto crate_num = mappings.get_current_crate ();
421422
for (auto &p : function.get_function_params ())
422423
{
423-
if (p->is_variadic () || p->is_self ())
424+
if (p->is_variadic ())
424425
continue;
426+
if (p->is_self ())
427+
{
428+
rich_location r (line_table, p->get_locus ());
429+
r.add_range (function.get_locus ());
430+
rust_error_at (
431+
r, "%<self%> parameter is only allowed in associated functions");
432+
433+
// rustc creates a synthetic regular fn-param here pointing to a
434+
// generic Self as far as i can see but that seems over the top for
435+
// now.
436+
//
437+
// see this example (invalid code):
438+
//
439+
// pub trait X {
440+
// fn x() {
441+
// fn f(&mut self) {}
442+
// f();
443+
// }
444+
// }
445+
//
446+
// without a synthetic param we wont get the number of args error as
447+
// well but i think this is fine for now.
448+
//
449+
// problem is what we make the param type to become...
450+
451+
continue;
452+
}
453+
425454
auto param = static_cast<AST::FunctionParam &> (*p);
426455

427456
auto translated_pattern = std::unique_ptr<HIR::Pattern> (
@@ -445,7 +474,6 @@ ASTLoweringItem::visit (AST::Function &function)
445474
ASTLoweringBlock::translate (*function.get_definition ().value (),
446475
&terminated));
447476

448-
auto crate_num = mappings.get_current_crate ();
449477
Analysis::NodeMapping mapping (crate_num, function.get_node_id (),
450478
mappings.get_next_hir_id (crate_num),
451479
mappings.get_next_localdef_id (crate_num));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub trait X {
2+
fn x() {
3+
fn f(&mut self) {}
4+
// { dg-error ".self. parameter is only allowed in associated functions" "" { target *-*-* } .-1 }
5+
f();
6+
}
7+
}

0 commit comments

Comments
 (0)