Skip to content

Fix fnptr not auto implement fn once #3492

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
50 changes: 50 additions & 0 deletions gcc/rust/typecheck/rust-tyty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2222,6 +2222,56 @@ FnPtr::clone () const
get_combined_refs ());
}

void
FnPtr::setup_fn_once_output () const
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is a dup of ClosureType::setup_fn_once_output its probably better to find a way to extract that out to be a shared helper

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, will do

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would adding this to CallableTypeInterface work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could work, ill refactor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i need to rebuild clangformat 10 on my ubuntu first...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh I just use clang format 19 -- it mostly works, aside from the once-in-6-months edge case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's refactoring some subparts of a source file and it's starting to be really a pain in the butt for me. I'm not sure if i should maintain some sort of repository to store different architecture of clang format 10...

Copy link
Collaborator

@powerboat9 powerboat9 Apr 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you used git add -p and git restore -p? May be easier than trying to get clang-format 10, depending on how far along you are

{
// lookup the lang items
auto fn_once_lookup = mappings.lookup_lang_item (LangItem::Kind::FN_ONCE);
auto fn_once_output_lookup
= mappings.lookup_lang_item (LangItem::Kind::FN_ONCE_OUTPUT);
if (!fn_once_lookup)
{
rust_fatal_error (UNKNOWN_LOCATION,
"Missing required %<fn_once%> lang item");
return;
}
if (!fn_once_output_lookup)
{
rust_fatal_error (UNKNOWN_LOCATION,
"Missing required %<fn_once_ouput%> lang item");
return;
}
Comment on lines +2229 to +2243
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be a mappings.get_lang_item(...) API which already emits the rust_fatal_error for you. we should probably rename it btw lol


DefId &trait_id = fn_once_lookup.value ();
DefId &trait_item_id = fn_once_output_lookup.value ();
Comment on lines +2245 to +2246
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would also allow you to avoid these lines, as get_lang_item already returns a DefId instead of an optional<Defid>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll probably fix the original code from Philip too


// resolve to the trait
HIR::Item *item = mappings.lookup_defid (trait_id).value ();
rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);
HIR::Trait *trait = static_cast<HIR::Trait *> (item);

Resolver::TraitReference *trait_ref
= Resolver::TraitResolver::Resolve (*trait);
rust_assert (!trait_ref->is_error ());

// resolve to trait item
HIR::TraitItem *trait_item
= mappings.lookup_trait_item_defid (trait_item_id).value ();
rust_assert (trait_item->get_item_kind ()
== HIR::TraitItem::TraitItemKind::TYPE);
std::string item_identifier = trait_item->trait_identifier ();

// setup associated types #[lang = "fn_once_output"]
Resolver::TraitItemReference *item_reference = nullptr;
bool found = trait_ref->lookup_trait_item_by_type (
item_identifier, Resolver::TraitItemReference::TraitItemType::TYPE,
&item_reference);
rust_assert (found);

// setup
item_reference->associated_type_set (&get_result_type ());
}

void
ClosureType::accept_vis (TyVisitor &vis)
{
Expand Down
11 changes: 9 additions & 2 deletions gcc/rust/typecheck/rust-tyty.h
Original file line number Diff line number Diff line change
Expand Up @@ -971,15 +971,19 @@ class FnPtr : public CallableTypeInterface
{Resolver::CanonicalPath::create_empty (), locus},
refs),
params (std::move (params)), result_type (result_type)
{}
{
setup_fn_once_output ();
}

FnPtr (HirId ref, HirId ty_ref, location_t locus, std::vector<TyVar> params,
TyVar result_type, std::set<HirId> refs = std::set<HirId> ())
: CallableTypeInterface (ref, ty_ref, TypeKind::FNPTR,
{Resolver::CanonicalPath::create_empty (), locus},
refs),
params (params), result_type (result_type)
{}
{
setup_fn_once_output ();
}

std::string get_name () const override final { return as_string (); }

Expand All @@ -1000,6 +1004,7 @@ class FnPtr : public CallableTypeInterface

const TyVar &get_var_return_type () const { return result_type; }

TyTy::BaseType &get_result_type () const { return *result_type.get_tyty (); }
size_t num_params () const { return params.size (); }

void accept_vis (TyVisitor &vis) override;
Expand All @@ -1016,6 +1021,8 @@ class FnPtr : public CallableTypeInterface
std::vector<TyVar> &get_params () { return params; }
const std::vector<TyVar> &get_params () const { return params; }

void setup_fn_once_output () const;

private:
std::vector<TyVar> params;
TyVar result_type;
Expand Down
8 changes: 8 additions & 0 deletions gcc/testsuite/rust/compile/format_args_basic_expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ macro_rules! format_args {
#[lang = "sized"]
trait Sized {}

#[lang = "fn_once"]
pub trait FnOnce<Args> {
#[lang = "fn_once_output"]
type Output;

extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
}

pub mod core {
pub mod fmt {
pub struct Formatter;
Expand Down
12 changes: 12 additions & 0 deletions gcc/testsuite/rust/compile/issue-2042.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
#[lang = "sized"]
pub trait Sized {}

#[lang = "fn_once"]
pub trait FnOnce<Args> {
#[lang = "fn_once_output"]
type Output;

extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
}


fn f<'r>(p: &'r mut fn(p: &mut ())) {
(*p)(())
// { dg-error "expected .&mut ()." "" { target *-*-* } .-1 }
Expand Down
10 changes: 10 additions & 0 deletions gcc/testsuite/rust/compile/privacy6.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@

#[lang = "fn_once"]
pub trait FnOnce<Args> {
#[lang = "fn_once_output"]
type Output;

extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
}


// { dg-additional-options "-w" }

#[lang = "sized"]
Expand Down
11 changes: 11 additions & 0 deletions gcc/testsuite/rust/compile/rust_abi.rs
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
#[lang = "sized"]
pub trait Sized {}

#[lang = "fn_once"]
pub trait FnOnce<Args> {
#[lang = "fn_once_output"]
type Output;

extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
}

pub fn f(_: extern "Rust" fn()) {}
11 changes: 11 additions & 0 deletions gcc/testsuite/rust/compile/torture/function_reference2.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#[lang = "sized"]
pub trait Sized {}

#[lang = "fn_once"]
pub trait FnOnce<Args> {
#[lang = "fn_once_output"]
type Output;

extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
}

fn test(a: i32) -> i32 {
a + 1
}
Expand Down
12 changes: 12 additions & 0 deletions gcc/testsuite/rust/compile/torture/function_reference3.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
#[lang = "sized"]
pub trait Sized {}

#[lang = "fn_once"]
pub trait FnOnce<Args> {
#[lang = "fn_once_output"]
type Output;

extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
}


struct Foo {
a: fn(i32) -> i32,
b: i32,
Expand Down
11 changes: 11 additions & 0 deletions gcc/testsuite/rust/compile/torture/function_reference4.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#[lang = "sized"]
pub trait Sized {}

#[lang = "fn_once"]
pub trait FnOnce<Args> {
#[lang = "fn_once_output"]
type Output;

extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
}

fn test(a: i32) -> i32 {
a + 1
}
Expand Down
11 changes: 11 additions & 0 deletions gcc/testsuite/rust/compile/try-catch-unwind-new.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
// { dg-options "-O2 -w -fdump-tree-optimized" }
#![feature(intrinsics)]

#[lang = "sized"]
pub trait Sized {}

#[lang = "fn_once"]
pub trait FnOnce<Args> {
#[lang = "fn_once_output"]
type Output;

extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
}

extern "rust-intrinsic" {
// { dg-final { scan-tree-dump-times "__builtin_eh_pointer" 1 "optimized" } }
fn catch_unwind(try_fn: fn(_: *mut u8), data: *mut u8, catch_fn: fn(_: *mut u8, _: *mut u8));
Expand Down
11 changes: 11 additions & 0 deletions gcc/testsuite/rust/compile/try-catch-unwind-old.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
// { dg-options "-O2 -w -fdump-tree-optimized" }
#![feature(intrinsics)]

#[lang = "sized"]
pub trait Sized {}

#[lang = "fn_once"]
pub trait FnOnce<Args> {
#[lang = "fn_once_output"]
type Output;

extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
}

extern "rust-intrinsic" {
// { dg-final { scan-tree-dump-times "__builtin_eh_pointer" 1 "optimized" } }
fn r#try(try_fn: fn(_: *mut u8), data: *mut u8, catch_fn: fn(_: *mut u8, _: *mut u8)) -> i32;
Expand Down