Skip to content

Commit 7c3f65b

Browse files
middle::intrinsicck -> rustc_passes
1 parent 82bfd8e commit 7c3f65b

File tree

8 files changed

+118
-114
lines changed

8 files changed

+118
-114
lines changed

Cargo.lock

+2
Original file line numberDiff line numberDiff line change
@@ -3613,6 +3613,8 @@ dependencies = [
36133613
"rustc",
36143614
"rustc_data_structures",
36153615
"rustc_errors",
3616+
"rustc_index",
3617+
"rustc_target",
36163618
"syntax",
36173619
"syntax_pos",
36183620
]

src/librustc/error_codes.rs

-105
Original file line numberDiff line numberDiff line change
@@ -1566,33 +1566,6 @@ It is not possible to use stability attributes outside of the standard library.
15661566
Also, for now, it is not possible to write deprecation messages either.
15671567
"##,
15681568

1569-
E0512: r##"
1570-
Transmute with two differently sized types was attempted. Erroneous code
1571-
example:
1572-
1573-
```compile_fail,E0512
1574-
fn takes_u8(_: u8) {}
1575-
1576-
fn main() {
1577-
unsafe { takes_u8(::std::mem::transmute(0u16)); }
1578-
// error: cannot transmute between types of different sizes,
1579-
// or dependently-sized types
1580-
}
1581-
```
1582-
1583-
Please use types with same size or use the expected type directly. Example:
1584-
1585-
```
1586-
fn takes_u8(_: u8) {}
1587-
1588-
fn main() {
1589-
unsafe { takes_u8(::std::mem::transmute(0i8)); } // ok!
1590-
// or:
1591-
unsafe { takes_u8(0u8); } // ok!
1592-
}
1593-
```
1594-
"##,
1595-
15961569
E0517: r##"
15971570
This error indicates that a `#[repr(..)]` attribute was placed on an
15981571
unsupported item.
@@ -1787,84 +1760,6 @@ See [RFC 1522] for more details.
17871760
[RFC 1522]: https://github.com/rust-lang/rfcs/blob/master/text/1522-conservative-impl-trait.md
17881761
"##,
17891762

1790-
E0591: r##"
1791-
Per [RFC 401][rfc401], if you have a function declaration `foo`:
1792-
1793-
```
1794-
// For the purposes of this explanation, all of these
1795-
// different kinds of `fn` declarations are equivalent:
1796-
struct S;
1797-
fn foo(x: S) { /* ... */ }
1798-
# #[cfg(for_demonstration_only)]
1799-
extern "C" { fn foo(x: S); }
1800-
# #[cfg(for_demonstration_only)]
1801-
impl S { fn foo(self) { /* ... */ } }
1802-
```
1803-
1804-
the type of `foo` is **not** `fn(S)`, as one might expect.
1805-
Rather, it is a unique, zero-sized marker type written here as `typeof(foo)`.
1806-
However, `typeof(foo)` can be _coerced_ to a function pointer `fn(S)`,
1807-
so you rarely notice this:
1808-
1809-
```
1810-
# struct S;
1811-
# fn foo(_: S) {}
1812-
let x: fn(S) = foo; // OK, coerces
1813-
```
1814-
1815-
The reason that this matter is that the type `fn(S)` is not specific to
1816-
any particular function: it's a function _pointer_. So calling `x()` results
1817-
in a virtual call, whereas `foo()` is statically dispatched, because the type
1818-
of `foo` tells us precisely what function is being called.
1819-
1820-
As noted above, coercions mean that most code doesn't have to be
1821-
concerned with this distinction. However, you can tell the difference
1822-
when using **transmute** to convert a fn item into a fn pointer.
1823-
1824-
This is sometimes done as part of an FFI:
1825-
1826-
```compile_fail,E0591
1827-
extern "C" fn foo(userdata: Box<i32>) {
1828-
/* ... */
1829-
}
1830-
1831-
# fn callback(_: extern "C" fn(*mut i32)) {}
1832-
# use std::mem::transmute;
1833-
# unsafe {
1834-
let f: extern "C" fn(*mut i32) = transmute(foo);
1835-
callback(f);
1836-
# }
1837-
```
1838-
1839-
Here, transmute is being used to convert the types of the fn arguments.
1840-
This pattern is incorrect because, because the type of `foo` is a function
1841-
**item** (`typeof(foo)`), which is zero-sized, and the target type (`fn()`)
1842-
is a function pointer, which is not zero-sized.
1843-
This pattern should be rewritten. There are a few possible ways to do this:
1844-
1845-
- change the original fn declaration to match the expected signature,
1846-
and do the cast in the fn body (the preferred option)
1847-
- cast the fn item fo a fn pointer before calling transmute, as shown here:
1848-
1849-
```
1850-
# extern "C" fn foo(_: Box<i32>) {}
1851-
# use std::mem::transmute;
1852-
# unsafe {
1853-
let f: extern "C" fn(*mut i32) = transmute(foo as extern "C" fn(_));
1854-
let f: extern "C" fn(*mut i32) = transmute(foo as usize); // works too
1855-
# }
1856-
```
1857-
1858-
The same applies to transmutes to `*mut fn()`, which were observed in practice.
1859-
Note though that use of this type is generally incorrect.
1860-
The intention is typically to describe a function pointer, but just `fn()`
1861-
alone suffices for that. `*mut fn()` is a pointer to a fn pointer.
1862-
(Since these values are typically just passed to C code, however, this rarely
1863-
makes a difference in practice.)
1864-
1865-
[rfc401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
1866-
"##,
1867-
18681763
E0593: r##"
18691764
You tried to supply an `Fn`-based type with an incorrect number of arguments
18701765
than what was expected.

src/librustc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ pub mod middle {
106106
pub mod diagnostic_items;
107107
pub mod exported_symbols;
108108
pub mod free_region;
109-
pub mod intrinsicck;
110109
pub mod lib_features;
111110
pub mod lang_items;
112111
pub mod mem_categorization;

src/librustc_interface/passes.rs

-1
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,6 @@ pub fn default_provide(providers: &mut ty::query::Providers<'_>) {
780780
ty::provide(providers);
781781
traits::provide(providers);
782782
stability::provide(providers);
783-
middle::intrinsicck::provide(providers);
784783
reachable::provide(providers);
785784
rustc_passes::provide(providers);
786785
rustc_traits::provide(providers);

src/librustc_passes/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ rustc_data_structures = { path = "../librustc_data_structures" }
1515
syntax = { path = "../libsyntax" }
1616
syntax_pos = { path = "../libsyntax_pos" }
1717
errors = { path = "../librustc_errors", package = "rustc_errors" }
18+
rustc_target = { path = "../librustc_target" }
19+
rustc_index = { path = "../librustc_index" }

src/librustc_passes/error_codes.rs

+105
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,111 @@ If you don't know the basics of Rust, you can go look to the Rust Book to get
396396
started: https://doc.rust-lang.org/book/
397397
"##,
398398

399+
E0591: r##"
400+
Per [RFC 401][rfc401], if you have a function declaration `foo`:
401+
402+
```
403+
// For the purposes of this explanation, all of these
404+
// different kinds of `fn` declarations are equivalent:
405+
struct S;
406+
fn foo(x: S) { /* ... */ }
407+
# #[cfg(for_demonstration_only)]
408+
extern "C" { fn foo(x: S); }
409+
# #[cfg(for_demonstration_only)]
410+
impl S { fn foo(self) { /* ... */ } }
411+
```
412+
413+
the type of `foo` is **not** `fn(S)`, as one might expect.
414+
Rather, it is a unique, zero-sized marker type written here as `typeof(foo)`.
415+
However, `typeof(foo)` can be _coerced_ to a function pointer `fn(S)`,
416+
so you rarely notice this:
417+
418+
```
419+
# struct S;
420+
# fn foo(_: S) {}
421+
let x: fn(S) = foo; // OK, coerces
422+
```
423+
424+
The reason that this matter is that the type `fn(S)` is not specific to
425+
any particular function: it's a function _pointer_. So calling `x()` results
426+
in a virtual call, whereas `foo()` is statically dispatched, because the type
427+
of `foo` tells us precisely what function is being called.
428+
429+
As noted above, coercions mean that most code doesn't have to be
430+
concerned with this distinction. However, you can tell the difference
431+
when using **transmute** to convert a fn item into a fn pointer.
432+
433+
This is sometimes done as part of an FFI:
434+
435+
```compile_fail,E0591
436+
extern "C" fn foo(userdata: Box<i32>) {
437+
/* ... */
438+
}
439+
440+
# fn callback(_: extern "C" fn(*mut i32)) {}
441+
# use std::mem::transmute;
442+
# unsafe {
443+
let f: extern "C" fn(*mut i32) = transmute(foo);
444+
callback(f);
445+
# }
446+
```
447+
448+
Here, transmute is being used to convert the types of the fn arguments.
449+
This pattern is incorrect because, because the type of `foo` is a function
450+
**item** (`typeof(foo)`), which is zero-sized, and the target type (`fn()`)
451+
is a function pointer, which is not zero-sized.
452+
This pattern should be rewritten. There are a few possible ways to do this:
453+
454+
- change the original fn declaration to match the expected signature,
455+
and do the cast in the fn body (the preferred option)
456+
- cast the fn item fo a fn pointer before calling transmute, as shown here:
457+
458+
```
459+
# extern "C" fn foo(_: Box<i32>) {}
460+
# use std::mem::transmute;
461+
# unsafe {
462+
let f: extern "C" fn(*mut i32) = transmute(foo as extern "C" fn(_));
463+
let f: extern "C" fn(*mut i32) = transmute(foo as usize); // works too
464+
# }
465+
```
466+
467+
The same applies to transmutes to `*mut fn()`, which were observed in practice.
468+
Note though that use of this type is generally incorrect.
469+
The intention is typically to describe a function pointer, but just `fn()`
470+
alone suffices for that. `*mut fn()` is a pointer to a fn pointer.
471+
(Since these values are typically just passed to C code, however, this rarely
472+
makes a difference in practice.)
473+
474+
[rfc401]: https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md
475+
"##,
476+
477+
E0512: r##"
478+
Transmute with two differently sized types was attempted. Erroneous code
479+
example:
480+
481+
```compile_fail,E0512
482+
fn takes_u8(_: u8) {}
483+
484+
fn main() {
485+
unsafe { takes_u8(::std::mem::transmute(0u16)); }
486+
// error: cannot transmute between types of different sizes,
487+
// or dependently-sized types
488+
}
489+
```
490+
491+
Please use types with same size or use the expected type directly. Example:
492+
493+
```
494+
fn takes_u8(_: u8) {}
495+
496+
fn main() {
497+
unsafe { takes_u8(::std::mem::transmute(0i8)); } // ok!
498+
// or:
499+
unsafe { takes_u8(0u8); } // ok!
500+
}
501+
```
502+
"##,
503+
399504
;
400505
E0226, // only a single explicit lifetime bound is permitted
401506
E0472, // asm! is unsupported on this target

src/librustc/middle/intrinsicck.rs renamed to src/librustc_passes/intrinsicck.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
use crate::hir::def::{Res, DefKind};
2-
use crate::hir::def_id::DefId;
3-
use crate::ty::{self, Ty, TyCtxt};
4-
use crate::ty::layout::{LayoutError, Pointer, SizeSkeleton, VariantIdx};
5-
use crate::ty::query::Providers;
1+
use rustc::hir::def::{Res, DefKind};
2+
use rustc::hir::def_id::DefId;
3+
use rustc::ty::{self, Ty, TyCtxt};
4+
use rustc::ty::layout::{LayoutError, Pointer, SizeSkeleton, VariantIdx};
5+
use rustc::ty::query::Providers;
66

77
use rustc_target::spec::abi::Abi::RustIntrinsic;
88
use rustc_index::vec::Idx;
99
use syntax_pos::{Span, sym};
10-
use crate::hir::intravisit::{self, Visitor, NestedVisitorMap};
11-
use crate::hir;
10+
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
11+
use rustc::hir;
1212

1313
fn check_mod_intrinsics(tcx: TyCtxt<'_>, module_def_id: DefId) {
1414
tcx.hir().visit_item_likes_in_module(

src/librustc_passes/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ pub mod loops;
2929
pub mod dead;
3030
pub mod entry;
3131
mod liveness;
32+
mod intrinsicck;
3233

3334
pub fn provide(providers: &mut Providers<'_>) {
3435
entry::provide(providers);
3536
loops::provide(providers);
3637
liveness::provide(providers);
38+
intrinsicck::provide(providers);
3739
}

0 commit comments

Comments
 (0)