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

Cannot derive both Clone and Copy on an empty struct #2987

Closed
CohenArthur opened this issue May 14, 2024 · 4 comments · Fixed by #3091
Closed

Cannot derive both Clone and Copy on an empty struct #2987

CohenArthur opened this issue May 14, 2024 · 4 comments · Fixed by #3091

Comments

@CohenArthur
Copy link
Member

I tried this code:

#[lang = "copy"]
trait Copy {}

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

#[derive(Copy, Clone)]
struct Empty;

I expected to see this happen: no error.

Instead, this happened:

crab1: internal compiler error: in go, at rust/expand/rust-derive-clone.cc:106
0x23e9f13 Rust::AST::DeriveClone::go(Rust::AST::Item&)
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/expand/rust-derive-clone.cc:106
0x23e870c Rust::AST::DeriveVisitor::derive(Rust::AST::Item&, Rust::AST::Attribute const&, Rust::BuiltinMacro)
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/expand/rust-derive.cc:37
0x23daaed Rust::builtin_derive_item(Rust::AST::Item&, Rust::AST::Attribute const&, Rust::BuiltinMacro)
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/expand/rust-expand-visitor.cc:49
0x23da6cd Rust::ExpandVisitor::expand_inner_items(std::vector<std::unique_ptr<Rust::AST::Item, std::default_delete<Rust::AST::Item> >, std::allocator<std::unique_ptr<Rust::AST::Item, std::default_delete<Rust::AST::Item> > > >&)
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/expand/rust-expand-visitor.cc:192
0x23dbf80 Rust::ExpandVisitor::visit(Rust::AST::Crate&)
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/expand/rust-expand-visitor.cc:443
0x23d9ff0 Rust::ExpandVisitor::go(Rust::AST::Crate&)
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/expand/rust-expand-visitor.cc:42
0x22ec683 Rust::Session::expansion(Rust::AST::Crate&, Rust::Resolver2_0::NameResolutionContext&)
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/rust-session-manager.cc:933
0x22eaf0f Rust::Session::compile_crate(char const*)
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/rust-session-manager.cc:604
0x22ea8b1 Rust::Session::handle_input_files(int, char const**)
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/rust-session-manager.cc:416
0x2144872 grs_langhook_parse_file()
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/rust-lang.cc:165
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.

if the order of Clone and Copy is switched, then this happens:

crab1: internal compiler error: in go, at rust/expand/rust-derive-copy.cc:34
0x23eb6a3 Rust::AST::DeriveCopy::go(Rust::AST::Item&)
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/expand/rust-derive-copy.cc:34
0x23e873f Rust::AST::DeriveVisitor::derive(Rust::AST::Item&, Rust::AST::Attribute const&, Rust::BuiltinMacro)
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/expand/rust-derive.cc:39
0x23daaed Rust::builtin_derive_item(Rust::AST::Item&, Rust::AST::Attribute const&, Rust::BuiltinMacro)
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/expand/rust-expand-visitor.cc:49
0x23da6cd Rust::ExpandVisitor::expand_inner_items(std::vector<std::unique_ptr<Rust::AST::Item, std::default_delete<Rust::AST::Item> >, std::allocator<std::unique_ptr<Rust::AST::Item, std::default_delete<Rust::AST::Item> > > >&)
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/expand/rust-expand-visitor.cc:192
0x23dbf80 Rust::ExpandVisitor::visit(Rust::AST::Crate&)
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/expand/rust-expand-visitor.cc:443
0x23d9ff0 Rust::ExpandVisitor::go(Rust::AST::Crate&)
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/expand/rust-expand-visitor.cc:42
0x22ec683 Rust::Session::expansion(Rust::AST::Crate&, Rust::Resolver2_0::NameResolutionContext&)
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/rust-session-manager.cc:933
0x22eaf0f Rust::Session::compile_crate(char const*)
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/rust-session-manager.cc:604
0x22ea8b1 Rust::Session::handle_input_files(int, char const**)
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/rust-session-manager.cc:416
0x2144872 grs_langhook_parse_file()
	/home/kagounard/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/rust-lang.cc:165
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.

Meta

8a72566

@liamnaddell
Copy link
Contributor

The same bug occurs if you try to derive Copy twice. I believe the root cause is due to the first derive expanding into a TraitImpl. This moves the second derive to applying to the TraitImpl instead of to the struct. When we try to derive on a traitimpl, nothing happens, then we assert expansion success, which causes the ICE

@liamnaddell
Copy link
Contributor

The following code triggers an ICE, but @CohenArthur , it might be good to break this out into a separate issue?

#[lang = "copy"]
trait Copy {}

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

struct Empty;

#[derive(Copy)]
impl Clone for Empty {}

@liamnaddell
Copy link
Contributor

liamnaddell commented Jul 20, 2024

I'll see if I can fix the first issue, i.e. double-derive = ice problem

liamnaddell added a commit to liamnaddell/gccrs that referenced this issue Jul 20, 2024
liamnaddell added a commit to liamnaddell/gccrs that referenced this issue Jul 20, 2024
gcc/rust/ChangeLog:
	* expand/rust-expand-visitor.cc:
	Fix ICE caused by unique_ptr UB and buggy iterator use

gcc/testsuite/ChangeLog:
	* rust/compile/issue-2987.rs:
	Add test for deriving Clone and Copy at the same time

Signed-off-by: Liam Naddell <[email protected]>
liamnaddell added a commit to liamnaddell/gccrs that referenced this issue Jul 20, 2024
gcc/rust/ChangeLog:
	* expand/rust-expand-visitor.cc:
	Fix ICE caused by unique_ptr UB and buggy iterator use

gcc/testsuite/ChangeLog:
	* rust/compile/issue-2987.rs:
	Add test for deriving Clone and Copy at the same time

Signed-off-by: Liam Naddell <[email protected]>
liamnaddell added a commit to liamnaddell/gccrs that referenced this issue Jul 20, 2024
gcc/rust/ChangeLog:
	* expand/rust-expand-visitor.cc:
	Fix ICE caused by unique_ptr UB and buggy iterator use

gcc/testsuite/ChangeLog:
	* rust/compile/issue-2987.rs:
	Add test for deriving Clone and Copy at the same time

Signed-off-by: Liam Naddell <[email protected]>
github-merge-queue bot pushed a commit that referenced this issue Jul 24, 2024
gcc/rust/ChangeLog:
	* expand/rust-expand-visitor.cc:
	Fix ICE caused by unique_ptr UB and buggy iterator use

gcc/testsuite/ChangeLog:
	* rust/compile/issue-2987.rs:
	Add test for deriving Clone and Copy at the same time

Signed-off-by: Liam Naddell <[email protected]>
@liamnaddell
Copy link
Contributor

The following code triggers an ICE, but @CohenArthur , it might be good to break this out into a separate issue?

#[lang = "copy"]
trait Copy {}

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

struct Empty;

#[derive(Copy)]
impl Clone for Empty {}

This is a known issue that Guillaume Gomez has a PR for, I did a closer search

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants