Skip to content

Commit c3c349c

Browse files
committed
Consolidate setup_fn_once_output into superclass
gcc/rust/ChangeLog: * typecheck/rust-tyty.cc (CallableTypeInterface::setup_fn_once_output): Consolidate setup_fn_once_output into superclass. (FnPtr::setup_fn_once_output): likewise. (ClosureType::setup_fn_once_output): likewise. * typecheck/rust-tyty.h: likewise. gcc/testsuite/ChangeLog: * rust/compile/format_args_extra_comma.rs: add sized and fn_once * rust/compile/issue-3615.rs: likewise. * rust/compile/issue-3647.rs: likewise. * rust/compile/issue-3654.rs: likewise.
1 parent e34d17e commit c3c349c

File tree

6 files changed

+75
-104
lines changed

6 files changed

+75
-104
lines changed

gcc/rust/typecheck/rust-tyty.cc

Lines changed: 34 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,41 @@ BaseType::get_subst_argument_mappings () const
10301030

10311031
return empty;
10321032
}
1033+
// CallableTypeInterface
1034+
void
1035+
CallableTypeInterface::setup_fn_once_output () const
1036+
{
1037+
DefId trait_id
1038+
= mappings.get_lang_item (LangItem::Kind::FN_ONCE, UNKNOWN_LOCATION);
1039+
DefId trait_item_id
1040+
= mappings.get_lang_item (LangItem::Kind::FN_ONCE_OUTPUT, UNKNOWN_LOCATION);
1041+
1042+
// resolve to the trait
1043+
HIR::Item *item = mappings.lookup_defid (trait_id).value ();
1044+
rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);
1045+
HIR::Trait *trait = static_cast<HIR::Trait *> (item);
1046+
1047+
Resolver::TraitReference *trait_ref
1048+
= Resolver::TraitResolver::Resolve (*trait);
1049+
rust_assert (!trait_ref->is_error ());
1050+
1051+
// resolve to trait item
1052+
HIR::TraitItem *trait_item
1053+
= mappings.lookup_trait_item_defid (trait_item_id).value ();
1054+
rust_assert (trait_item->get_item_kind ()
1055+
== HIR::TraitItem::TraitItemKind::TYPE);
1056+
std::string item_identifier = trait_item->trait_identifier ();
1057+
1058+
// setup associated types #[lang = "fn_once_output"]
1059+
Resolver::TraitItemReference *item_reference = nullptr;
1060+
bool found = trait_ref->lookup_trait_item_by_type (
1061+
item_identifier, Resolver::TraitItemReference::TraitItemType::TYPE,
1062+
&item_reference);
1063+
rust_assert (found);
10331064

1065+
// setup
1066+
item_reference->associated_type_set (get_return_type ());
1067+
}
10341068
// InferType
10351069

10361070
InferType::InferType (HirId ref, InferTypeKind infer_kind, TypeHint hint,
@@ -2347,56 +2381,6 @@ FnPtr::clone () const
23472381
get_combined_refs ());
23482382
}
23492383

2350-
void
2351-
FnPtr::setup_fn_once_output () const
2352-
{
2353-
// lookup the lang items
2354-
auto fn_once_lookup = mappings.lookup_lang_item (LangItem::Kind::FN_ONCE);
2355-
auto fn_once_output_lookup
2356-
= mappings.lookup_lang_item (LangItem::Kind::FN_ONCE_OUTPUT);
2357-
if (!fn_once_lookup)
2358-
{
2359-
rust_fatal_error (UNKNOWN_LOCATION,
2360-
"Missing required %<fn_once%> lang item");
2361-
return;
2362-
}
2363-
if (!fn_once_output_lookup)
2364-
{
2365-
rust_fatal_error (UNKNOWN_LOCATION,
2366-
"Missing required %<fn_once_ouput%> lang item");
2367-
return;
2368-
}
2369-
2370-
DefId &trait_id = fn_once_lookup.value ();
2371-
DefId &trait_item_id = fn_once_output_lookup.value ();
2372-
2373-
// resolve to the trait
2374-
HIR::Item *item = mappings.lookup_defid (trait_id).value ();
2375-
rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);
2376-
HIR::Trait *trait = static_cast<HIR::Trait *> (item);
2377-
2378-
Resolver::TraitReference *trait_ref
2379-
= Resolver::TraitResolver::Resolve (*trait);
2380-
rust_assert (!trait_ref->is_error ());
2381-
2382-
// resolve to trait item
2383-
HIR::TraitItem *trait_item
2384-
= mappings.lookup_trait_item_defid (trait_item_id).value ();
2385-
rust_assert (trait_item->get_item_kind ()
2386-
== HIR::TraitItem::TraitItemKind::TYPE);
2387-
std::string item_identifier = trait_item->trait_identifier ();
2388-
2389-
// setup associated types #[lang = "fn_once_output"]
2390-
Resolver::TraitItemReference *item_reference = nullptr;
2391-
bool found = trait_ref->lookup_trait_item_by_type (
2392-
item_identifier, Resolver::TraitItemReference::TraitItemType::TYPE,
2393-
&item_reference);
2394-
rust_assert (found);
2395-
2396-
// setup
2397-
item_reference->associated_type_set (&get_result_type ());
2398-
}
2399-
24002384
void
24012385
ClosureType::accept_vis (TyVisitor &vis)
24022386
{
@@ -2455,56 +2439,6 @@ ClosureType::handle_substitions (SubstitutionArgumentMappings &mappings)
24552439
return nullptr;
24562440
}
24572441

2458-
void
2459-
ClosureType::setup_fn_once_output () const
2460-
{
2461-
// lookup the lang items
2462-
auto fn_once_lookup = mappings.lookup_lang_item (LangItem::Kind::FN_ONCE);
2463-
auto fn_once_output_lookup
2464-
= mappings.lookup_lang_item (LangItem::Kind::FN_ONCE_OUTPUT);
2465-
if (!fn_once_lookup)
2466-
{
2467-
rust_fatal_error (UNKNOWN_LOCATION,
2468-
"Missing required %<fn_once%> lang item");
2469-
return;
2470-
}
2471-
if (!fn_once_output_lookup)
2472-
{
2473-
rust_fatal_error (UNKNOWN_LOCATION,
2474-
"Missing required %<fn_once_ouput%> lang item");
2475-
return;
2476-
}
2477-
2478-
DefId &trait_id = fn_once_lookup.value ();
2479-
DefId &trait_item_id = fn_once_output_lookup.value ();
2480-
2481-
// resolve to the trait
2482-
HIR::Item *item = mappings.lookup_defid (trait_id).value ();
2483-
rust_assert (item->get_item_kind () == HIR::Item::ItemKind::Trait);
2484-
HIR::Trait *trait = static_cast<HIR::Trait *> (item);
2485-
2486-
Resolver::TraitReference *trait_ref
2487-
= Resolver::TraitResolver::Resolve (*trait);
2488-
rust_assert (!trait_ref->is_error ());
2489-
2490-
// resolve to trait item
2491-
HIR::TraitItem *trait_item
2492-
= mappings.lookup_trait_item_defid (trait_item_id).value ();
2493-
rust_assert (trait_item->get_item_kind ()
2494-
== HIR::TraitItem::TraitItemKind::TYPE);
2495-
std::string item_identifier = trait_item->trait_identifier ();
2496-
2497-
// setup associated types #[lang = "fn_once_output"]
2498-
Resolver::TraitItemReference *item_reference = nullptr;
2499-
bool found = trait_ref->lookup_trait_item_by_type (
2500-
item_identifier, Resolver::TraitItemReference::TraitItemType::TYPE,
2501-
&item_reference);
2502-
rust_assert (found);
2503-
2504-
// setup
2505-
item_reference->associated_type_set (&get_result_type ());
2506-
}
2507-
25082442
void
25092443
ArrayType::accept_vis (TyVisitor &vis)
25102444
{

gcc/rust/typecheck/rust-tyty.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ class CallableTypeInterface : public BaseType
271271
WARN_UNUSED_RESULT virtual BaseType *
272272
get_param_type_at (size_t index) const = 0;
273273
WARN_UNUSED_RESULT virtual BaseType *get_return_type () const = 0;
274+
void setup_fn_once_output() const;
274275
};
275276

276277
class InferType : public BaseType
@@ -1063,8 +1064,6 @@ class FnPtr : public CallableTypeInterface
10631064
std::vector<TyVar> &get_params () { return params; }
10641065
const std::vector<TyVar> &get_params () const { return params; }
10651066

1066-
void setup_fn_once_output () const;
1067-
10681067
private:
10691068
std::vector<TyVar> params;
10701069
TyVar result_type;
@@ -1147,8 +1146,6 @@ class ClosureType : public CallableTypeInterface, public SubstitutionRef
11471146

11481147
DefId get_def_id () const { return id; }
11491148

1150-
void setup_fn_once_output () const;
1151-
11521149
const std::set<NodeId> &get_captures () const { return captures; }
11531150

11541151
private:

gcc/testsuite/rust/compile/format_args_extra_comma.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ macro_rules! format_args {
88
#[lang = "sized"]
99
trait Sized {}
1010

11+
#[lang = "fn_once"]
12+
pub trait FnOnce<Args> {
13+
#[lang = "fn_once_output"]
14+
type Output;
15+
16+
extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
17+
}
18+
1119
pub mod core {
1220
pub mod fmt {
1321
pub struct Formatter;

gcc/testsuite/rust/compile/issue-3615.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
#[lang = "sized"]
2+
pub trait Sized {}
3+
4+
#[lang = "fn_once"]
5+
pub trait FnOnce<Args> {
6+
#[lang = "fn_once_output"]
7+
type Output;
8+
9+
extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
10+
}
11+
112
pub trait Trait {
213
pub fn nrvo(init: fn()) -> [u8; 4096] {
314
let mut buf = [0; 4096];

gcc/testsuite/rust/compile/issue-3647.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
#![allow(dead_code)]
2+
#[lang = "sized"]
3+
pub trait Sized {}
4+
5+
#[lang = "fn_once"]
6+
pub trait FnOnce<Args> {
7+
#[lang = "fn_once_output"]
8+
type Output;
9+
10+
extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
11+
}
212
type A = fn();
313

414
type B = for<'static> fn();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
#[lang = "sized"]
2+
pub trait Sized {}
3+
4+
#[lang = "fn_once"]
5+
pub trait FnOnce<Args> {
6+
#[lang = "fn_once_output"]
7+
type Output;
8+
9+
extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
10+
}
11+
112
type Meeshka = Mow<!>;
213
// { dg-error "generic arguments are not allowed for this type .E0109." "" { target *-*-* } .-1 }
314
type Mow = &'static fn(!) -> !;

0 commit comments

Comments
 (0)