Skip to content

Commit 1b846bf

Browse files
committed
Slightly improve code samples in E0591
* Improve formatting * Don't hide `unsafe` block - it's important!
1 parent 72da5a9 commit 1b846bf

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

Diff for: compiler/rustc_error_codes/src/error_codes/E0591.md

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
Per [RFC 401][rfc401], if you have a function declaration `foo`:
22

33
```
4+
struct S;
5+
46
// For the purposes of this explanation, all of these
57
// different kinds of `fn` declarations are equivalent:
6-
struct S;
8+
79
fn foo(x: S) { /* ... */ }
810
# #[cfg(for_demonstration_only)]
9-
extern "C" { fn foo(x: S); }
11+
extern "C" {
12+
fn foo(x: S);
13+
}
1014
# #[cfg(for_demonstration_only)]
11-
impl S { fn foo(self) { /* ... */ } }
15+
impl S {
16+
fn foo(self) { /* ... */ }
17+
}
1218
```
1319

1420
the type of `foo` is **not** `fn(S)`, as one might expect.
@@ -40,10 +46,10 @@ extern "C" fn foo(userdata: Box<i32>) {
4046
4147
# fn callback(_: extern "C" fn(*mut i32)) {}
4248
# use std::mem::transmute;
43-
# unsafe {
44-
let f: extern "C" fn(*mut i32) = transmute(foo);
45-
callback(f);
46-
# }
49+
unsafe {
50+
let f: extern "C" fn(*mut i32) = transmute(foo);
51+
callback(f);
52+
}
4753
```
4854

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

Diff for: src/test/ui/explain.stdout

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
Per [RFC 401][rfc401], if you have a function declaration `foo`:
22

33
```
4+
struct S;
5+
46
// For the purposes of this explanation, all of these
57
// different kinds of `fn` declarations are equivalent:
6-
struct S;
8+
79
fn foo(x: S) { /* ... */ }
8-
extern "C" { fn foo(x: S); }
9-
impl S { fn foo(self) { /* ... */ } }
10+
extern "C" {
11+
fn foo(x: S);
12+
}
13+
impl S {
14+
fn foo(self) { /* ... */ }
15+
}
1016
```
1117

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

37-
let f: extern "C" fn(*mut i32) = transmute(foo);
38-
callback(f);
43+
unsafe {
44+
let f: extern "C" fn(*mut i32) = transmute(foo);
45+
callback(f);
46+
}
3947
```
4048

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

0 commit comments

Comments
 (0)