Skip to content

Commit

Permalink
Slightly improve code samples in E0591
Browse files Browse the repository at this point in the history
* Improve formatting
* Don't hide `unsafe` block - it's important!
  • Loading branch information
camelid committed Nov 28, 2020
1 parent 72da5a9 commit 1b846bf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
20 changes: 13 additions & 7 deletions compiler/rustc_error_codes/src/error_codes/E0591.md
@@ -1,14 +1,20 @@
Per [RFC 401][rfc401], if you have a function declaration `foo`:

```
struct S;
// For the purposes of this explanation, all of these
// different kinds of `fn` declarations are equivalent:
struct S;
fn foo(x: S) { /* ... */ }
# #[cfg(for_demonstration_only)]
extern "C" { fn foo(x: S); }
extern "C" {
fn foo(x: S);
}
# #[cfg(for_demonstration_only)]
impl S { fn foo(self) { /* ... */ } }
impl S {
fn foo(self) { /* ... */ }
}
```

the type of `foo` is **not** `fn(S)`, as one might expect.
Expand Down Expand Up @@ -40,10 +46,10 @@ extern "C" fn foo(userdata: Box<i32>) {
# fn callback(_: extern "C" fn(*mut i32)) {}
# use std::mem::transmute;
# unsafe {
let f: extern "C" fn(*mut i32) = transmute(foo);
callback(f);
# }
unsafe {
let f: extern "C" fn(*mut i32) = transmute(foo);
callback(f);
}
```

Here, transmute is being used to convert the types of the fn arguments.
Expand Down
18 changes: 13 additions & 5 deletions src/test/ui/explain.stdout
@@ -1,12 +1,18 @@
Per [RFC 401][rfc401], if you have a function declaration `foo`:

```
struct S;

// For the purposes of this explanation, all of these
// different kinds of `fn` declarations are equivalent:
struct S;

fn foo(x: S) { /* ... */ }
extern "C" { fn foo(x: S); }
impl S { fn foo(self) { /* ... */ } }
extern "C" {
fn foo(x: S);
}
impl S {
fn foo(self) { /* ... */ }
}
```

the type of `foo` is **not** `fn(S)`, as one might expect.
Expand Down Expand Up @@ -34,8 +40,10 @@ extern "C" fn foo(userdata: Box<i32>) {
/* ... */
}

let f: extern "C" fn(*mut i32) = transmute(foo);
callback(f);
unsafe {
let f: extern "C" fn(*mut i32) = transmute(foo);
callback(f);
}
```

Here, transmute is being used to convert the types of the fn arguments.
Expand Down

0 comments on commit 1b846bf

Please sign in to comment.