Skip to content
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
6 changes: 4 additions & 2 deletions gcc/rust/backend/rust-compile-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ TyTyResolveCompile::visit (const TyTy::TupleType &type)
}

tree struct_type_record = Backend::struct_type (fields);
translated = Backend::named_type (type.as_string (), struct_type_record,
translated = Backend::named_type (type.get_name (), struct_type_record,
type.get_ident ().locus);
}

Expand Down Expand Up @@ -755,7 +755,9 @@ TyTyResolveCompile::visit (const TyTy::DynamicObjectType &type)
void
TyTyResolveCompile::visit (const TyTy::OpaqueType &type)
{
translated = error_mark_node;
rust_assert (type.can_resolve ());
auto underlying = type.resolve ();
translated = TyTyResolveCompile::compile (ctx, underlying, trait_object_mode);
}

tree
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/typecheck/rust-substitution-mapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ SubstMapperInternal::visit (TyTy::DynamicObjectType &type)
void
SubstMapperInternal::visit (TyTy::OpaqueType &type)
{
resolved = type.handle_substitions (mappings);
resolved = type.clone ();
}

// SubstMapperFromExisting
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/typecheck/rust-tyty-call.cc
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ TypeCheckCallExpr::visit (FnType &type)
}

type.monomorphize ();
resolved = type.get_return_type ()->clone ();
resolved = type.get_return_type ()->monomorphized_clone ();
}

void
Expand Down
74 changes: 9 additions & 65 deletions gcc/rust/typecheck/rust-tyty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -546,17 +546,14 @@ BaseType::destructure () const
{
x = p->get ();
}
// else if (auto p = x->try_as<const OpaqueType> ())
// {
// auto pr = p->resolve ();

// rust_debug ("XXXXXX")

// if (pr == x)
// return pr;
else if (auto p = x->try_as<const OpaqueType> ())
{
auto pr = p->resolve ();
if (pr == x)
return pr;

// x = pr;
// }
x = pr;
}
else
{
return x;
Expand Down Expand Up @@ -1992,7 +1989,7 @@ TupleType::get_name () const
std::string fields_buffer;
for (const TyVar &field : get_fields ())
{
fields_buffer += field.get_tyty ()->as_string ();
fields_buffer += field.get_tyty ()->get_name ();
bool has_next = (i + 1) < get_fields ().size ();
fields_buffer += has_next ? ", " : "";
i++;
Expand Down Expand Up @@ -3624,28 +3621,7 @@ BaseType *
OpaqueType::resolve () const
{
TyVar var (get_ty_ref ());
BaseType *r = var.get_tyty ();

while (r->get_kind () == TypeKind::OPAQUE)
{
OpaqueType *rr = static_cast<OpaqueType *> (r);
if (!rr->can_resolve ())
break;

TyVar v (rr->get_ty_ref ());
BaseType *n = v.get_tyty ();

// fix infinite loop
if (r == n)
break;

r = n;
}

if (r->get_kind () == TypeKind::OPAQUE && (r->get_ref () == r->get_ty_ref ()))
return TyVar (r->get_ty_ref ()).get_tyty ();

return r;
return var.get_tyty ();
}

bool
Expand All @@ -3655,41 +3631,9 @@ OpaqueType::is_equal (const BaseType &other) const
if (can_resolve () != other2.can_resolve ())
return false;

if (can_resolve ())
return resolve ()->can_eq (other2.resolve (), false);

return bounds_compatible (other, UNDEF_LOCATION, false);
}

OpaqueType *
OpaqueType::handle_substitions (SubstitutionArgumentMappings &subst_mappings)
{
// SubstitutionArg arg = SubstitutionArg::error ();
// bool ok = subst_mappings.get_argument_for_symbol (this, &arg);
// if (!ok || arg.is_error ())
// return this;

// OpaqueType *p = static_cast<OpaqueType *> (clone ());
// subst_mappings.on_param_subst (*p, arg);

// // there are two cases one where we substitute directly to a new PARAM and
// // otherwise
// if (arg.get_tyty ()->get_kind () == TyTy::TypeKind::PARAM)
// {
// p->set_ty_ref (arg.get_tyty ()->get_ref ());
// return p;
// }

// // this is the new subst that this needs to pass
// p->set_ref (mappings.get_next_hir_id ());
// p->set_ty_ref (arg.get_tyty ()->get_ref ());

// return p;

rust_unreachable ();
return nullptr;
}

// StrType

StrType::StrType (HirId ref, std::set<HirId> refs)
Expand Down
2 changes: 0 additions & 2 deletions gcc/rust/typecheck/rust-tyty.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,6 @@ class OpaqueType : public BaseType
std::string get_name () const override final;

bool is_equal (const BaseType &other) const override;

OpaqueType *handle_substitions (SubstitutionArgumentMappings &mappings);
};

class StructFieldType
Expand Down
90 changes: 41 additions & 49 deletions gcc/rust/typecheck/rust-unify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1788,59 +1788,51 @@ UnifyRules::expect_closure (TyTy::ClosureType *ltype, TyTy::BaseType *rtype)
TyTy::BaseType *
UnifyRules::expect_opaque (TyTy::OpaqueType *ltype, TyTy::BaseType *rtype)
{
switch (rtype->get_kind ())
if (rtype->is<TyTy::OpaqueType> ())
{
case TyTy::INFER: {
TyTy::InferType *r = static_cast<TyTy::InferType *> (rtype);
bool is_valid
= r->get_infer_kind () == TyTy::InferType::InferTypeKind::GENERAL;
if (is_valid)
return ltype->clone ();
}
break;

case TyTy::OPAQUE: {
auto &type = *static_cast<TyTy::OpaqueType *> (rtype);
if (ltype->num_specified_bounds () != type.num_specified_bounds ())
{
return new TyTy::ErrorType (0);
}
TyTy::OpaqueType *ro = rtype->as<TyTy::OpaqueType> ();
if (!ltype->is_equal (*ro))
return new TyTy::ErrorType (0);

if (!ltype->bounds_compatible (type, locus, true))
{
if (ltype->can_resolve () && ro->can_resolve ())
{
auto lr = ltype->resolve ();
auto rr = ro->resolve ();

auto res = UnifyRules::Resolve (TyTy::TyWithLocation (lr),
TyTy::TyWithLocation (rr), locus,
commit_flag, false /* emit_error */,
infer_flag, commits, infers);
if (res->get_kind () == TyTy::TypeKind::ERROR)
return new TyTy::ErrorType (0);
}

return ltype->clone ();
}
break;

case TyTy::CLOSURE:
case TyTy::SLICE:
case TyTy::PARAM:
case TyTy::POINTER:
case TyTy::STR:
case TyTy::ADT:
case TyTy::REF:
case TyTy::ARRAY:
case TyTy::FNDEF:
case TyTy::FNPTR:
case TyTy::TUPLE:
case TyTy::BOOL:
case TyTy::CHAR:
case TyTy::INT:
case TyTy::UINT:
case TyTy::FLOAT:
case TyTy::USIZE:
case TyTy::ISIZE:
case TyTy::NEVER:
case TyTy::PLACEHOLDER:
case TyTy::PROJECTION:
case TyTy::DYNAMIC:
case TyTy::ERROR:
return new TyTy::ErrorType (0);
}
else if (ltype->can_resolve ())
{
auto lr = ltype->resolve ();
ro->set_ty_ref (lr->get_ref ());
}
else if (ro->can_resolve ())
{
auto rr = ro->resolve ();
ltype->set_ty_ref (rr->get_ref ());
}
}
return new TyTy::ErrorType (0);
else if (ltype->can_resolve ())
{
auto underly = ltype->resolve ();
auto res = UnifyRules::Resolve (TyTy::TyWithLocation (underly),
TyTy::TyWithLocation (rtype), locus,
commit_flag, false /* emit_error */,
infer_flag, commits, infers);
if (res->get_kind () == TyTy::TypeKind::ERROR)
return new TyTy::ErrorType (0);
}
else
{
ltype->set_ty_ref (rtype->get_ref ());
}

return ltype;
}

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

trait Foo {
fn id(&self) -> i32;
}

struct A;
struct B;

impl Foo for A {
fn id(&self) -> i32 {
1
}
}

impl Foo for B {
fn id(&self) -> i32 {
2
}
}

fn make_foo(cond: bool) -> impl Foo {
if cond { A } else { B }
// { dg-error "mismatched types, expected .A. but got .B. .E0308." "" { target *-*-* } .-1 }
}
28 changes: 28 additions & 0 deletions gcc/testsuite/rust/execute/torture/impl_rpit1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#[lang = "sized"]
trait Sized {}

trait Foo {
fn id(&self) -> i32;
}

struct Thing(i32);

impl Foo for Thing {
fn id(&self) -> i32 {
self.0
}
}

fn make_thing(a: i32) -> impl Foo {
Thing(a)
}

fn use_foo(f: impl Foo) -> i32 {
f.id()
}

fn main() -> i32 {
let value = make_thing(42);
let val = use_foo(value);
val - 42
}
36 changes: 36 additions & 0 deletions gcc/testsuite/rust/execute/torture/impl_rpit2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#[lang = "sized"]
trait Sized {}

trait Foo {
fn id(&self) -> i32;
}

struct Thing(i32);

impl Thing {
fn double(&self) -> i32 {
// { dg-warning "associated function is never used: .double." "" { target *-*-* } .-1 }
self.0 * 2
}
}

impl Foo for Thing {
fn id(&self) -> i32 {
self.0
}
}

fn make_thing(a: i32) -> impl Foo {
Thing(a)
}

fn use_foo(f: impl Foo) -> i32 {
f.id()
}

fn main() -> i32 {
let value = make_thing(21);
let id = use_foo(value);

id - 21
}
25 changes: 25 additions & 0 deletions gcc/testsuite/rust/execute/torture/impl_rpit3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#[lang = "sized"]
trait Sized {}

trait Foo {
fn id(&self) -> i32;
}

struct Thing(i32);

impl Foo for Thing {
fn id(&self) -> i32 {
self.0
}
}

fn make_thing() -> impl Foo {
Thing(99)
}

fn main() -> i32 {
let v = make_thing();
let r = &v;
let val = r.id();
val - 99
}