Skip to content
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

Ice on using clone #3139

Closed
liamnaddell opened this issue Aug 23, 2024 · 4 comments · Fixed by #3175
Closed

Ice on using clone #3139

liamnaddell opened this issue Aug 23, 2024 · 4 comments · Fixed by #3175
Assignees
Labels

Comments

@liamnaddell
Copy link
Contributor

Code

#![feature(lang_items)]

#[lang = "clone"]
trait Clone {
    fn clone(&self) -> Self;
}

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

struct Abound {
    a: u32,
    b: u32,

}

#[derive(Clone)]
struct Be<T:Clone> {
    a: T,
    b: Abound,
}

impl Clone for u32 {
    fn clone(&self) -> Self {
        *self
    }
}

impl Clone for usize {
    fn clone(&self) -> Self {
        *self
    }
}

impl Clone for Abound {
    fn clone(&self) -> Self {
        return Abound { a: self.a.clone(), b: self.b.clone() };
    }
}

fn main() {
    let b: Be<usize> = Be {a:1,b:Abound { a:0,b:1 }};
    let _: Be<usize> = b.clone();
}

Meta

  • What version of Rust GCC were you using, git sha if possible.

Error output

<output>
Backtrace

derive_clone_generic.rs:17:3: error: generic item takes at least 1 type arguments but 0 were supplied [E0107]
 17 | #[derive(Clone)]
    |   ^~~~~~
 18 | struct Be<T:Clone> {
    |           ~
derive_clone_generic.rs:17:3: error: failed to resolve type arguments
derive_clone_generic.rs:17:3: note: failed to resolve lifetime
derive_clone_generic.rs:17:3: error: expected algebraic data type got: [<tyty::error>]
crab1: internal compiler error: in TyVar, at rust/typecheck/rust-tyty-util.cc:31
0x131f9ff Rust::TyTy::TyVar::TyVar(unsigned int)
  ../../gccrs/gcc/rust/typecheck/rust-tyty-util.cc:31
0x138500b Rust::Resolver::TypeCheckExpr::visit(Rust::HIR::BorrowExpr&)
  ../../gccrs/gcc/rust/typecheck/rust-hir-type-check-expr.cc:1400
0x11cdd87 Rust::HIR::BorrowExpr::accept_vis(Rust::HIR::HIRExpressionVisitor&)
  ../../gccrs/gcc/rust/hir/tree/rust-hir.cc:3809
0x137f7af Rust::Resolver::TypeCheckExpr::Resolve(Rust::HIR::Expr*)
  ../../gccrs/gcc/rust/typecheck/rust-hir-type-check-expr.cc:42
0x13205d3 Rust::TyTy::TypeCheckCallExpr::visit(Rust::TyTy::FnType&)
  ../../gccrs/gcc/rust/typecheck/rust-tyty-call.cc:143
0x1306db7 Rust::TyTy::FnType::accept_vis(Rust::TyTy::TyVisitor&)
  ../../gccrs/gcc/rust/typecheck/rust-tyty.cc:1879
0x1387dff Rust::TyTy::TypeCheckCallExpr::go(Rust::TyTy::BaseType*, Rust::HIR::CallExpr&, Rust::TyTy::VariantDef&, Rust::Resolver::TypeCheckContext*)
  ../../gccrs/gcc/rust/typecheck/rust-tyty-call.h:39
0x138037b Rust::Resolver::TypeCheckExpr::visit(Rust::HIR::CallExpr&)
  ../../gccrs/gcc/rust/typecheck/rust-hir-type-check-expr.cc:239
0x11d0867 Rust::HIR::CallExpr::accept_vis(Rust::HIR::HIRExpressionVisitor&)
  ../../gccrs/gcc/rust/hir/tree/rust-hir.cc:4865
0x137f7af Rust::Resolver::TypeCheckExpr::Resolve(Rust::HIR::Expr*)
  ../../gccrs/gcc/rust/typecheck/rust-hir-type-check-expr.cc:42
0x1377f07 Rust::Resolver::TypeCheckStructExpr::visit(Rust::HIR::StructExprFieldIdentifierValue&)
  ../../gccrs/gcc/rust/typecheck/rust-hir-type-check-struct.cc:287
0x1377487 Rust::Resolver::TypeCheckStructExpr::resolve(Rust::HIR::StructExprStructFields&)
  ../../gccrs/gcc/rust/typecheck/rust-hir-type-check-struct.cc:110
0x13770c7 Rust::Resolver::TypeCheckStructExpr::Resolve(Rust::HIR::StructExprStructFields*)
  ../../gccrs/gcc/rust/typecheck/rust-hir-type-check-struct.cc:38
0x1383b4f Rust::Resolver::TypeCheckExpr::visit(Rust::HIR::StructExprStructFields&)
  ../../gccrs/gcc/rust/typecheck/rust-hir-type-check-expr.cc:1042
0x11d001f Rust::HIR::StructExprStructFields::accept_vis(Rust::HIR::HIRExpressionVisitor&)
  ../../gccrs/gcc/rust/hir/tree/rust-hir.cc:4637
0x137f7af Rust::Resolver::TypeCheckExpr::Resolve(Rust::HIR::Expr*)
  ../../gccrs/gcc/rust/typecheck/rust-hir-type-check-expr.cc:42
0x1381ebb Rust::Resolver::TypeCheckExpr::visit(Rust::HIR::BlockExpr&)
  ../../gccrs/gcc/rust/typecheck/rust-hir-type-check-expr.cc:612
0x11d0e5f Rust::HIR::BlockExpr::accept_vis(Rust::HIR::HIRExpressionVisitor&)
  ../../gccrs/gcc/rust/hir/tree/rust-hir.cc:5027
0x137f7af Rust::Resolver::TypeCheckExpr::Resolve(Rust::HIR::Expr*)
  ../../gccrs/gcc/rust/typecheck/rust-hir-type-check-expr.cc:42
0x138d727 Rust::Resolver::TypeCheckImplItem::visit(Rust::HIR::Function&)
  ../../gccrs/gcc/rust/typecheck/rust-hir-type-check-implitem.cc:489
Please submit a full bug report, with preprocessed source (by using -freport-bug).
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.

@CohenArthur
Copy link
Member

ah, nice find - this is because we don't use the type's generics when generating code for derive(Clone). if you're interested in fixing it it should hopefully be pretty straightforward :D

@philberty
Copy link
Member

this works if you define the clone directly:

impl<T: Clone> Clone for Be<T> {
    fn clone(&self) -> Self {
        return Be::<T> {
            a: self.a.clone(),
            b: self.b.clone(),
        };
    }
}

I think this is meant to be the correct clone i need to look into the derive here

@philberty
Copy link
Member

philberty commented Sep 10, 2024

Yeah looks like we are just missing doing the generics on derive impl blocks

@philberty philberty self-assigned this Sep 10, 2024
@CohenArthur
Copy link
Member

@philberty this should be pretty easy to fix, the derive clone mechanism is a simple AST builder. rustc shows the following when outputting expanded/generated code:

#[automatically_derived]
impl <T> ::core::clone::Clone for Be<T> where T: ::core::clone::Clone
    {
    #[inline]
    fn clone<'_>(self: &'_ Self)
        ->
            Be<T> {
        Be{
            a: ::core::clone::Clone::clone(&self.a),

            b: ::core::clone::Clone::clone(&self.b),}
    }
}

so we should simply be able to add the bound

philberty added a commit that referenced this issue Sep 19, 2024
TODO

Fixes #3139

gcc/rust/ChangeLog:

	* Make-lang.in:
	* ast/rust-ast-builder.cc (Builder::generic_type_path_segment):
	(Builder::single_generic_type_path):
	(Builder::new_type):
	(Builder::new_lifetime_param):
	(Builder::new_type_param):
	* ast/rust-ast-builder.h:
	* ast/rust-ast.h:
	* ast/rust-type.h:
	* expand/rust-derive-clone.cc (DeriveClone::clone_impl):
	(DeriveClone::visit_tuple):
	(DeriveClone::visit_struct):
	(DeriveClone::visit_union):
	* expand/rust-derive-clone.h:
	* ast/rust-ast-builder-base.cc: New file.
	* ast/rust-ast-builder-base.h: New file.
	* ast/rust-ast-builder-type.cc: New file.
	* ast/rust-ast-builder-type.h: New file.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
philberty added a commit that referenced this issue Sep 20, 2024
When we generate derivations for Copy and Clone we need to make sure
the associated impl block sets up the generic parameters and arguments
correctly. This patch introduces the framework to copy chunks of the AST
because we need to make sure these new AST nodes have their own associated
id, calling clone on the nodes will just confuse name-resolution and
subsequent mappings.

Fixes #3139

gcc/rust/ChangeLog:

	* Make-lang.in: new objects
	* ast/rust-ast-builder.cc (Builder::generic_type_path_segment): new helper
	(Builder::single_generic_type_path): likewise
	(Builder::new_type): likewise
	(Builder::new_lifetime_param): likewise
	(Builder::new_type_param): likewise
	(Builder::new_lifetime): likewise
	(Builder::new_generic_args): likewise
	* ast/rust-ast-builder.h: new helper decls
	* ast/rust-ast.h: new const getters
	* ast/rust-path.h: likewise
	* ast/rust-type.h: likewise
	* expand/rust-derive-clone.cc (DeriveClone::clone_impl): take the types generics
	(DeriveClone::visit_tuple): likewise
	(DeriveClone::visit_struct): likewise
	(DeriveClone::visit_union): likewise
	* expand/rust-derive-clone.h: update header
	* expand/rust-derive-copy.cc (DeriveCopy::copy_impl): similarly take type generics
	(DeriveCopy::visit_struct): likewise
	(DeriveCopy::visit_tuple): likewise
	(DeriveCopy::visit_enum): likewise
	(DeriveCopy::visit_union): likewise
	* expand/rust-derive-copy.h: likewse
	* ast/rust-ast-builder-base.cc: New file.
	* ast/rust-ast-builder-base.h: New file.
	* ast/rust-ast-builder-type.cc: New file.
	* ast/rust-ast-builder-type.h: New file.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-3139-1.rs: New test.
	* rust/compile/issue-3139-2.rs: New test.
	* rust/compile/issue-3139-3.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
philberty added a commit that referenced this issue Sep 20, 2024
When we generate derivations for Copy and Clone we need to make sure
the associated impl block sets up the generic parameters and arguments
correctly. This patch introduces the framework to copy chunks of the AST
because we need to make sure these new AST nodes have their own associated
id, calling clone on the nodes will just confuse name-resolution and
subsequent mappings.

Fixes #3139

gcc/rust/ChangeLog:

	* Make-lang.in: new objects
	* ast/rust-ast-builder.cc (Builder::generic_type_path_segment): new helper
	(Builder::single_generic_type_path): likewise
	(Builder::new_type): likewise
	(Builder::new_lifetime_param): likewise
	(Builder::new_type_param): likewise
	(Builder::new_lifetime): likewise
	(Builder::new_generic_args): likewise
	* ast/rust-ast-builder.h: new helper decls
	* ast/rust-ast.h: new const getters
	* ast/rust-path.h: likewise
	* ast/rust-type.h: likewise
	* expand/rust-derive-clone.cc (DeriveClone::clone_impl): take the types generics
	(DeriveClone::visit_tuple): likewise
	(DeriveClone::visit_struct): likewise
	(DeriveClone::visit_union): likewise
	* expand/rust-derive-clone.h: update header
	* expand/rust-derive-copy.cc (DeriveCopy::copy_impl): similarly take type generics
	(DeriveCopy::visit_struct): likewise
	(DeriveCopy::visit_tuple): likewise
	(DeriveCopy::visit_enum): likewise
	(DeriveCopy::visit_union): likewise
	* expand/rust-derive-copy.h: likewse
	* ast/rust-ast-builder-base.cc: New file.
	* ast/rust-ast-builder-base.h: New file.
	* ast/rust-ast-builder-type.cc: New file.
	* ast/rust-ast-builder-type.h: New file.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-3139-1.rs: New test.
	* rust/compile/issue-3139-2.rs: New test.
	* rust/compile/issue-3139-3.rs: New test.
	* rust/compile/nr2/exclude: these all break nr2
philberty added a commit that referenced this issue Sep 20, 2024
When we generate derivations for Copy and Clone we need to make sure
the associated impl block sets up the generic parameters and arguments
correctly. This patch introduces the framework to copy chunks of the AST
because we need to make sure these new AST nodes have their own associated
id, calling clone on the nodes will just confuse name-resolution and
subsequent mappings.

Fixes #3139

gcc/rust/ChangeLog:

	* Make-lang.in: new objects
	* ast/rust-ast-builder.cc (Builder::generic_type_path_segment): new helper
	(Builder::single_generic_type_path): likewise
	(Builder::new_type): likewise
	(Builder::new_lifetime_param): likewise
	(Builder::new_type_param): likewise
	(Builder::new_lifetime): likewise
	(Builder::new_generic_args): likewise
	* ast/rust-ast-builder.h: new helper decls
	* ast/rust-ast.h: new const getters
	* ast/rust-path.h: likewise
	* ast/rust-type.h: likewise
	* expand/rust-derive-clone.cc (DeriveClone::clone_impl): take the types generics
	(DeriveClone::visit_tuple): likewise
	(DeriveClone::visit_struct): likewise
	(DeriveClone::visit_union): likewise
	* expand/rust-derive-clone.h: update header
	* expand/rust-derive-copy.cc (DeriveCopy::copy_impl): similarly take type generics
	(DeriveCopy::visit_struct): likewise
	(DeriveCopy::visit_tuple): likewise
	(DeriveCopy::visit_enum): likewise
	(DeriveCopy::visit_union): likewise
	* expand/rust-derive-copy.h: likewse
	* ast/rust-ast-builder-base.cc: New file.
	* ast/rust-ast-builder-base.h: New file.
	* ast/rust-ast-builder-type.cc: New file.
	* ast/rust-ast-builder-type.h: New file.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-3139-1.rs: New test.
	* rust/compile/issue-3139-2.rs: New test.
	* rust/compile/issue-3139-3.rs: New test.
	* rust/compile/nr2/exclude: these all break nr2
philberty added a commit that referenced this issue Sep 23, 2024
When we generate derivations for Copy and Clone we need to make sure
the associated impl block sets up the generic parameters and arguments
correctly. This patch introduces the framework to copy chunks of the AST
because we need to make sure these new AST nodes have their own associated
id, calling clone on the nodes will just confuse name-resolution and
subsequent mappings.

Fixes #3139

gcc/rust/ChangeLog:

	* Make-lang.in: new objects
	* ast/rust-ast-builder.cc (Builder::generic_type_path_segment): new helper
	(Builder::single_generic_type_path): likewise
	(Builder::new_type): likewise
	(Builder::new_lifetime_param): likewise
	(Builder::new_type_param): likewise
	(Builder::new_lifetime): likewise
	(Builder::new_generic_args): likewise
	* ast/rust-ast-builder.h: new helper decls
	* ast/rust-ast.h: new const getters
	* ast/rust-path.h: likewise
	* ast/rust-type.h: likewise
	* expand/rust-derive-clone.cc (DeriveClone::clone_impl): take the types generics
	(DeriveClone::visit_tuple): likewise
	(DeriveClone::visit_struct): likewise
	(DeriveClone::visit_union): likewise
	* expand/rust-derive-clone.h: update header
	* expand/rust-derive-copy.cc (DeriveCopy::copy_impl): similarly take type generics
	(DeriveCopy::visit_struct): likewise
	(DeriveCopy::visit_tuple): likewise
	(DeriveCopy::visit_enum): likewise
	(DeriveCopy::visit_union): likewise
	* expand/rust-derive-copy.h: likewse
	* ast/rust-ast-builder-type.cc: New file.
	* ast/rust-ast-builder-type.h: New file.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-3139-1.rs: New test.
	* rust/compile/issue-3139-2.rs: New test.
	* rust/compile/issue-3139-3.rs: New test.
	* rust/compile/nr2/exclude: these all break nr2
philberty added a commit that referenced this issue Sep 26, 2024
When we generate derivations for Copy and Clone we need to make sure
the associated impl block sets up the generic parameters and arguments
correctly. This patch introduces the framework to copy chunks of the AST
because we need to make sure these new AST nodes have their own associated
id, calling clone on the nodes will just confuse name-resolution and
subsequent mappings.

Fixes #3139

gcc/rust/ChangeLog:

	* Make-lang.in: new objects
	* ast/rust-ast-builder.cc (Builder::generic_type_path_segment): new helper
	(Builder::single_generic_type_path): likewise
	(Builder::new_type): likewise
	(Builder::new_lifetime_param): likewise
	(Builder::new_type_param): likewise
	(Builder::new_lifetime): likewise
	(Builder::new_generic_args): likewise
	* ast/rust-ast-builder.h: new helper decls
	* ast/rust-ast.h: new const getters
	* ast/rust-path.h: likewise
	* ast/rust-type.h: likewise
	* expand/rust-derive-clone.cc (DeriveClone::clone_impl): take the types generics
	(DeriveClone::visit_tuple): likewise
	(DeriveClone::visit_struct): likewise
	(DeriveClone::visit_union): likewise
	* expand/rust-derive-clone.h: update header
	* expand/rust-derive-copy.cc (DeriveCopy::copy_impl): similarly take type generics
	(DeriveCopy::visit_struct): likewise
	(DeriveCopy::visit_tuple): likewise
	(DeriveCopy::visit_enum): likewise
	(DeriveCopy::visit_union): likewise
	* expand/rust-derive-copy.h: likewse
	* ast/rust-ast-builder-type.cc: New file.
	* ast/rust-ast-builder-type.h: New file.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-3139-1.rs: New test.
	* rust/compile/issue-3139-2.rs: New test.
	* rust/compile/issue-3139-3.rs: New test.
	* rust/compile/nr2/exclude: these all break nr2
philberty added a commit that referenced this issue Sep 27, 2024
When we generate derivations for Copy and Clone we need to make sure
the associated impl block sets up the generic parameters and arguments
correctly. This patch introduces the framework to copy chunks of the AST
because we need to make sure these new AST nodes have their own associated
id, calling clone on the nodes will just confuse name-resolution and
subsequent mappings.

Fixes #3139

gcc/rust/ChangeLog:

	* Make-lang.in: new objects
	* ast/rust-ast-builder.cc (Builder::generic_type_path_segment): new helper
	(Builder::single_generic_type_path): likewise
	(Builder::new_type): likewise
	(Builder::new_lifetime_param): likewise
	(Builder::new_type_param): likewise
	(Builder::new_lifetime): likewise
	(Builder::new_generic_args): likewise
	* ast/rust-ast-builder.h: new helper decls
	* ast/rust-ast.h: new const getters
	* ast/rust-path.h: likewise
	* ast/rust-type.h: likewise
	* expand/rust-derive-clone.cc (DeriveClone::clone_impl): take the types generics
	(DeriveClone::visit_tuple): likewise
	(DeriveClone::visit_struct): likewise
	(DeriveClone::visit_union): likewise
	* expand/rust-derive-clone.h: update header
	* expand/rust-derive-copy.cc (DeriveCopy::copy_impl): similarly take type generics
	(DeriveCopy::visit_struct): likewise
	(DeriveCopy::visit_tuple): likewise
	(DeriveCopy::visit_enum): likewise
	(DeriveCopy::visit_union): likewise
	* expand/rust-derive-copy.h: likewse
	* ast/rust-ast-builder-type.cc: New file.
	* ast/rust-ast-builder-type.h: New file.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-3139-1.rs: New test.
	* rust/compile/issue-3139-2.rs: New test.
	* rust/compile/issue-3139-3.rs: New test.
	* rust/compile/nr2/exclude: these all break nr2
github-merge-queue bot pushed a commit that referenced this issue Sep 27, 2024
When we generate derivations for Copy and Clone we need to make sure
the associated impl block sets up the generic parameters and arguments
correctly. This patch introduces the framework to copy chunks of the AST
because we need to make sure these new AST nodes have their own associated
id, calling clone on the nodes will just confuse name-resolution and
subsequent mappings.

Fixes #3139

gcc/rust/ChangeLog:

	* Make-lang.in: new objects
	* ast/rust-ast-builder.cc (Builder::generic_type_path_segment): new helper
	(Builder::single_generic_type_path): likewise
	(Builder::new_type): likewise
	(Builder::new_lifetime_param): likewise
	(Builder::new_type_param): likewise
	(Builder::new_lifetime): likewise
	(Builder::new_generic_args): likewise
	* ast/rust-ast-builder.h: new helper decls
	* ast/rust-ast.h: new const getters
	* ast/rust-path.h: likewise
	* ast/rust-type.h: likewise
	* expand/rust-derive-clone.cc (DeriveClone::clone_impl): take the types generics
	(DeriveClone::visit_tuple): likewise
	(DeriveClone::visit_struct): likewise
	(DeriveClone::visit_union): likewise
	* expand/rust-derive-clone.h: update header
	* expand/rust-derive-copy.cc (DeriveCopy::copy_impl): similarly take type generics
	(DeriveCopy::visit_struct): likewise
	(DeriveCopy::visit_tuple): likewise
	(DeriveCopy::visit_enum): likewise
	(DeriveCopy::visit_union): likewise
	* expand/rust-derive-copy.h: likewse
	* ast/rust-ast-builder-type.cc: New file.
	* ast/rust-ast-builder-type.h: New file.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-3139-1.rs: New test.
	* rust/compile/issue-3139-2.rs: New test.
	* rust/compile/issue-3139-3.rs: New test.
	* rust/compile/nr2/exclude: these all break nr2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants