Rust-GCC / gccrs

GCC Front-End for Rust
https://rust-gcc.github.io/
GNU General Public License v2.0
2.34k stars 149 forks source link

ICE when using associated types #2015

Open CohenArthur opened 1 year ago

CohenArthur commented 1 year ago

I tried this code:

macro_rules! impl_foo {
    () => { impl Foo }
}

pub trait Foo {}

pub trait Bar {
    type Baz;
}

pub fn foo(_value: impl Bar<Baz = impl_foo!()>) -> i32 {
    15
}

pub fn bar(_value: impl Bar<Baz = impl Foo>) -> i32 {
    16
}

I expected to see this happen: no errors

Instead, this happened: internal compiler error when lowering the type:

crab1: internal compiler error: in translate, at rust/hir/rust-ast-lower-type.h:71
0x20fe2c8 Rust::HIR::ASTLoweringType::translate(Rust::AST::Type*)
    ../../gcc/rust/hir/rust-ast-lower-type.h:71
0x211211f Rust::HIR::ASTLoweringItem::visit(Rust::AST::Function&)
    /home/arthur/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/hir/rust-ast-lower-item.cc:428
0x1ed9240 Rust::AST::Function::accept_vis(Rust::AST::ASTVisitor&)
    /home/arthur/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/ast/rust-ast.cc:5395
0x210f362 Rust::HIR::ASTLoweringItem::translate(Rust::AST::Item*)
    /home/arthur/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/hir/rust-ast-lower-item.cc:37
0x20fa70e Rust::HIR::ASTLowering::go()
    /home/arthur/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/hir/rust-ast-lower.cc:79
0x20fa625 Rust::HIR::ASTLowering::Resolve(Rust::AST::Crate&)
    /home/arthur/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/hir/rust-ast-lower.cc:69
0x1fd77d4 Rust::Session::compile_crate(char const*)
    /home/arthur/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/rust-session-manager.cc:591
0x1fd6f87 Rust::Session::handle_input_files(int, char const**)
    /home/arthur/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/rust-session-manager.cc:375
0x1e78586 grs_langhook_parse_file()
    /home/arthur/Git/rust-gcc/gccrs/build/gcc/../../gcc/rust/rust-lang.cc:185
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.

9024d99f5b7

A different case with simpler associated types:

macro_rules! foo {
    () => {
        u32
    };
}

pub trait Bar {
    type Baz;
}

pub fn foo<T: Bar<Baz = foo!()>>(_value: T) -> i32 {
    15
}

pub fn bar<T: Bar<Baz = u32>>(_value: T) -> i32 {
    16
}

Same error, same hash

philberty commented 1 year ago

There is a stray macro invocation during HIR lowering:

test.rs:11:25: note: lowering type!!!: MacroInvocation: [regular] 
 outer attributes: none                                                               
 foo!(())                                                                             
 has semicolon: false                                                                 
   11 | pub fn foo<T: Bar<Baz = foo!()>>(_value: T) -> i32 {     
      |                         ^~~                                                   
crab1: internal compiler error: in translate, at rust/hir/rust-ast-lower-type.h:73
philberty commented 1 year ago

Its hitting:

(gdb) n
232       if (depth_exceeds_recursion_limit ())
(gdb) 
238       if (invoc.get_kind () == AST::MacroInvocation::InvocKind::Builtin)
(gdb) 
241       AST::MacroInvocData &invoc_data = invoc.get_invoc_data ();
(gdb) 
261       auto fragment = AST::Fragment::create_error ();
(gdb) 
262       invoc_data.set_expander (this);
(gdb) 
265       AST::MacroRulesDefinition *rules_def = nullptr;
(gdb) 
266       bool ok = mappings->lookup_macro_invocation (invoc, &rules_def);
(gdb) 
270       if (!ok)
(gdb) 
271         return;
philberty commented 1 year ago

I made a few tweaks:

diff --git a/gcc/rust/expand/rust-attribute-visitor.cc b/gcc/rust/expand/rust-attribute-visitor.cc
index 13ae28c0b5e..e9c3f5c6c38 100644
--- a/gcc/rust/expand/rust-attribute-visitor.cc
+++ b/gcc/rust/expand/rust-attribute-visitor.cc
@@ -166,19 +166,22 @@ AttrVisitor::expand_generic_args (AST::GenericArgs &args)
        }
     }

-  expander.pop_context ();
-
   // FIXME: Can we have macro invocations in generic type bindings?
   // expand binding args - strip sub-types only
   for (auto &binding : args.get_binding_args ())
     {
       auto &type = binding.get_type ();
+
+      rust_debug_loc (type->get_locus (), "HELLO");
       type->accept_vis (*this);
+      maybe_expand_type (type);

       if (type->is_marked_for_strip ())
        rust_error_at (type->get_locus (),
                       "cannot strip type in this position");
     }
+
+  expander.pop_context ();
philberty commented 1 year ago

@CohenArthur I know your making changes to the early name resolver at the moment I wonder if there is cross over here

philberty commented 1 year ago

I think the early name resolver is not being run on the type bindings so following gdb it just returns early.

CohenArthur commented 1 year ago

@philberty I checked and this issue is still present. Did you maybe refer to the wrong issue number in #2322?

philberty commented 1 year ago

Ah darn it yes i mark the wrong issue sorry about this