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

Can't call extern functions #421

Closed
bjorn3 opened this issue May 10, 2021 · 2 comments · Fixed by #598
Closed

Can't call extern functions #421

bjorn3 opened this issue May 10, 2021 · 2 comments · Fixed by #598

Comments

@bjorn3
Copy link

bjorn3 commented May 10, 2021

extern "C" {
    fn puts(s: *const i8);
}

fn main() {
    unsafe {
        puts("foo\0" as *const str as *const i8);
    }
}

as is this gives

<source>:6:5: fatal error: Failed to lower expr: [UnsafeBlockExpr:
  outer attributes: noneBlockExpr:

  outer attributes: none
  inner attributes: none
 statements: 
 ExprStmtWithoutBlock:
  CallExpr: 
   outer attributes: none
 Function expr: puts
 Call params:
  foo as *const str as *const i8
 final expression: none

]
    6 |     unsafe {
      |     ^
compilation terminated.

without unsafe block it gives

<source>:6:5: error: unknown root segment in path puts lookup puts
    6 |     puts("foo\0" as *const str as *const i8);
      |     ^
@philberty
Copy link
Member

I am going to bring this into the traits milestone as this goal should in theory finish off the type system. There are 3 features in this test case that are not supported yet:

  1. Extern blocks
  2. Unsafe blocks 'unsafe' ICE #382
  3. Type coercions

@philberty philberty added this to the Data Structures 3 - Traits milestone May 20, 2021
@philberty
Copy link
Member

philberty commented Jul 26, 2021

Found a crash when trying to dump the AST for this:

Program received signal SIGSEGV, Segmentation fault.                                                                                                                                                                                                   
Rust::AST::ExternalFunctionItem::as_string[abi:cxx11]() const (this=0x3d35f50) at ../../gccrs/gcc/rust/ast/rust-ast-full-test.cc:3598                                                                                                                  
3598      str += "\n (return) Type: " + return_type->as_string ();                                                                                                                                                                                     
(gdb) bt                                                                                                                   
#0  Rust::AST::ExternalFunctionItem::as_string[abi:cxx11]() const (this=0x3d35f50) at ../../gccrs/gcc/rust/ast/rust-ast-full-test.cc:3598
#1  0x0000000000e9e12a in Rust::AST::ExternBlock::as_string[abi:cxx11]() const (this=0x3cfc820) at ../../gccrs/gcc/rust/ast/rust-ast-full-test.cc:1316                                                                                                 
#2  0x0000000000e9875e in Rust::AST::Crate::as_string[abi:cxx11]() const (this=0x7fffffffd4e0) at ../../gccrs/gcc/rust/ast/rust-ast-full-test.cc:197                                                                                                   
#3  0x0000000000f2940e in Rust::Parser<Rust::Lexer>::debug_dump_ast_output (this=0x7fffffffd5d0, crate=..., out=...) at ../../gccrs/gcc/rust/parse/rust-parse-impl.h:14890                                                                             
#4  0x0000000000f0ea55 in Rust::Session::dump_ast (this=0x3b87820 <session>, parser=..., crate=...) at ../../gccrs/gcc/rust/rust-session-manager.cc:852                                                                                                
#5  0x0000000000f0d3fe in Rust::Session::parse_file (this=0x3b87820 <session>, filename=0x7fffffffe029 "test.rs") at ../../gccrs/gcc/rust/rust-session-manager.cc:494                                                                                  
#6  0x0000000000f0d1fb in Rust::Session::parse_files (this=0x3b87820 <session>, num_files=1, files=0x3d47a20) at ../../gccrs/gcc/rust/rust-session-manager.cc:458
#7  0x0000000000e68265 in grs_langhook_parse_file () at ../../gccrs/gcc/rust/rust-lang.cc:171
#8  0x00000000018bb23b in compile_file () at ../../gccrs/gcc/toplev.c:457
#9  0x00000000018be509 in do_compile () at ../../gccrs/gcc/toplev.c:2201
#10 0x00000000018be838 in toplev::main (this=0x7fffffffdad6, argc=18, argv=0x7fffffffdbe8) at ../../gccrs/gcc/toplev.c:2340
#11 0x0000000002ac7c7d in main (argc=18, argv=0x7fffffffdbe8) at ../../gccrs/gcc/main.c:39

philberty added a commit that referenced this issue Jul 26, 2021
Return types are optional in rust and extern fn items can be a ZST for the
result.

Addresses #421
Fixes #595
bors bot added a commit that referenced this issue Jul 26, 2021
596: Fix crash when extern function item has no return type r=philberty a=philberty

Addresses #421
Fixes #595


Co-authored-by: Philip Herron <[email protected]>
philberty added a commit that referenced this issue Jul 26, 2021
This is the initial patch to do the ground work to support extern blocks.
Type resolution and Generic output still needs to be done to actually
support extern blocks.

Addresses #421
bors bot added a commit that referenced this issue Jul 26, 2021
597: Add name-resolution and HIR lowering for extern blocks r=philberty a=philberty


Addresses #421 

Co-authored-by: Philip Herron <[email protected]>
philberty added a commit that referenced this issue Jul 26, 2021
…ons.

```
extern "C" {
    fn puts(s: *const i8);
}

fn main() {
    unsafe {
        let a = "Hello World\0";
        let b = a as *const str;
        let c = b as *const i8;
        puts(c);
    }
}
```

Fixes #421
@philberty philberty mentioned this issue Jul 26, 2021
philberty added a commit that referenced this issue Jul 27, 2021
This adds extern block compilation support. It currently assumes the C abi
and does not perform any name mangling. It does not support varadic
arguments yet but its the initial support to get this working.

Fixes #421
bors bot added a commit that referenced this issue Jul 27, 2021
598: Hello world r=philberty a=philberty

```rust
extern "C" {
    fn puts(s: *const i8);
}

fn main() {
    unsafe {
        let a = "Hello World\0";
        let b = a as *const str;
        let c = b as *const i8;
        puts(c);
    }
}
```

Fixes #421

Co-authored-by: Philip Herron <[email protected]>
@bors bors bot closed this as completed in #598 Jul 27, 2021
@bors bors bot closed this as completed in 2818017 Jul 27, 2021
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