diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-06-20 08:36:03 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-06-20 08:36:03 +0200 |
| commit | 942a7fec30e93ca466a30287c8764cfdd8158d9e (patch) | |
| tree | 5218a52f0662893cd3f4067c6349629a7205dc5f /src/libsyntax | |
| parent | 2ead0072b6a27d9b15fc22c181fdd6d129b20b08 (diff) | |
| parent | 673c3fc23ad0c78fe26420b641957e4fdfea553b (diff) | |
| download | rust-942a7fec30e93ca466a30287c8764cfdd8158d9e.tar.gz rust-942a7fec30e93ca466a30287c8764cfdd8158d9e.zip | |
Rollup merge of #61968 - eddyb:hir-noclone, r=petrochenkov
rustc: disallow cloning HIR nodes.
Besides being inefficient, cloning also risks creating broken HIR (without properly recreating all the IDs and whatnot, in which case you might as well reconstruct the entire node without ever `Clone`-ing anything).
We detect *some* detrimental situations (based on the occurrence of `HirId`s, I believe?), but it's better to statically disallow it, IMO.
One of the examples that is fixed by this PR is `tcx.hir().fn_decl{,_by_hir_id}`, which was cloning an entire `hir::FnDecl` *every single time it was called*.
r? @petrochenkov cc @rust-lang/compiler
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/lib.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/ptr.rs | 11 |
2 files changed, 11 insertions, 2 deletions
diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 55db8da3276..337b8424736 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -12,6 +12,8 @@ #![deny(unused_lifetimes)] #![feature(bind_by_move_pattern_guards)] +#![feature(const_fn)] +#![feature(const_transmute)] #![feature(crate_visibility_modifier)] #![feature(label_break_value)] #![feature(nll)] diff --git a/src/libsyntax/ptr.rs b/src/libsyntax/ptr.rs index d577243fb3d..f0cfa5a84a8 100644 --- a/src/libsyntax/ptr.rs +++ b/src/libsyntax/ptr.rs @@ -133,8 +133,15 @@ impl<T: Encodable> Encodable for P<T> { } impl<T> P<[T]> { - pub fn new() -> P<[T]> { - P { ptr: Default::default() } + pub const fn new() -> P<[T]> { + // HACK(eddyb) bypass the lack of a `const fn` to create an empty `Box<[T]>` + // (as trait methods, `default` in this case, can't be `const fn` yet). + P { + ptr: unsafe { + use std::ptr::NonNull; + std::mem::transmute(NonNull::<[T; 0]>::dangling() as NonNull<[T]>) + }, + } } #[inline(never)] |
