diff --git a/Cargo.lock b/Cargo.lock index 87471451fa754..f6b5b317646c3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4624,6 +4624,10 @@ dependencies = [ "walkdir", ] +[[package]] +name = "tier-check" +version = "0.1.0" + [[package]] name = "time" version = "0.1.42" diff --git a/Cargo.toml b/Cargo.toml index 1936e35aa4c5d..87e958a1bafe6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ members = [ "src/tools/rustbook", "src/tools/unstable-book-gen", "src/tools/tidy", + "src/tools/tier-check", "src/tools/build-manifest", "src/tools/remote-test-client", "src/tools/remote-test-server", diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index c0bf3833b9c33..66ae064a3760b 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -364,3 +364,66 @@ macro_rules! array_impl_default { } array_impl_default! {32, T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T} + +#[cfg(not(bootstrap))] +#[lang = "array"] +impl<T, const N: usize> [T; N] { + /// Returns an array of the same size as `self`, with function `f` applied to each element + /// in order. + /// + /// # Examples + /// + /// ``` + /// #![feature(array_map)] + /// let x = [1, 2, 3]; + /// let y = x.map(|v| v + 1); + /// assert_eq!(y, [2, 3, 4]); + /// + /// let x = [1, 2, 3]; + /// let mut temp = 0; + /// let y = x.map(|v| { temp += 1; v * temp }); + /// assert_eq!(y, [1, 4, 9]); + /// + /// let x = ["Ferris", "Bueller's", "Day", "Off"]; + /// let y = x.map(|v| v.len()); + /// assert_eq!(y, [6, 9, 3, 3]); + /// ``` + #[unstable(feature = "array_map", issue = "75243")] + pub fn map<F, U>(self, mut f: F) -> [U; N] + where + F: FnMut(T) -> U, + { + use crate::mem::MaybeUninit; + struct Guard<T, const N: usize> { + dst: *mut T, + initialized: usize, + } + + impl<T, const N: usize> Drop for Guard<T, N> { + fn drop(&mut self) { + debug_assert!(self.initialized <= N); + + let initialized_part = + crate::ptr::slice_from_raw_parts_mut(self.dst, self.initialized); + // SAFETY: this raw slice will contain only initialized objects + // that's why, it is allowed to drop it. + unsafe { + crate::ptr::drop_in_place(initialized_part); + } + } + } + let mut dst = MaybeUninit::uninit_array::<N>(); + let mut guard: Guard<U, N> = + Guard { dst: MaybeUninit::first_ptr_mut(&mut dst), initialized: 0 }; + for (src, dst) in IntoIter::new(self).zip(&mut dst) { + dst.write(f(src)); + guard.initialized += 1; + } + // FIXME: Convert to crate::mem::transmute once it works with generics. + // unsafe { crate::mem::transmute::<[MaybeUninit<U>; N], [U; N]>(dst) } + crate::mem::forget(guard); + // SAFETY: At this point we've properly initialized the whole array + // and we just need to cast it to the correct type. + unsafe { (&mut dst as *mut _ as *mut [U; N]).read() } + } +} diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index fcf5454308b47..763457d485da4 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -145,6 +145,7 @@ #![feature(abi_unadjusted)] #![feature(adx_target_feature)] #![feature(maybe_uninit_slice)] +#![feature(maybe_uninit_extra)] #![feature(external_doc)] #![feature(associated_type_bounds)] #![feature(const_caller_location)] diff --git a/library/core/tests/array.rs b/library/core/tests/array.rs index 4bc44e98fc802..33b464c430a92 100644 --- a/library/core/tests/array.rs +++ b/library/core/tests/array.rs @@ -290,3 +290,42 @@ fn empty_array_is_always_default() { let _arr = <[DoesNotImplDefault; 0]>::default(); } + +#[test] +fn array_map() { + let a = [1, 2, 3]; + let b = a.map(|v| v + 1); + assert_eq!(b, [2, 3, 4]); + + let a = [1u8, 2, 3]; + let b = a.map(|v| v as u64); + assert_eq!(b, [1, 2, 3]); +} + +#[test] +fn array_map_drop_safety() { + use core::sync::atomic::AtomicUsize; + use core::sync::atomic::Ordering; + static DROPPED: AtomicUsize = AtomicUsize::new(0); + struct DropCounter; + impl Drop for DropCounter { + fn drop(&mut self) { + DROPPED.fetch_add(1, Ordering::SeqCst); + } + } + + let num_to_create = 5; + let success = std::panic::catch_unwind(|| { + let items = [0; 10]; + let mut nth = 0; + items.map(|_| { + if nth == num_to_create { + panic!("Oh no!"); + } + nth += 1; + DropCounter + }); + }); + assert!(success.is_err()); + assert_eq!(DROPPED.load(Ordering::SeqCst), num_to_create); +} diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index b4c299d390586..904e3f7284049 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -1,5 +1,6 @@ #![feature(alloc_layout_extra)] #![feature(array_chunks)] +#![feature(array_map)] #![feature(bool_to_option)] #![feature(bound_cloned)] #![feature(box_syntax)] diff --git a/library/std/src/primitive_docs.rs b/library/std/src/primitive_docs.rs index f9c96b7c3d4b3..0d2aca6bbc3eb 100644 --- a/library/std/src/primitive_docs.rs +++ b/library/std/src/primitive_docs.rs @@ -768,7 +768,8 @@ mod prim_tuple {} /// /// This type can represent a wide range of decimal numbers, like `3.5`, `27`, /// `-113.75`, `0.0078125`, `34359738368`, `0`, `-1`. So unlike integer types -/// (like `i32`), floating point types can represent non-integer numbers, too. +/// (such as `i32`), floating point types can represent non-integer numbers, +/// too. /// /// However, being able to represent this wide range of numbers comes at the /// cost of precision: floats can only represent some of the real numbers and @@ -779,15 +780,12 @@ mod prim_tuple {} /// often discard insignificant digits: `println!("{}", 1.0f32 / 5.0f32)` will /// print `0.2`. /// -/// The precision is better for numbers near 0 and worse for large numbers. For -/// example, above 2<sup>24</sup>, not even all integers are representable. -/// /// Additionally, `f32` can represent a couple of special values: /// /// - `-0`: this is just due to how floats are encoded. It is semantically /// equivalent to `0` and `-0.0 == 0.0` results in `true`. /// - [∞](#associatedconstant.INFINITY) and -/// [-∞](#associatedconstant.NEG_INFINITY): these result from calculations +/// [−∞](#associatedconstant.NEG_INFINITY): these result from calculations /// like `1.0 / 0.0`. /// - [NaN (not a number)](#associatedconstant.NAN): this value results from /// calculations like `(-1.0).sqrt()`. NaN has some potentially unexpected diff --git a/library/std/src/sys/cloudabi/abi/cloudabi.rs b/library/std/src/sys/cloudabi/abi/cloudabi.rs index b02faf1830c53..5c4e3fd85c41c 100644 --- a/library/std/src/sys/cloudabi/abi/cloudabi.rs +++ b/library/std/src/sys/cloudabi/abi/cloudabi.rs @@ -1910,7 +1910,7 @@ extern "C" { /// The resolution of the clock. #[inline] pub unsafe fn clock_res_get(clock_id_: clockid, resolution_: &mut timestamp) -> errno { - cloudabi_sys_clock_res_get(clock_id_, resolution_) + unsafe { cloudabi_sys_clock_res_get(clock_id_, resolution_) } } /// Obtains the time value of a clock. @@ -1934,7 +1934,7 @@ pub unsafe fn clock_time_get( precision_: timestamp, time_: *mut timestamp, ) -> errno { - cloudabi_sys_clock_time_get(clock_id_, precision_, time_) + unsafe { cloudabi_sys_clock_time_get(clock_id_, precision_, time_) } } /// Wakes up threads waiting on a userspace condition variable. @@ -1961,7 +1961,7 @@ pub unsafe fn clock_time_get( /// threads, all threads are woken up. #[inline] pub unsafe fn condvar_signal(condvar_: *mut condvar, scope_: scope, nwaiters_: nthreads) -> errno { - cloudabi_sys_condvar_signal(condvar_, scope_, nwaiters_) + unsafe { cloudabi_sys_condvar_signal(condvar_, scope_, nwaiters_) } } /// Closes a file descriptor. @@ -1972,7 +1972,7 @@ pub unsafe fn condvar_signal(condvar_: *mut condvar, scope_: scope, nwaiters_: n /// The file descriptor that needs to be closed. #[inline] pub unsafe fn fd_close(fd_: fd) -> errno { - cloudabi_sys_fd_close(fd_) + unsafe { cloudabi_sys_fd_close(fd_) } } /// Creates a file descriptor. @@ -1990,7 +1990,7 @@ pub unsafe fn fd_close(fd_: fd) -> errno { /// The file descriptor that has been created. #[inline] pub unsafe fn fd_create1(type_: filetype, fd_: &mut fd) -> errno { - cloudabi_sys_fd_create1(type_, fd_) + unsafe { cloudabi_sys_fd_create1(type_, fd_) } } /// Creates a pair of file descriptors. @@ -2013,7 +2013,8 @@ pub unsafe fn fd_create1(type_: filetype, fd_: &mut fd) -> errno { /// The second file descriptor of the pair. #[inline] pub unsafe fn fd_create2(type_: filetype, fd1_: &mut fd, fd2_: &mut fd) -> errno { - cloudabi_sys_fd_create2(type_, fd1_, fd2_) + // SAFETY: the caller must uphold the safety contract for `cloudabi_sys_fd_create2`. + unsafe { cloudabi_sys_fd_create2(type_, fd1_, fd2_) } } /// Synchronizes the data of a file to disk. @@ -2025,7 +2026,9 @@ pub unsafe fn fd_create2(type_: filetype, fd1_: &mut fd, fd2_: &mut fd) -> errno /// needs to be synchronized to disk. #[inline] pub unsafe fn fd_datasync(fd_: fd) -> errno { - cloudabi_sys_fd_datasync(fd_) + // SAFETY: the caller must guarantee that `fd` is valid + // for synchronization. + unsafe { cloudabi_sys_fd_datasync(fd_) } } /// Duplicates a file descriptor. @@ -2040,7 +2043,7 @@ pub unsafe fn fd_datasync(fd_: fd) -> errno { /// The new file descriptor. #[inline] pub unsafe fn fd_dup(from_: fd, fd_: &mut fd) -> errno { - cloudabi_sys_fd_dup(from_, fd_) + unsafe { cloudabi_sys_fd_dup(from_, fd_) } } /// Reads from a file descriptor, without using and updating the @@ -2064,7 +2067,7 @@ pub unsafe fn fd_dup(from_: fd, fd_: &mut fd) -> errno { /// The number of bytes read. #[inline] pub unsafe fn fd_pread(fd_: fd, iovs_: &[iovec], offset_: filesize, nread_: &mut usize) -> errno { - cloudabi_sys_fd_pread(fd_, iovs_.as_ptr(), iovs_.len(), offset_, nread_) + unsafe { cloudabi_sys_fd_pread(fd_, iovs_.as_ptr(), iovs_.len(), offset_, nread_) } } /// Writes to a file descriptor, without using and updating the @@ -2093,7 +2096,7 @@ pub unsafe fn fd_pwrite( offset_: filesize, nwritten_: &mut usize, ) -> errno { - cloudabi_sys_fd_pwrite(fd_, iovs_.as_ptr(), iovs_.len(), offset_, nwritten_) + unsafe { cloudabi_sys_fd_pwrite(fd_, iovs_.as_ptr(), iovs_.len(), offset_, nwritten_) } } /// Reads from a file descriptor. @@ -2112,7 +2115,7 @@ pub unsafe fn fd_pwrite( /// The number of bytes read. #[inline] pub unsafe fn fd_read(fd_: fd, iovs_: &[iovec], nread_: &mut usize) -> errno { - cloudabi_sys_fd_read(fd_, iovs_.as_ptr(), iovs_.len(), nread_) + unsafe { cloudabi_sys_fd_read(fd_, iovs_.as_ptr(), iovs_.len(), nread_) } } /// Atomically replaces a file descriptor by a copy of another @@ -2138,7 +2141,7 @@ pub unsafe fn fd_read(fd_: fd, iovs_: &[iovec], nread_: &mut usize) -> errno { /// overwritten. #[inline] pub unsafe fn fd_replace(from_: fd, to_: fd) -> errno { - cloudabi_sys_fd_replace(from_, to_) + unsafe { cloudabi_sys_fd_replace(from_, to_) } } /// Moves the offset of the file descriptor. @@ -2166,7 +2169,7 @@ pub unsafe fn fd_seek( whence_: whence, newoffset_: &mut filesize, ) -> errno { - cloudabi_sys_fd_seek(fd_, offset_, whence_, newoffset_) + unsafe { cloudabi_sys_fd_seek(fd_, offset_, whence_, newoffset_) } } /// Gets attributes of a file descriptor. @@ -2182,7 +2185,7 @@ pub unsafe fn fd_seek( /// attributes are stored. #[inline] pub unsafe fn fd_stat_get(fd_: fd, buf_: *mut fdstat) -> errno { - cloudabi_sys_fd_stat_get(fd_, buf_) + unsafe { cloudabi_sys_fd_stat_get(fd_, buf_) } } /// Adjusts attributes of a file descriptor. @@ -2202,7 +2205,7 @@ pub unsafe fn fd_stat_get(fd_: fd, buf_: *mut fdstat) -> errno { /// be adjusted. #[inline] pub unsafe fn fd_stat_put(fd_: fd, buf_: *const fdstat, flags_: fdsflags) -> errno { - cloudabi_sys_fd_stat_put(fd_, buf_, flags_) + unsafe { cloudabi_sys_fd_stat_put(fd_, buf_, flags_) } } /// Synchronizes the data and metadata of a file to disk. @@ -2214,7 +2217,7 @@ pub unsafe fn fd_stat_put(fd_: fd, buf_: *const fdstat, flags_: fdsflags) -> err /// and metadata needs to be synchronized to disk. #[inline] pub unsafe fn fd_sync(fd_: fd) -> errno { - cloudabi_sys_fd_sync(fd_) + unsafe { cloudabi_sys_fd_sync(fd_) } } /// Writes to a file descriptor. @@ -2233,7 +2236,7 @@ pub unsafe fn fd_sync(fd_: fd) -> errno { /// The number of bytes written. #[inline] pub unsafe fn fd_write(fd_: fd, iovs_: &[ciovec], nwritten_: &mut usize) -> errno { - cloudabi_sys_fd_write(fd_, iovs_.as_ptr(), iovs_.len(), nwritten_) + unsafe { cloudabi_sys_fd_write(fd_, iovs_.as_ptr(), iovs_.len(), nwritten_) } } /// Provides file advisory information on a file descriptor. @@ -2256,7 +2259,7 @@ pub unsafe fn fd_write(fd_: fd, iovs_: &[ciovec], nwritten_: &mut usize) -> errn /// The advice. #[inline] pub unsafe fn file_advise(fd_: fd, offset_: filesize, len_: filesize, advice_: advice) -> errno { - cloudabi_sys_file_advise(fd_, offset_, len_, advice_) + unsafe { cloudabi_sys_file_advise(fd_, offset_, len_, advice_) } } /// Forces the allocation of space in a file. @@ -2275,7 +2278,7 @@ pub unsafe fn file_advise(fd_: fd, offset_: filesize, len_: filesize, advice_: a /// The length of the area that is allocated. #[inline] pub unsafe fn file_allocate(fd_: fd, offset_: filesize, len_: filesize) -> errno { - cloudabi_sys_file_allocate(fd_, offset_, len_) + unsafe { cloudabi_sys_file_allocate(fd_, offset_, len_) } } /// Creates a file of a specified type. @@ -2296,7 +2299,7 @@ pub unsafe fn file_allocate(fd_: fd, offset_: filesize, len_: filesize) -> errno /// Creates a directory. #[inline] pub unsafe fn file_create(fd_: fd, path_: &[u8], type_: filetype) -> errno { - cloudabi_sys_file_create(fd_, path_.as_ptr(), path_.len(), type_) + unsafe { cloudabi_sys_file_create(fd_, path_.as_ptr(), path_.len(), type_)} } /// Creates a hard link. @@ -2320,7 +2323,7 @@ pub unsafe fn file_create(fd_: fd, path_: &[u8], type_: filetype) -> errno { /// should be created. #[inline] pub unsafe fn file_link(fd1_: lookup, path1_: &[u8], fd2_: fd, path2_: &[u8]) -> errno { - cloudabi_sys_file_link(fd1_, path1_.as_ptr(), path1_.len(), fd2_, path2_.as_ptr(), path2_.len()) + unsafe { cloudabi_sys_file_link(fd1_, path1_.as_ptr(), path1_.len(), fd2_, path2_.as_ptr(), path2_.len()) } } /// Opens a file. @@ -2362,7 +2365,7 @@ pub unsafe fn file_open( fds_: *const fdstat, fd_: &mut fd, ) -> errno { - cloudabi_sys_file_open(dirfd_, path_.as_ptr(), path_.len(), oflags_, fds_, fd_) + unsafe { cloudabi_sys_file_open(dirfd_, path_.as_ptr(), path_.len(), oflags_, fds_, fd_) } } /// Reads directory entries from a directory. @@ -2402,7 +2405,7 @@ pub unsafe fn file_readdir( cookie_: dircookie, bufused_: &mut usize, ) -> errno { - cloudabi_sys_file_readdir(fd_, buf_.as_mut_ptr() as *mut (), buf_.len(), cookie_, bufused_) + unsafe { cloudabi_sys_file_readdir(fd_, buf_.as_mut_ptr() as *mut (), buf_.len(), cookie_, bufused_) } } /// Reads the contents of a symbolic link. @@ -2425,14 +2428,16 @@ pub unsafe fn file_readdir( /// The number of bytes placed in the buffer. #[inline] pub unsafe fn file_readlink(fd_: fd, path_: &[u8], buf_: &mut [u8], bufused_: &mut usize) -> errno { - cloudabi_sys_file_readlink( - fd_, - path_.as_ptr(), - path_.len(), - buf_.as_mut_ptr(), - buf_.len(), - bufused_, - ) + unsafe { + cloudabi_sys_file_readlink( + fd_, + path_.as_ptr(), + path_.len(), + buf_.as_mut_ptr(), + buf_.len(), + bufused_, + ) + } } /// Renames a file. @@ -2456,14 +2461,16 @@ pub unsafe fn file_readlink(fd_: fd, path_: &[u8], buf_: &mut [u8], bufused_: &m /// be renamed. #[inline] pub unsafe fn file_rename(fd1_: fd, path1_: &[u8], fd2_: fd, path2_: &[u8]) -> errno { - cloudabi_sys_file_rename( - fd1_, - path1_.as_ptr(), - path1_.len(), - fd2_, - path2_.as_ptr(), - path2_.len(), - ) + unsafe { + cloudabi_sys_file_rename( + fd1_, + path1_.as_ptr(), + path1_.len(), + fd2_, + path2_.as_ptr(), + path2_.len(), + ) + } } /// Gets attributes of a file by file descriptor. @@ -2479,7 +2486,7 @@ pub unsafe fn file_rename(fd1_: fd, path1_: &[u8], fd2_: fd, path2_: &[u8]) -> e /// stored. #[inline] pub unsafe fn file_stat_fget(fd_: fd, buf_: *mut filestat) -> errno { - cloudabi_sys_file_stat_fget(fd_, buf_) + unsafe { cloudabi_sys_file_stat_fget(fd_, buf_) } } /// Adjusts attributes of a file by file descriptor. @@ -2499,7 +2506,7 @@ pub unsafe fn file_stat_fget(fd_: fd, buf_: *mut filestat) -> errno { /// be adjusted. #[inline] pub unsafe fn file_stat_fput(fd_: fd, buf_: *const filestat, flags_: fsflags) -> errno { - cloudabi_sys_file_stat_fput(fd_, buf_, flags_) + unsafe { cloudabi_sys_file_stat_fput(fd_, buf_, flags_) } } /// Gets attributes of a file by path. @@ -2520,7 +2527,7 @@ pub unsafe fn file_stat_fput(fd_: fd, buf_: *const filestat, flags_: fsflags) -> /// stored. #[inline] pub unsafe fn file_stat_get(fd_: lookup, path_: &[u8], buf_: *mut filestat) -> errno { - cloudabi_sys_file_stat_get(fd_, path_.as_ptr(), path_.len(), buf_) + unsafe { cloudabi_sys_file_stat_get(fd_, path_.as_ptr(), path_.len(), buf_) } } /// Adjusts attributes of a file by path. @@ -2550,7 +2557,7 @@ pub unsafe fn file_stat_put( buf_: *const filestat, flags_: fsflags, ) -> errno { - cloudabi_sys_file_stat_put(fd_, path_.as_ptr(), path_.len(), buf_, flags_) + unsafe { cloudabi_sys_file_stat_put(fd_, path_.as_ptr(), path_.len(), buf_, flags_) } } /// Creates a symbolic link. @@ -2569,7 +2576,7 @@ pub unsafe fn file_stat_put( /// link should be created. #[inline] pub unsafe fn file_symlink(path1_: &[u8], fd_: fd, path2_: &[u8]) -> errno { - cloudabi_sys_file_symlink(path1_.as_ptr(), path1_.len(), fd_, path2_.as_ptr(), path2_.len()) + unsafe { cloudabi_sys_file_symlink(path1_.as_ptr(), path1_.len(), fd_, path2_.as_ptr(), path2_.len()) } } /// Unlinks a file, or removes a directory. @@ -2591,7 +2598,7 @@ pub unsafe fn file_symlink(path1_: &[u8], fd_: fd, path2_: &[u8]) -> errno { /// Otherwise, unlink a file. #[inline] pub unsafe fn file_unlink(fd_: fd, path_: &[u8], flags_: ulflags) -> errno { - cloudabi_sys_file_unlink(fd_, path_.as_ptr(), path_.len(), flags_) + unsafe { cloudabi_sys_file_unlink(fd_, path_.as_ptr(), path_.len(), flags_) } } /// Unlocks a write-locked userspace lock. @@ -2618,7 +2625,7 @@ pub unsafe fn file_unlink(fd_: fd, path_: &[u8], flags_: ulflags) -> errno { /// shared memory. #[inline] pub unsafe fn lock_unlock(lock_: *mut lock, scope_: scope) -> errno { - cloudabi_sys_lock_unlock(lock_, scope_) + unsafe { cloudabi_sys_lock_unlock(lock_, scope_) } } /// Provides memory advisory information on a region of memory. @@ -2633,7 +2640,7 @@ pub unsafe fn lock_unlock(lock_: *mut lock, scope_: scope) -> errno { /// The advice. #[inline] pub unsafe fn mem_advise(mapping_: &mut [u8], advice_: advice) -> errno { - cloudabi_sys_mem_advise(mapping_.as_mut_ptr() as *mut (), mapping_.len(), advice_) + unsafe { cloudabi_sys_mem_advise(mapping_.as_mut_ptr() as *mut (), mapping_.len(), advice_) } } /// Creates a memory mapping, making the contents of a file @@ -2682,7 +2689,7 @@ pub unsafe fn mem_map( off_: filesize, mem_: &mut *mut (), ) -> errno { - cloudabi_sys_mem_map(addr_, len_, prot_, flags_, fd_, off_, mem_) + unsafe { cloudabi_sys_mem_map(addr_, len_, prot_, flags_, fd_, off_, mem_) } } /// Changes the protection of a memory mapping. @@ -2696,7 +2703,7 @@ pub unsafe fn mem_map( /// New protection options. #[inline] pub unsafe fn mem_protect(mapping_: &mut [u8], prot_: mprot) -> errno { - cloudabi_sys_mem_protect(mapping_.as_mut_ptr() as *mut (), mapping_.len(), prot_) + unsafe { cloudabi_sys_mem_protect(mapping_.as_mut_ptr() as *mut (), mapping_.len(), prot_) } } /// Synchronizes a region of memory with its physical storage. @@ -2710,7 +2717,7 @@ pub unsafe fn mem_protect(mapping_: &mut [u8], prot_: mprot) -> errno { /// The method of synchronization. #[inline] pub unsafe fn mem_sync(mapping_: &mut [u8], flags_: msflags) -> errno { - cloudabi_sys_mem_sync(mapping_.as_mut_ptr() as *mut (), mapping_.len(), flags_) + unsafe { cloudabi_sys_mem_sync(mapping_.as_mut_ptr() as *mut (), mapping_.len(), flags_) } } /// Unmaps a region of memory. @@ -2721,7 +2728,7 @@ pub unsafe fn mem_sync(mapping_: &mut [u8], flags_: msflags) -> errno { /// The pages that needs to be unmapped. #[inline] pub unsafe fn mem_unmap(mapping_: &mut [u8]) -> errno { - cloudabi_sys_mem_unmap(mapping_.as_mut_ptr() as *mut (), mapping_.len()) + unsafe { cloudabi_sys_mem_unmap(mapping_.as_mut_ptr() as *mut (), mapping_.len()) } } /// Concurrently polls for the occurrence of a set of events. @@ -2746,7 +2753,7 @@ pub unsafe fn poll( nsubscriptions_: usize, nevents_: *mut usize, ) -> errno { - cloudabi_sys_poll(in_, out_, nsubscriptions_, nevents_) + unsafe { cloudabi_sys_poll(in_, out_, nsubscriptions_, nevents_) } } /// Replaces the process by a new executable. @@ -2784,7 +2791,7 @@ pub unsafe fn poll( /// execution. #[inline] pub unsafe fn proc_exec(fd_: fd, data_: &[u8], fds_: &[fd]) -> errno { - cloudabi_sys_proc_exec(fd_, data_.as_ptr() as *const (), data_.len(), fds_.as_ptr(), fds_.len()) + unsafe { cloudabi_sys_proc_exec(fd_, data_.as_ptr() as *const (), data_.len(), fds_.as_ptr(), fds_.len()) } } /// Terminates the process normally. @@ -2797,7 +2804,7 @@ pub unsafe fn proc_exec(fd_: fd, data_: &[u8], fds_: &[fd]) -> errno { /// through [`event.union.proc_terminate.exitcode`](struct.event_proc_terminate.html#structfield.exitcode). #[inline] pub unsafe fn proc_exit(rval_: exitcode) -> ! { - cloudabi_sys_proc_exit(rval_) + unsafe { cloudabi_sys_proc_exit(rval_) } } /// Forks the process of the calling thread. @@ -2822,7 +2829,7 @@ pub unsafe fn proc_exit(rval_: exitcode) -> ! { /// initial thread of the child process. #[inline] pub unsafe fn proc_fork(fd_: &mut fd, tid_: &mut tid) -> errno { - cloudabi_sys_proc_fork(fd_, tid_) + unsafe { cloudabi_sys_proc_fork(fd_, tid_) } } /// Sends a signal to the process of the calling thread. @@ -2837,7 +2844,7 @@ pub unsafe fn proc_fork(fd_: &mut fd, tid_: &mut tid) -> errno { /// [`event.union.proc_terminate.signal`](struct.event_proc_terminate.html#structfield.signal). #[inline] pub unsafe fn proc_raise(sig_: signal) -> errno { - cloudabi_sys_proc_raise(sig_) + unsafe { cloudabi_sys_proc_raise(sig_) } } /// Obtains random data from the kernel random number generator. @@ -2853,7 +2860,7 @@ pub unsafe fn proc_raise(sig_: signal) -> errno { /// data. #[inline] pub unsafe fn random_get(buf_: &mut [u8]) -> errno { - cloudabi_sys_random_get(buf_.as_mut_ptr() as *mut (), buf_.len()) + unsafe { cloudabi_sys_random_get(buf_.as_mut_ptr() as *mut (), buf_.len()) } } /// Receives a message on a socket. @@ -2871,7 +2878,7 @@ pub unsafe fn random_get(buf_: &mut [u8]) -> errno { /// Output parameters. #[inline] pub unsafe fn sock_recv(sock_: fd, in_: *const recv_in, out_: *mut recv_out) -> errno { - cloudabi_sys_sock_recv(sock_, in_, out_) + unsafe { cloudabi_sys_sock_recv(sock_, in_, out_) } } /// Sends a message on a socket. @@ -2888,7 +2895,7 @@ pub unsafe fn sock_recv(sock_: fd, in_: *const recv_in, out_: *mut recv_out) -> /// Output parameters. #[inline] pub unsafe fn sock_send(sock_: fd, in_: *const send_in, out_: *mut send_out) -> errno { - cloudabi_sys_sock_send(sock_, in_, out_) + unsafe { cloudabi_sys_sock_send(sock_, in_, out_) } } /// Shuts down socket send and receive channels. @@ -2903,7 +2910,7 @@ pub unsafe fn sock_send(sock_: fd, in_: *const send_in, out_: *mut send_out) -> /// down. #[inline] pub unsafe fn sock_shutdown(sock_: fd, how_: sdflags) -> errno { - cloudabi_sys_sock_shutdown(sock_, how_) + unsafe { cloudabi_sys_sock_shutdown(sock_, how_) } } /// Creates a new thread within the current process. @@ -2917,7 +2924,7 @@ pub unsafe fn sock_shutdown(sock_: fd, how_: sdflags) -> errno { /// The thread ID of the new thread. #[inline] pub unsafe fn thread_create(attr_: *mut threadattr, tid_: &mut tid) -> errno { - cloudabi_sys_thread_create(attr_, tid_) + unsafe { cloudabi_sys_thread_create(attr_, tid_) } } /// Terminates the calling thread. @@ -2937,11 +2944,11 @@ pub unsafe fn thread_create(attr_: *mut threadattr, tid_: &mut tid) -> errno { /// shared memory. #[inline] pub unsafe fn thread_exit(lock_: *mut lock, scope_: scope) -> ! { - cloudabi_sys_thread_exit(lock_, scope_) + unsafe { cloudabi_sys_thread_exit(lock_, scope_) } } /// Temporarily yields execution of the calling thread. #[inline] pub unsafe fn thread_yield() -> errno { - cloudabi_sys_thread_yield() + unsafe { cloudabi_sys_thread_yield() } } diff --git a/library/std/src/sys/cloudabi/condvar.rs b/library/std/src/sys/cloudabi/condvar.rs index dabdc0c9b510a..0eddca55e6021 100644 --- a/library/std/src/sys/cloudabi/condvar.rs +++ b/library/std/src/sys/cloudabi/condvar.rs @@ -57,22 +57,24 @@ impl Condvar { ); // Call into the kernel to wait on the condition variable. - let condvar = self.condvar.get(); - let subscription = abi::subscription { - type_: abi::eventtype::CONDVAR, - union: abi::subscription_union { - condvar: abi::subscription_condvar { - condvar: condvar as *mut abi::condvar, - condvar_scope: abi::scope::PRIVATE, - lock: mutex as *mut abi::lock, - lock_scope: abi::scope::PRIVATE, + unsafe { + let condvar = self.condvar.get(); + let subscription = abi::subscription { + type_: abi::eventtype::CONDVAR, + union: abi::subscription_union { + condvar: abi::subscription_condvar { + condvar: condvar as *mut abi::condvar, + condvar_scope: abi::scope::PRIVATE, + lock: mutex as *mut abi::lock, + lock_scope: abi::scope::PRIVATE, + }, }, - }, - ..mem::zeroed() - }; - let mut event: mem::MaybeUninit<abi::event> = mem::MaybeUninit::uninit(); - let mut nevents: mem::MaybeUninit<usize> = mem::MaybeUninit::uninit(); - let ret = abi::poll(&subscription, event.as_mut_ptr(), 1, nevents.as_mut_ptr()); + ..mem::zeroed() + }; + let mut event: mem::MaybeUninit<abi::event> = mem::MaybeUninit::uninit(); + let mut nevents: mem::MaybeUninit<usize> = mem::MaybeUninit::uninit(); + let ret = abi::poll(&subscription, event.as_mut_ptr(), 1, nevents.as_mut_ptr()); + } assert_eq!(ret, abi::errno::SUCCESS, "Failed to wait on condition variable"); assert_eq!( event.assume_init().error, @@ -90,42 +92,44 @@ impl Condvar { ); // Call into the kernel to wait on the condition variable. - let condvar = self.condvar.get(); - let timeout = - checked_dur2intervals(&dur).expect("overflow converting duration to nanoseconds"); - let subscriptions = [ - abi::subscription { - type_: abi::eventtype::CONDVAR, - union: abi::subscription_union { - condvar: abi::subscription_condvar { - condvar: condvar as *mut abi::condvar, - condvar_scope: abi::scope::PRIVATE, - lock: mutex as *mut abi::lock, - lock_scope: abi::scope::PRIVATE, + unsafe { + let condvar = self.condvar.get(); + let timeout = + checked_dur2intervals(&dur).expect("overflow converting duration to nanoseconds"); + let subscriptions = [ + abi::subscription { + type_: abi::eventtype::CONDVAR, + union: abi::subscription_union { + condvar: abi::subscription_condvar { + condvar: condvar as *mut abi::condvar, + condvar_scope: abi::scope::PRIVATE, + lock: mutex as *mut abi::lock, + lock_scope: abi::scope::PRIVATE, + }, }, + ..mem::zeroed() }, - ..mem::zeroed() - }, - abi::subscription { - type_: abi::eventtype::CLOCK, - union: abi::subscription_union { - clock: abi::subscription_clock { - clock_id: abi::clockid::MONOTONIC, - timeout, - ..mem::zeroed() + abi::subscription { + type_: abi::eventtype::CLOCK, + union: abi::subscription_union { + clock: abi::subscription_clock { + clock_id: abi::clockid::MONOTONIC, + timeout, + ..mem::zeroed() + }, }, + ..mem::zeroed() }, - ..mem::zeroed() - }, - ]; - let mut events: [mem::MaybeUninit<abi::event>; 2] = [mem::MaybeUninit::uninit(); 2]; - let mut nevents: mem::MaybeUninit<usize> = mem::MaybeUninit::uninit(); - let ret = abi::poll( - subscriptions.as_ptr(), - mem::MaybeUninit::first_ptr_mut(&mut events), - 2, - nevents.as_mut_ptr(), - ); + ]; + let mut events: [mem::MaybeUninit<abi::event>; 2] = [mem::MaybeUninit::uninit(); 2]; + let mut nevents: mem::MaybeUninit<usize> = mem::MaybeUninit::uninit(); + let ret = abi::poll( + subscriptions.as_ptr(), + mem::MaybeUninit::first_ptr_mut(&mut events), + 2, + nevents.as_mut_ptr(), + ); + } assert_eq!(ret, abi::errno::SUCCESS, "Failed to wait on condition variable"); let nevents = nevents.assume_init(); for i in 0..nevents { diff --git a/library/std/src/sys/cloudabi/mod.rs b/library/std/src/sys/cloudabi/mod.rs index f7dd2c8d00fd2..13f1bc8826e61 100644 --- a/library/std/src/sys/cloudabi/mod.rs +++ b/library/std/src/sys/cloudabi/mod.rs @@ -1,3 +1,5 @@ +#![deny(unsafe_op_in_unsafe_fn)] + use crate::io::ErrorKind; use crate::mem; diff --git a/library/std/src/sys/cloudabi/mutex.rs b/library/std/src/sys/cloudabi/mutex.rs index 580ab0e8ad863..aa955298d59b7 100644 --- a/library/std/src/sys/cloudabi/mutex.rs +++ b/library/std/src/sys/cloudabi/mutex.rs @@ -106,7 +106,9 @@ impl ReentrantMutex { }; let mut event = MaybeUninit::<abi::event>::uninit(); let mut nevents = MaybeUninit::<usize>::uninit(); - let ret = abi::poll(&subscription, event.as_mut_ptr(), 1, nevents.as_mut_ptr()); + // SAFE: The caller must to ensure that `event` and `nevents` are initialized. + let ret = + unsafe { abi::poll(&subscription, event.as_mut_ptr(), 1, nevents.as_mut_ptr()) }; assert_eq!(ret, abi::errno::SUCCESS, "Failed to acquire mutex"); let event = event.assume_init(); assert_eq!(event.error, abi::errno::SUCCESS, "Failed to acquire mutex"); diff --git a/library/std/src/sys/cloudabi/rwlock.rs b/library/std/src/sys/cloudabi/rwlock.rs index b8af5af1d701c..8c28fb0ac8637 100644 --- a/library/std/src/sys/cloudabi/rwlock.rs +++ b/library/std/src/sys/cloudabi/rwlock.rs @@ -61,23 +61,25 @@ impl RWLock { pub unsafe fn read(&self) { if !self.try_read() { // Call into the kernel to acquire a read lock. - let lock = self.lock.get(); - let subscription = abi::subscription { - type_: abi::eventtype::LOCK_RDLOCK, - union: abi::subscription_union { - lock: abi::subscription_lock { - lock: lock as *mut abi::lock, - lock_scope: abi::scope::PRIVATE, + unsafe { + let lock = self.lock.get(); + let subscription = abi::subscription { + type_: abi::eventtype::LOCK_RDLOCK, + union: abi::subscription_union { + lock: abi::subscription_lock { + lock: lock as *mut abi::lock, + lock_scope: abi::scope::PRIVATE, + }, }, - }, - ..mem::zeroed() - }; - let mut event = MaybeUninit::<abi::event>::uninit(); - let mut nevents = MaybeUninit::<usize>::uninit(); - let ret = abi::poll(&subscription, event.as_mut_ptr(), 1, nevents.as_mut_ptr()); - assert_eq!(ret, abi::errno::SUCCESS, "Failed to acquire read lock"); - let event = event.assume_init(); - assert_eq!(event.error, abi::errno::SUCCESS, "Failed to acquire read lock"); + ..mem::zeroed() + }; + let mut event = MaybeUninit::<abi::event>::uninit(); + let mut nevents = MaybeUninit::<usize>::uninit(); + let ret = abi::poll(&subscription, event.as_mut_ptr(), 1, nevents.as_mut_ptr()); + assert_eq!(ret, abi::errno::SUCCESS, "Failed to acquire read lock"); + let event = event.assume_init(); + assert_eq!(event.error, abi::errno::SUCCESS, "Failed to acquire read lock"); + } RDLOCKS_ACQUIRED += 1; } @@ -140,46 +142,50 @@ impl RWLock { pub unsafe fn try_write(&self) -> bool { // Attempt to acquire the lock. - let lock = self.lock.get(); - if let Err(old) = (*lock).compare_exchange( - abi::LOCK_UNLOCKED.0, - __pthread_thread_id.0 | abi::LOCK_WRLOCKED.0, - Ordering::Acquire, - Ordering::Relaxed, - ) { - // Failure. Crash upon recursive acquisition. - assert_ne!( - old & !abi::LOCK_KERNEL_MANAGED.0, + unsafe { + let lock = self.lock.get(); + if let Err(old) = (*lock).compare_exchange( + abi::LOCK_UNLOCKED.0, __pthread_thread_id.0 | abi::LOCK_WRLOCKED.0, - "Attempted to recursive write-lock a rwlock", - ); - false - } else { - // Success. - true + Ordering::Acquire, + Ordering::Relaxed, + ) { + // Failure. Crash upon recursive acquisition. + assert_ne!( + old & !abi::LOCK_KERNEL_MANAGED.0, + __pthread_thread_id.0 | abi::LOCK_WRLOCKED.0, + "Attempted to recursive write-lock a rwlock", + ); + false + } else { + // Success. + true + } } } pub unsafe fn write(&self) { if !self.try_write() { // Call into the kernel to acquire a write lock. - let lock = self.lock.get(); - let subscription = abi::subscription { - type_: abi::eventtype::LOCK_WRLOCK, - union: abi::subscription_union { - lock: abi::subscription_lock { - lock: lock as *mut abi::lock, - lock_scope: abi::scope::PRIVATE, + unsafe { + let lock = self.lock.get(); + let subscription = abi::subscription { + type_: abi::eventtype::LOCK_WRLOCK, + union: abi::subscription_union { + lock: abi::subscription_lock { + lock: lock as *mut abi::lock, + lock_scope: abi::scope::PRIVATE, + }, }, - }, - ..mem::zeroed() - }; - let mut event = MaybeUninit::<abi::event>::uninit(); - let mut nevents = MaybeUninit::<usize>::uninit(); - let ret = abi::poll(&subscription, event.as_mut_ptr(), 1, nevents.as_mut_ptr()); - assert_eq!(ret, abi::errno::SUCCESS, "Failed to acquire write lock"); - let event = event.assume_init(); - assert_eq!(event.error, abi::errno::SUCCESS, "Failed to acquire write lock"); + ..mem::zeroed() + }; + let mut event = MaybeUninit::<abi::event>::uninit(); + let mut nevents = MaybeUninit::<usize>::uninit(); + let ret = abi::poll(&subscription, event.as_mut_ptr(), 1, nevents.as_mut_ptr()); + assert_eq!(ret, abi::errno::SUCCESS, "Failed to acquire write lock"); + let event = event.assume_init(); + assert_eq!(event.error, abi::errno::SUCCESS, "Failed to acquire write lock"); + } } } diff --git a/library/std/src/sys/wasi/fs.rs b/library/std/src/sys/wasi/fs.rs index 6782d845bb056..8408756f1b3be 100644 --- a/library/std/src/sys/wasi/fs.rs +++ b/library/std/src/sys/wasi/fs.rs @@ -46,6 +46,7 @@ pub struct DirEntry { pub struct OpenOptions { read: bool, write: bool, + append: bool, dirflags: wasi::Lookupflags, fdflags: wasi::Fdflags, oflags: wasi::Oflags, @@ -270,8 +271,9 @@ impl OpenOptions { } } - pub fn append(&mut self, set: bool) { - self.fdflag(wasi::FDFLAGS_APPEND, set); + pub fn append(&mut self, append: bool) { + self.append = append; + self.fdflag(wasi::FDFLAGS_APPEND, append); } pub fn dsync(&mut self, set: bool) { @@ -321,7 +323,7 @@ impl OpenOptions { base |= wasi::RIGHTS_FD_READ; base |= wasi::RIGHTS_FD_READDIR; } - if self.write { + if self.write || self.append { base |= wasi::RIGHTS_FD_WRITE; base |= wasi::RIGHTS_FD_DATASYNC; base |= wasi::RIGHTS_FD_ALLOCATE; diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index e13a5f2465336..4b0905bd6c16c 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -404,6 +404,7 @@ impl<'a> Builder<'a> { test::CrateLibrustc, test::CrateRustdoc, test::Linkcheck, + test::TierCheck, test::Cargotest, test::Cargo, test::Rls, diff --git a/src/bootstrap/test.rs b/src/bootstrap/test.rs index bb5b9296c0aa7..11e2564305f0d 100644 --- a/src/bootstrap/test.rs +++ b/src/bootstrap/test.rs @@ -2043,3 +2043,47 @@ impl Step for Bootstrap { run.builder.ensure(Bootstrap); } } + +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +pub struct TierCheck { + pub compiler: Compiler, + target: TargetSelection, +} + +impl Step for TierCheck { + type Output = (); + const DEFAULT: bool = true; + const ONLY_HOSTS: bool = true; + + fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { + run.path("src/tools/tier-check") + } + + fn make_run(run: RunConfig<'_>) { + let compiler = run.builder.compiler_for(run.builder.top_stage, run.host, run.host); + run.builder.ensure(TierCheck { compiler, target: run.host }); + } + + /// Tests the Platform Support page in the rustc book. + fn run(self, builder: &Builder<'_>) { + builder.ensure(compile::Std { compiler: self.compiler, target: self.target }); + let mut cargo = tool::prepare_tool_cargo( + builder, + self.compiler, + Mode::ToolRustc, + self.target, + "run", + "src/tools/tier-check", + SourceType::InTree, + &[], + ); + cargo.arg(builder.src.join("src/doc/rustc/src/platform-support.md")); + cargo.arg(&builder.rustc(self.compiler)); + if builder.is_verbose() { + cargo.arg("--verbose"); + } + + builder.info("platform support check"); + try_run(builder, &mut cargo.into()); + } +} diff --git a/src/doc/rustc/src/SUMMARY.md b/src/doc/rustc/src/SUMMARY.md index b603c7b231e68..57013e9194bc1 100644 --- a/src/doc/rustc/src/SUMMARY.md +++ b/src/doc/rustc/src/SUMMARY.md @@ -11,6 +11,7 @@ - [Deny-by-default lints](lints/listing/deny-by-default.md) - [Codegen options](codegen-options/index.md) - [JSON Output](json.md) +- [Platform Support](platform-support.md) - [Targets](targets/index.md) - [Built-in Targets](targets/built-in.md) - [Custom Targets](targets/custom.md) diff --git a/src/doc/rustc/src/platform-support.md b/src/doc/rustc/src/platform-support.md new file mode 100644 index 0000000000000..b8d3c985cb5b6 --- /dev/null +++ b/src/doc/rustc/src/platform-support.md @@ -0,0 +1,223 @@ +# Platform Support + +<style type="text/css"> + td code { + white-space: nowrap; + } +</style> + +Support for different platforms are organized into three tiers, each with a +different set of guarantees. + +Platforms are identified by their "target triple" which is the string to +inform the compiler what kind of output should be produced. The columns in the +tables below have the following meanings: + +* std: + * ✓ indicates the full standard library is available. + * \* indicates the target only supports [`no_std`] development. + * ? indicates the standard library support is unknown or a work-in-progress. +* host: A ✓ indicates that `rustc` and `cargo` can run on the host platform. + +[`no_std`]: https://rust-embedded.github.io/book/intro/no-std.html + +## Tier 1 + +Tier 1 platforms can be thought of as "guaranteed to work". +Specifically they will each satisfy the following requirements: + +* Official binary releases are provided for the platform. +* Automated testing is set up to run tests for the platform. +* Landing changes to the `rust-lang/rust` repository's master branch is gated + on tests passing. +* Documentation for how to use and how to build the platform is available. + +target | std | host | notes +-------|-----|------|------- +`i686-pc-windows-gnu` | ✓ | ✓ | 32-bit MinGW (Windows 7+) +`i686-pc-windows-msvc` | ✓ | ✓ | 32-bit MSVC (Windows 7+) +`i686-unknown-linux-gnu` | ✓ | ✓ | 32-bit Linux (kernel 2.6.32+, glibc 2.11+) +`x86_64-apple-darwin` | ✓ | ✓ | 64-bit OSX (10.7+, Lion+) +`x86_64-pc-windows-gnu` | ✓ | ✓ | 64-bit MinGW (Windows 7+) +`x86_64-pc-windows-msvc` | ✓ | ✓ | 64-bit MSVC (Windows 7+) +`x86_64-unknown-linux-gnu` | ✓ | ✓ | 64-bit Linux (kernel 2.6.32+, glibc 2.11+) + +## Tier 2 + +Tier 2 platforms can be thought of as "guaranteed to build". Automated tests +are not run so it's not guaranteed to produce a working build, but platforms +often work to quite a good degree and patches are always welcome! +Specifically, these platforms are required to have each of the following: + +* Official binary releases are provided for the platform. +* Automated building is set up, but may not be running tests. +* Landing changes to the `rust-lang/rust` repository's master branch is gated on + platforms **building**. For some platforms only the standard library is + compiled, but for others `rustc` and `cargo` are too. + +target | std | host | notes +-------|-----|------|------- +`aarch64-apple-ios` | ✓[^apple] | | ARM64 iOS +`aarch64-fuchsia` | ✓ | | ARM64 Fuchsia +`aarch64-linux-android` | ✓ | | ARM64 Android +`aarch64-pc-windows-msvc` | ✓ | | ARM64 Windows MSVC +`aarch64-unknown-linux-gnu` | ✓ | ✓ | ARM64 Linux (kernel 4.2, glibc 2.17) +`aarch64-unknown-linux-musl` | ✓ | | ARM64 Linux with MUSL +`aarch64-unknown-none` | * | | Bare ARM64, hardfloat +`aarch64-unknown-none-softfloat` | * | | Bare ARM64, softfloat +`arm-linux-androideabi` | ✓ | | ARMv7 Android +`arm-unknown-linux-gnueabi` | ✓ | ✓ | ARMv6 Linux (kernel 3.2, glibc 2.17) +`arm-unknown-linux-gnueabihf` | ✓ | ✓ | ARMv6 Linux, hardfloat (kernel 3.2, glibc 2.17) +`arm-unknown-linux-musleabi` | ✓ | | ARMv6 Linux with MUSL +`arm-unknown-linux-musleabihf` | ✓ | | ARMv6 Linux with MUSL, hardfloat +`armebv7r-none-eabi` | * | | Bare ARMv7-R, Big Endian +`armebv7r-none-eabihf` | * | | Bare ARMv7-R, Big Endian, hardfloat +`armv5te-unknown-linux-gnueabi` | ✓ | | ARMv5TE Linux (kernel 4.4, glibc 2.23) +`armv5te-unknown-linux-musleabi` | ✓ | | ARMv5TE Linux with MUSL +`armv7-linux-androideabi` | ✓ | | ARMv7a Android +`armv7a-none-eabi` | * | | Bare ARMv7-A +`armv7r-none-eabi` | * | | Bare ARMv7-R +`armv7r-none-eabihf` | * | | Bare ARMv7-R, hardfloat +`armv7-unknown-linux-gnueabi` | ✓ | | ARMv7 Linux (kernel 4.15, glibc 2.27) +`armv7-unknown-linux-gnueabihf` | ✓ | ✓ | ARMv7 Linux, hardfloat (kernel 3.2, glibc 2.17) +`armv7-unknown-linux-musleabi` | ✓ | | ARMv7 Linux, MUSL +`armv7-unknown-linux-musleabihf` | ✓ | | ARMv7 Linux with MUSL +`asmjs-unknown-emscripten` | ✓ | | asm.js via Emscripten +`i586-pc-windows-msvc` | ✓ | | 32-bit Windows w/o SSE +`i586-unknown-linux-gnu` | ✓ | | 32-bit Linux w/o SSE (kernel 4.4, glibc 2.23) +`i586-unknown-linux-musl` | ✓ | | 32-bit Linux w/o SSE, MUSL +`i686-linux-android` | ✓ | | 32-bit x86 Android +`i686-unknown-freebsd` | ✓ | ✓ | 32-bit FreeBSD +`i686-unknown-linux-musl` | ✓ | | 32-bit Linux with MUSL +`mips-unknown-linux-gnu` | ✓ | ✓ | MIPS Linux (kernel 4.4, glibc 2.23) +`mips-unknown-linux-musl` | ✓ | | MIPS Linux with MUSL +`mips64-unknown-linux-gnuabi64` | ✓ | ✓ | MIPS64 Linux, n64 ABI (kernel 4.4, glibc 2.23) +`mips64-unknown-linux-muslabi64` | ✓ | | MIPS64 Linux, n64 ABI, MUSL +`mips64el-unknown-linux-gnuabi64` | ✓ | ✓ | MIPS64 (LE) Linux, n64 ABI (kernel 4.4, glibc 2.23) +`mips64el-unknown-linux-muslabi64` | ✓ | | MIPS64 (LE) Linux, n64 ABI, MUSL +`mipsel-unknown-linux-gnu` | ✓ | ✓ | MIPS (LE) Linux (kernel 4.4, glibc 2.23) +`mipsel-unknown-linux-musl` | ✓ | | MIPS (LE) Linux with MUSL +`nvptx64-nvidia-cuda` | ✓ | | --emit=asm generates PTX code that [runs on NVIDIA GPUs] +`powerpc-unknown-linux-gnu` | ✓ | ✓ | PowerPC Linux (kernel 2.6.32, glibc 2.11) +`powerpc64-unknown-linux-gnu` | ✓ | ✓ | PPC64 Linux (kernel 2.6.32, glibc 2.11) +`powerpc64le-unknown-linux-gnu` | ✓ | ✓ | PPC64LE Linux (kernel 3.10, glibc 2.17) +`riscv32i-unknown-none-elf` | * | | Bare RISC-V (RV32I ISA) +`riscv32imac-unknown-none-elf` | * | | Bare RISC-V (RV32IMAC ISA) +`riscv32imc-unknown-none-elf` | * | | Bare RISC-V (RV32IMC ISA) +`riscv64gc-unknown-linux-gnu` | ✓ | ✓ | RISC-V Linux (kernel 4.20, glibc 2.29) +`riscv64gc-unknown-none-elf` | * | | Bare RISC-V (RV64IMAFDC ISA) +`riscv64imac-unknown-none-elf` | * | | Bare RISC-V (RV64IMAC ISA) +`s390x-unknown-linux-gnu` | ✓ | ✓ | S390x Linux (kernel 2.6.32, glibc 2.11) +`sparc64-unknown-linux-gnu` | ✓ | | SPARC Linux (kernel 4.4, glibc 2.23) +`sparcv9-sun-solaris` | ✓ | | SPARC Solaris 10/11, illumos +`thumbv6m-none-eabi` | * | | Bare Cortex-M0, M0+, M1 +`thumbv7em-none-eabi` | * | | Bare Cortex-M4, M7 +`thumbv7em-none-eabihf` | * | | Bare Cortex-M4F, M7F, FPU, hardfloat +`thumbv7m-none-eabi` | * | | Bare Cortex-M3 +`thumbv7neon-linux-androideabi` | ✓ | | Thumb2-mode ARMv7a Android with NEON +`thumbv7neon-unknown-linux-gnueabihf` | ✓ | | Thumb2-mode ARMv7a Linux with NEON (kernel 4.4, glibc 2.23) +`thumbv8m.base-none-eabi` | * | | ARMv8-M Baseline +`thumbv8m.main-none-eabi` | * | | ARMv8-M Mainline +`thumbv8m.main-none-eabihf` | * | | ARMv8-M Baseline, hardfloat +`wasm32-unknown-emscripten` | ✓ | | WebAssembly via Emscripten +`wasm32-unknown-unknown` | ✓ | | WebAssembly +`wasm32-wasi` | ✓ | | WebAssembly with WASI +`x86_64-apple-ios` | ✓[^apple] | | 64-bit x86 iOS +`x86_64-fortanix-unknown-sgx` | ✓ | | [Fortanix ABI] for 64-bit Intel SGX +`x86_64-fuchsia` | ✓ | | 64-bit Fuchsia +`x86_64-linux-android` | ✓ | | 64-bit x86 Android +`x86_64-rumprun-netbsd` | ✓ | | 64-bit NetBSD Rump Kernel +`x86_64-sun-solaris` | ✓ | | 64-bit Solaris 10/11, illumos +`x86_64-unknown-cloudabi` | ✓ | | 64-bit CloudABI +`x86_64-unknown-freebsd` | ✓ | ✓ | 64-bit FreeBSD +`x86_64-unknown-illumos` | ✓ | ✓ | illumos +`x86_64-unknown-linux-gnux32` | ✓ | | 64-bit Linux (x32 ABI) (kernel 4.15, glibc 2.27) +`x86_64-unknown-linux-musl` | ✓ | ✓ | 64-bit Linux with MUSL +`x86_64-unknown-netbsd` | ✓ | ✓ | NetBSD/amd64 +`x86_64-unknown-redox` | ✓ | | Redox OS + +[Fortanix ABI]: https://edp.fortanix.com/ + +## Tier 3 + +Tier 3 platforms are those which the Rust codebase has support for, but which +are not built or tested automatically, and may not work. Official builds are +not available. + +target | std | host | notes +-------|-----|------|------- +`aarch64-apple-darwin` | ? | | ARM64 macOS +`aarch64-apple-tvos` | *[^apple] | | ARM64 tvOS +`aarch64-unknown-cloudabi` | ✓ | | ARM64 CloudABI +`aarch64-unknown-freebsd` | ✓ | ✓ | ARM64 FreeBSD +`aarch64-unknown-hermit` | ? | | +`aarch64-unknown-netbsd` | ? | | +`aarch64-unknown-openbsd` | ✓ | ✓ | ARM64 OpenBSD +`aarch64-unknown-redox` | ? | | ARM64 Redox OS +`aarch64-uwp-windows-msvc` | ? | | +`aarch64-wrs-vxworks` | ? | | +`armv4t-unknown-linux-gnueabi` | ? | | +`armv6-unknown-freebsd` | ✓ | ✓ | ARMv6 FreeBSD +`armv6-unknown-netbsd-eabihf` | ? | | +`armv7-apple-ios` | ✓[^apple] | | ARMv7 iOS, Cortex-a8 +`armv7-unknown-cloudabi-eabihf` | ✓ | | ARMv7 CloudABI, hardfloat +`armv7-unknown-freebsd` | ✓ | ✓ | ARMv7 FreeBSD +`armv7-unknown-netbsd-eabihf` | ? | | +`armv7-wrs-vxworks-eabihf` | ? | | +`armv7a-none-eabihf` | * | | ARM Cortex-A, hardfloat +`armv7s-apple-ios` | ✓[^apple] | | +`avr-unknown-unknown` | ? | | AVR +`hexagon-unknown-linux-musl` | ? | | +`i386-apple-ios` | ✓[^apple] | | 32-bit x86 iOS +`i686-apple-darwin` | ✓ | ✓ | 32-bit OSX (10.7+, Lion+) +`i686-pc-windows-msvc` | ✓ | | 32-bit Windows XP support +`i686-unknown-cloudabi` | ✓ | | 32-bit CloudABI +`i686-unknown-uefi` | ? | | 32-bit UEFI +`i686-unknown-haiku` | ✓ | ✓ | 32-bit Haiku +`i686-unknown-netbsd` | ✓ | | NetBSD/i386 with SSE2 +`i686-unknown-openbsd` | ✓ | ✓ | 32-bit OpenBSD +`i686-uwp-windows-gnu` | ? | | +`i686-uwp-windows-msvc` | ? | | +`i686-wrs-vxworks` | ? | | +`mips-unknown-linux-uclibc` | ✓ | | MIPS Linux with uClibc +`mipsel-unknown-linux-uclibc` | ✓ | | MIPS (LE) Linux with uClibc +`mipsel-sony-psp` | * | | MIPS (LE) Sony PlayStation Portable (PSP) +`mipsisa32r6-unknown-linux-gnu` | ? | | +`mipsisa32r6el-unknown-linux-gnu` | ? | | +`mipsisa64r6-unknown-linux-gnuabi64` | ? | | +`mipsisa64r6el-unknown-linux-gnuabi64` | ? | | +`msp430-none-elf` | * | | 16-bit MSP430 microcontrollers +`powerpc-unknown-linux-gnuspe` | ✓ | | PowerPC SPE Linux +`powerpc-unknown-linux-musl` | ? | | +`powerpc-unknown-netbsd` | ? | | +`powerpc-wrs-vxworks` | ? | | +`powerpc-wrs-vxworks-spe` | ? | | +`powerpc64-unknown-freebsd` | ✓ | ✓ | PPC64 FreeBSD (ELFv1 and ELFv2) +`powerpc64-unknown-linux-musl` | ? | | +`powerpc64-wrs-vxworks` | ? | | +`powerpc64le-unknown-linux-musl` | ? | | +`sparc-unknown-linux-gnu` | ✓ | | 32-bit SPARC Linux +`sparc64-unknown-netbsd` | ✓ | ✓ | NetBSD/sparc64 +`sparc64-unknown-openbsd` | ? | | +`thumbv7a-pc-windows-msvc` | ? | | +`thumbv7a-uwp-windows-msvc` | ✓ | | +`thumbv7neon-unknown-linux-musleabihf` | ? | | Thumb2-mode ARMv7a Linux with NEON, MUSL +`thumbv4t-none-eabi` | * | | ARMv4T T32 +`x86_64-apple-ios-macabi` | ✓[^apple] | | Apple Catalyst +`x86_64-apple-tvos` | *[^apple] | | x86 64-bit tvOS +`x86_64-linux-kernel` | ? | | Linux kernel modules +`x86_64-pc-solaris` | ? | | +`x86_64-pc-windows-msvc` | ✓ | | 64-bit Windows XP support +`x86_64-unknown-dragonfly` | ✓ | ✓ | 64-bit DragonFlyBSD +`x86_64-unknown-haiku` | ✓ | ✓ | 64-bit Haiku +`x86_64-unknown-hermit` | ? | | +`x86_64-unknown-hermit-kernel` | ? | | HermitCore kernel +`x86_64-unknown-l4re-uclibc` | ? | | +`x86_64-unknown-openbsd` | ✓ | ✓ | 64-bit OpenBSD +`x86_64-unknown-uefi` | ? | | +`x86_64-uwp-windows-gnu` | ✓ | | +`x86_64-uwp-windows-msvc` | ✓ | | +`x86_64-wrs-vxworks` | ? | | + +[runs on NVIDIA GPUs]: https://github.com/japaric-archived/nvptx#targets +[^apple]: These targets are only available on macOS. diff --git a/src/doc/rustc/src/targets/built-in.md b/src/doc/rustc/src/targets/built-in.md index 2e94ebe345adb..c33b506cdaefb 100644 --- a/src/doc/rustc/src/targets/built-in.md +++ b/src/doc/rustc/src/targets/built-in.md @@ -2,9 +2,14 @@ `rustc` ships with the ability to compile to many targets automatically, we call these "built-in" targets, and they generally correspond to targets that -the team is supporting directly. +the team is supporting directly. To see the list of built-in targets, you can +run `rustc --print target-list`. -To see the list of built-in targets, you can run `rustc --print target-list`, -or look at [the API -docs](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_target/spec/index.html#modules). -Each module there defines a builder for a particular target. \ No newline at end of file +Typically, a target needs a compiled copy of the Rust standard library to +work. If using [rustup], then check out the documentation on +[Cross-compilation][rustup-cross] on how to download a pre-built standard +library built by the official Rust distributions. Most targets will need a +system linker, and possibly other things. + +[rustup]: https://github.com/rust-lang/rustup +[rustup-cross]: https://github.com/rust-lang/rustup#cross-compilation diff --git a/src/librustc_codegen_llvm/consts.rs b/src/librustc_codegen_llvm/consts.rs index 024834bfe2aa5..0c749657001a6 100644 --- a/src/librustc_codegen_llvm/consts.rs +++ b/src/librustc_codegen_llvm/consts.rs @@ -215,7 +215,7 @@ impl CodegenCx<'ll, 'tcx> { debug!("get_static: sym={} instance={:?}", sym, instance); let g = if let Some(def_id) = def_id.as_local() { - let id = self.tcx.hir().as_local_hir_id(def_id); + let id = self.tcx.hir().local_def_id_to_hir_id(def_id); let llty = self.layout_of(ty).llvm_type(self); // FIXME: refactor this to work without accessing the HIR let (g, attrs) = match self.tcx.hir().get(id) { diff --git a/src/librustc_codegen_ssa/back/symbol_export.rs b/src/librustc_codegen_ssa/back/symbol_export.rs index 7d742e7a7afd2..a9ffd416485ca 100644 --- a/src/librustc_codegen_ssa/back/symbol_export.rs +++ b/src/librustc_codegen_ssa/back/symbol_export.rs @@ -370,7 +370,7 @@ fn upstream_drop_glue_for_provider<'tcx>( fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: DefId) -> bool { if let Some(def_id) = def_id.as_local() { - !tcx.reachable_set(LOCAL_CRATE).contains(&tcx.hir().as_local_hir_id(def_id)) + !tcx.reachable_set(LOCAL_CRATE).contains(&tcx.hir().local_def_id_to_hir_id(def_id)) } else { bug!("is_unreachable_local_definition called with non-local DefId: {:?}", def_id) } diff --git a/src/librustc_hir/definitions.rs b/src/librustc_hir/definitions.rs index 79b7068273932..caa86b54fdfd8 100644 --- a/src/librustc_hir/definitions.rs +++ b/src/librustc_hir/definitions.rs @@ -306,11 +306,6 @@ impl Definitions { }) } - #[inline] - pub fn as_local_hir_id(&self, def_id: LocalDefId) -> hir::HirId { - self.local_def_id_to_hir_id(def_id) - } - #[inline] pub fn local_def_id_to_hir_id(&self, id: LocalDefId) -> hir::HirId { self.def_id_to_hir_id[id].unwrap() diff --git a/src/librustc_hir/lang_items.rs b/src/librustc_hir/lang_items.rs index 7f473a458481f..2f7edeb405ffe 100644 --- a/src/librustc_hir/lang_items.rs +++ b/src/librustc_hir/lang_items.rs @@ -165,6 +165,7 @@ language_item_table! { BoolImplItem, sym::bool, bool_impl, Target::Impl; CharImplItem, sym::char, char_impl, Target::Impl; StrImplItem, sym::str, str_impl, Target::Impl; + ArrayImplItem, sym::array, array_impl, Target::Impl; SliceImplItem, sym::slice, slice_impl, Target::Impl; SliceU8ImplItem, sym::slice_u8, slice_u8_impl, Target::Impl; StrAllocImplItem, sym::str_alloc, str_alloc_impl, Target::Impl; diff --git a/src/librustc_infer/infer/error_reporting/mod.rs b/src/librustc_infer/infer/error_reporting/mod.rs index 063246f79fe36..2b2c42207e48b 100644 --- a/src/librustc_infer/infer/error_reporting/mod.rs +++ b/src/librustc_infer/infer/error_reporting/mod.rs @@ -144,7 +144,7 @@ fn msg_span_from_early_bound_and_free_regions( let sm = tcx.sess.source_map(); let scope = region.free_region_binding_scope(tcx); - let node = tcx.hir().as_local_hir_id(scope.expect_local()); + let node = tcx.hir().local_def_id_to_hir_id(scope.expect_local()); let tag = match tcx.hir().find(node) { Some(Node::Block(_) | Node::Expr(_)) => "body", Some(Node::Item(it)) => item_scope_tag(&it), @@ -1707,7 +1707,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { .in_progress_typeck_results .map(|typeck_results| typeck_results.borrow().hir_owner) .map(|owner| { - let hir_id = hir.as_local_hir_id(owner); + let hir_id = hir.local_def_id_to_hir_id(owner); let parent_id = hir.get_parent_item(hir_id); ( // Parent item could be a `mod`, so we check the HIR before calling: @@ -1733,7 +1733,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { // Get the `hir::Param` to verify whether it already has any bounds. // We do this to avoid suggesting code that ends up as `T: 'a'b`, // instead we suggest `T: 'a + 'b` in that case. - let id = hir.as_local_hir_id(def_id); + let id = hir.local_def_id_to_hir_id(def_id); let mut has_bounds = false; if let Node::GenericParam(param) = hir.get(id) { has_bounds = !param.bounds.is_empty(); @@ -1786,7 +1786,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { .and_then(|(_, g)| g.params.first()) .and_then(|param| param.def_id.as_local()) .map(|def_id| { - (hir.span(hir.as_local_hir_id(def_id)).shrink_to_lo(), format!("{}, ", new_lt)) + ( + hir.span(hir.local_def_id_to_hir_id(def_id)).shrink_to_lo(), + format!("{}, ", new_lt), + ) }); let labeled_user_string = match bound_kind { diff --git a/src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs b/src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs index 20617bb8bd8fc..eb1521f056576 100644 --- a/src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs +++ b/src/librustc_infer/infer/error_reporting/nice_region_error/find_anon_type.rs @@ -28,7 +28,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { br: &ty::BoundRegion, ) -> Option<(&hir::Ty<'tcx>, &hir::FnDecl<'tcx>)> { if let Some(anon_reg) = self.tcx().is_suitable_region(region) { - let hir_id = self.tcx().hir().as_local_hir_id(anon_reg.def_id); + let hir_id = self.tcx().hir().local_def_id_to_hir_id(anon_reg.def_id); let fndecl = match self.tcx().hir().get(hir_id) { Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref m, ..), .. }) | Node::TraitItem(&hir::TraitItem { diff --git a/src/librustc_infer/infer/error_reporting/nice_region_error/trait_impl_difference.rs b/src/librustc_infer/infer/error_reporting/nice_region_error/trait_impl_difference.rs index 1ddf88c030660..788eabf296d76 100644 --- a/src/librustc_infer/infer/error_reporting/nice_region_error/trait_impl_difference.rs +++ b/src/librustc_infer/infer/error_reporting/nice_region_error/trait_impl_difference.rs @@ -67,7 +67,8 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { match assoc_item.kind { ty::AssocKind::Fn => { let hir = self.tcx().hir(); - if let Some(hir_id) = assoc_item.def_id.as_local().map(|id| hir.as_local_hir_id(id)) + if let Some(hir_id) = + assoc_item.def_id.as_local().map(|id| hir.local_def_id_to_hir_id(id)) { if let Some(decl) = hir.fn_decl_by_hir_id(hir_id) { visitor.visit_fn_decl(decl); diff --git a/src/librustc_infer/infer/error_reporting/nice_region_error/util.rs b/src/librustc_infer/infer/error_reporting/nice_region_error/util.rs index 28e9dd90cfd67..6e2d49f1ad792 100644 --- a/src/librustc_infer/infer/error_reporting/nice_region_error/util.rs +++ b/src/librustc_infer/infer/error_reporting/nice_region_error/util.rs @@ -49,7 +49,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { }; let hir = &self.tcx().hir(); - let hir_id = hir.as_local_hir_id(id.as_local()?); + let hir_id = hir.local_def_id_to_hir_id(id.as_local()?); let body_id = hir.maybe_body_owned_by(hir_id)?; let body = hir.body(body_id); let owner_id = hir.body_owner(body_id); diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index c42794e00b41e..ac3a9937dbad8 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -440,7 +440,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc { // reported for missing docs. let real_trait = trait_ref.path.res.def_id(); if let Some(def_id) = real_trait.as_local() { - let hir_id = cx.tcx.hir().as_local_hir_id(def_id); + let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id); if let Some(Node::Item(item)) = cx.tcx.hir().find(hir_id) { if let hir::VisibilityKind::Inherited = item.vis.node { for impl_item_ref in items { @@ -614,7 +614,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDebugImplementations { cx.tcx.for_each_impl(debug, |d| { if let Some(ty_def) = cx.tcx.type_of(d).ty_adt_def() { if let Some(def_id) = ty_def.did.as_local() { - impls.insert(cx.tcx.hir().as_local_hir_id(def_id)); + impls.insert(cx.tcx.hir().local_def_id_to_hir_id(def_id)); } } }); diff --git a/src/librustc_lint/late.rs b/src/librustc_lint/late.rs index f43c197d2d201..4a612b89cac22 100644 --- a/src/librustc_lint/late.rs +++ b/src/librustc_lint/late.rs @@ -379,7 +379,7 @@ fn late_lint_mod_pass<'tcx, T: LateLintPass<'tcx>>( param_env: ty::ParamEnv::empty(), access_levels, lint_store: unerased_lint_store(tcx), - last_node_with_lint_attrs: tcx.hir().as_local_hir_id(module_def_id), + last_node_with_lint_attrs: tcx.hir().local_def_id_to_hir_id(module_def_id), generics: None, only_module: true, }; diff --git a/src/librustc_metadata/rmeta/encoder.rs b/src/librustc_metadata/rmeta/encoder.rs index 6723e236a1fe9..76e395f2f1e2c 100644 --- a/src/librustc_metadata/rmeta/encoder.rs +++ b/src/librustc_metadata/rmeta/encoder.rs @@ -733,7 +733,7 @@ impl EncodeContext<'a, 'tcx> { is_non_exhaustive: variant.is_field_list_non_exhaustive(), }; - let enum_id = tcx.hir().as_local_hir_id(def.did.expect_local()); + let enum_id = tcx.hir().local_def_id_to_hir_id(def.did.expect_local()); let enum_vis = &tcx.hir().expect_item(enum_id).vis; record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data))); @@ -780,7 +780,7 @@ impl EncodeContext<'a, 'tcx> { // Variant constructors have the same visibility as the parent enums, unless marked as // non-exhaustive, in which case they are lowered to `pub(crate)`. - let enum_id = tcx.hir().as_local_hir_id(def.did.expect_local()); + let enum_id = tcx.hir().local_def_id_to_hir_id(def.did.expect_local()); let enum_vis = &tcx.hir().expect_item(enum_id).vis; let mut ctor_vis = ty::Visibility::from_hir(enum_vis, enum_id, tcx); if variant.is_field_list_non_exhaustive() && ctor_vis == ty::Visibility::Public { @@ -819,11 +819,11 @@ impl EncodeContext<'a, 'tcx> { let data = ModData { reexports: match tcx.module_exports(local_def_id) { Some(exports) => { - let hir_map = self.tcx.hir(); + let hir = self.tcx.hir(); self.lazy( exports .iter() - .map(|export| export.map_id(|id| hir_map.as_local_hir_id(id))), + .map(|export| export.map_id(|id| hir.local_def_id_to_hir_id(id))), ) } _ => Lazy::empty(), @@ -855,7 +855,7 @@ impl EncodeContext<'a, 'tcx> { let def_id = field.did; debug!("EncodeContext::encode_field({:?})", def_id); - let variant_id = tcx.hir().as_local_hir_id(variant.def_id.expect_local()); + let variant_id = tcx.hir().local_def_id_to_hir_id(variant.def_id.expect_local()); let variant_data = tcx.hir().expect_variant_data(variant_id); record!(self.tables.kind[def_id] <- EntryKind::Field); @@ -883,7 +883,7 @@ impl EncodeContext<'a, 'tcx> { is_non_exhaustive: variant.is_field_list_non_exhaustive(), }; - let struct_id = tcx.hir().as_local_hir_id(adt_def.did.expect_local()); + let struct_id = tcx.hir().local_def_id_to_hir_id(adt_def.did.expect_local()); let struct_vis = &tcx.hir().expect_item(struct_id).vis; let mut ctor_vis = ty::Visibility::from_hir(struct_vis, struct_id, tcx); for field in &variant.fields { @@ -945,7 +945,7 @@ impl EncodeContext<'a, 'tcx> { debug!("EncodeContext::encode_info_for_trait_item({:?})", def_id); let tcx = self.tcx; - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let ast_item = tcx.hir().expect_trait_item(hir_id); let trait_item = tcx.associated_item(def_id); @@ -1034,7 +1034,7 @@ impl EncodeContext<'a, 'tcx> { debug!("EncodeContext::encode_info_for_impl_item({:?})", def_id); let tcx = self.tcx; - let hir_id = self.tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let ast_item = self.tcx.hir().expect_impl_item(hir_id); let impl_item = self.tcx.associated_item(def_id); @@ -1438,7 +1438,7 @@ impl EncodeContext<'a, 'tcx> { // NOTE(eddyb) `tcx.type_of(def_id)` isn't used because it's fully generic, // including on the signature, which is inferred in `typeck. - let hir_id = self.tcx.hir().as_local_hir_id(def_id); + let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); let ty = self.tcx.typeck(def_id).node_type(hir_id); record!(self.tables.kind[def_id.to_def_id()] <- match ty.kind { @@ -1465,7 +1465,7 @@ impl EncodeContext<'a, 'tcx> { fn encode_info_for_anon_const(&mut self, def_id: LocalDefId) { debug!("EncodeContext::encode_info_for_anon_const({:?})", def_id); - let id = self.tcx.hir().as_local_hir_id(def_id); + let id = self.tcx.hir().local_def_id_to_hir_id(def_id); let body_id = self.tcx.hir().body_owned_by(id); let const_data = self.encode_rendered_const_for_body(body_id); let qualifs = self.tcx.mir_const_qualif(def_id); diff --git a/src/librustc_middle/dep_graph/mod.rs b/src/librustc_middle/dep_graph/mod.rs index 682b335c5d071..6697524279874 100644 --- a/src/librustc_middle/dep_graph/mod.rs +++ b/src/librustc_middle/dep_graph/mod.rs @@ -185,6 +185,6 @@ impl<'tcx> DepContext for TyCtxt<'tcx> { } fn def_id_corresponds_to_hir_dep_node(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { - let hir_id = tcx.hir().as_local_hir_id(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); def_id == hir_id.owner } diff --git a/src/librustc_middle/hir/map/mod.rs b/src/librustc_middle/hir/map/mod.rs index 250f4d5187f2f..0794caca1baa5 100644 --- a/src/librustc_middle/hir/map/mod.rs +++ b/src/librustc_middle/hir/map/mod.rs @@ -173,11 +173,6 @@ impl<'hir> Map<'hir> { self.tcx.definitions.opt_hir_id_to_local_def_id(hir_id) } - #[inline] - pub fn as_local_hir_id(&self, def_id: LocalDefId) -> HirId { - self.tcx.definitions.as_local_hir_id(def_id) - } - #[inline] pub fn local_def_id_to_hir_id(&self, def_id: LocalDefId) -> HirId { self.tcx.definitions.local_def_id_to_hir_id(def_id) @@ -450,7 +445,7 @@ impl<'hir> Map<'hir> { } pub fn get_module(&self, module: LocalDefId) -> (&'hir Mod<'hir>, Span, HirId) { - let hir_id = self.as_local_hir_id(module); + let hir_id = self.local_def_id_to_hir_id(module); match self.get_entry(hir_id).node { Node::Item(&Item { span, kind: ItemKind::Mod(ref m), .. }) => (m, span, hir_id), Node::Crate(item) => (&item.module, item.span, hir_id), @@ -483,7 +478,7 @@ impl<'hir> Map<'hir> { } pub fn get_if_local(&self, id: DefId) -> Option<Node<'hir>> { - id.as_local().map(|id| self.get(self.as_local_hir_id(id))) + id.as_local().map(|id| self.get(self.local_def_id_to_hir_id(id))) } pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics<'hir>> { @@ -872,7 +867,7 @@ impl<'hir> Map<'hir> { } pub fn span_if_local(&self, id: DefId) -> Option<Span> { - id.as_local().map(|id| self.span(self.as_local_hir_id(id))) + id.as_local().map(|id| self.span(self.local_def_id_to_hir_id(id))) } pub fn res_span(&self, res: Res) -> Option<Span> { diff --git a/src/librustc_middle/hir/mod.rs b/src/librustc_middle/hir/mod.rs index b014f3c8eb794..ae3b30217cc4a 100644 --- a/src/librustc_middle/hir/mod.rs +++ b/src/librustc_middle/hir/mod.rs @@ -66,20 +66,20 @@ impl<'tcx> TyCtxt<'tcx> { pub fn provide(providers: &mut Providers) { providers.parent_module_from_def_id = |tcx, id| { let hir = tcx.hir(); - hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id))) + hir.local_def_id(hir.get_module_parent_node(hir.local_def_id_to_hir_id(id))) }; providers.hir_crate = |tcx, _| tcx.untracked_crate; providers.index_hir = map::index_hir; providers.hir_module_items = |tcx, id| { let hir = tcx.hir(); - let module = hir.as_local_hir_id(id); + let module = hir.local_def_id_to_hir_id(id); &tcx.untracked_crate.modules[&module] }; providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature; providers.hir_owner_nodes = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_deref(); providers.fn_arg_names = |tcx, id| { let hir = tcx.hir(); - let hir_id = hir.as_local_hir_id(id.expect_local()); + let hir_id = hir.local_def_id_to_hir_id(id.expect_local()); if let Some(body_id) = hir.maybe_body_owned_by(hir_id) { tcx.arena.alloc_from_iter(hir.body_param_names(body_id)) } else if let Node::TraitItem(&TraitItem { diff --git a/src/librustc_middle/mir/mod.rs b/src/librustc_middle/mir/mod.rs index 3b0c480f6d400..0a253e777dd9b 100644 --- a/src/librustc_middle/mir/mod.rs +++ b/src/librustc_middle/mir/mod.rs @@ -2127,7 +2127,7 @@ impl<'tcx> Debug for Rvalue<'tcx> { AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| { if let Some(def_id) = def_id.as_local() { - let hir_id = tcx.hir().as_local_hir_id(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let name = if tcx.sess.opts.debugging_opts.span_free_formats { let substs = tcx.lift(&substs).unwrap(); format!( @@ -2155,7 +2155,7 @@ impl<'tcx> Debug for Rvalue<'tcx> { AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| { if let Some(def_id) = def_id.as_local() { - let hir_id = tcx.hir().as_local_hir_id(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let name = format!("[generator@{:?}]", tcx.hir().span(hir_id)); let mut struct_fmt = fmt.debug_struct(&name); diff --git a/src/librustc_middle/mir/mono.rs b/src/librustc_middle/mir/mono.rs index bb204223b6060..d240ae65b6dff 100644 --- a/src/librustc_middle/mir/mono.rs +++ b/src/librustc_middle/mir/mono.rs @@ -198,10 +198,10 @@ impl<'tcx> MonoItem<'tcx> { pub fn local_span(&self, tcx: TyCtxt<'tcx>) -> Option<Span> { match *self { MonoItem::Fn(Instance { def, .. }) => { - def.def_id().as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) + def.def_id().as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)) } MonoItem::Static(def_id) => { - def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) + def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)) } MonoItem::GlobalAsm(hir_id) => Some(hir_id), } @@ -346,9 +346,10 @@ impl<'tcx> CodegenUnit<'tcx> { // instances into account. The others don't matter for // the codegen tests and can even make item order // unstable. - InstanceDef::Item(def) => { - def.did.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) - } + InstanceDef::Item(def) => def + .did + .as_local() + .map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)), InstanceDef::VtableShim(..) | InstanceDef::ReifyShim(..) | InstanceDef::Intrinsic(..) @@ -360,7 +361,7 @@ impl<'tcx> CodegenUnit<'tcx> { } } MonoItem::Static(def_id) => { - def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) + def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)) } MonoItem::GlobalAsm(hir_id) => Some(hir_id), }, diff --git a/src/librustc_middle/query/mod.rs b/src/librustc_middle/query/mod.rs index a8f6723a35605..d364a46463821 100644 --- a/src/librustc_middle/query/mod.rs +++ b/src/librustc_middle/query/mod.rs @@ -352,7 +352,7 @@ rustc_queries! { /// per-type-parameter predicates for resolving `T::AssocTy`. query type_param_predicates(key: (DefId, LocalDefId)) -> ty::GenericPredicates<'tcx> { desc { |tcx| "computing the bounds for type parameter `{}`", { - let id = tcx.hir().as_local_hir_id(key.1); + let id = tcx.hir().local_def_id_to_hir_id(key.1); tcx.hir().ty_param_name(id) }} } diff --git a/src/librustc_middle/ty/consts.rs b/src/librustc_middle/ty/consts.rs index c0b5693dc594e..2e10c79598450 100644 --- a/src/librustc_middle/ty/consts.rs +++ b/src/librustc_middle/ty/consts.rs @@ -88,7 +88,7 @@ impl<'tcx> Const<'tcx> { ExprKind::Path(QPath::Resolved(_, &Path { res: Res::Def(ConstParam, def_id), .. })) => { // Find the name and index of the const parameter by indexing the generics of // the parent item and construct a `ParamConst`. - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let item_id = tcx.hir().get_parent_node(hir_id); let item_def_id = tcx.hir().local_def_id(item_id); let generics = tcx.generics_of(item_def_id.to_def_id()); diff --git a/src/librustc_middle/ty/context.rs b/src/librustc_middle/ty/context.rs index d6bcfbf49cff1..4f990ece4d796 100644 --- a/src/librustc_middle/ty/context.rs +++ b/src/librustc_middle/ty/context.rs @@ -1420,7 +1420,7 @@ impl<'tcx> TyCtxt<'tcx> { _ => return None, // not a free region }; - let hir_id = self.hir().as_local_hir_id(suitable_region_binding_scope); + let hir_id = self.hir().local_def_id_to_hir_id(suitable_region_binding_scope); let is_impl_item = match self.hir().find(hir_id) { Some(Node::Item(..) | Node::TraitItem(..)) => false, Some(Node::ImplItem(..)) => { @@ -1441,7 +1441,7 @@ impl<'tcx> TyCtxt<'tcx> { &self, scope_def_id: LocalDefId, ) -> Vec<&'tcx hir::Ty<'tcx>> { - let hir_id = self.hir().as_local_hir_id(scope_def_id); + let hir_id = self.hir().local_def_id_to_hir_id(scope_def_id); let hir_output = match self.hir().get(hir_id) { Node::Item(hir::Item { kind: @@ -1486,7 +1486,7 @@ impl<'tcx> TyCtxt<'tcx> { pub fn return_type_impl_trait(&self, scope_def_id: LocalDefId) -> Option<(Ty<'tcx>, Span)> { // HACK: `type_of_def_id()` will fail on these (#55796), so return `None`. - let hir_id = self.hir().as_local_hir_id(scope_def_id); + let hir_id = self.hir().local_def_id_to_hir_id(scope_def_id); match self.hir().get(hir_id) { Node::Item(item) => { match item.kind { diff --git a/src/librustc_middle/ty/error.rs b/src/librustc_middle/ty/error.rs index 30ff5a2d9ebdf..438da832226d7 100644 --- a/src/librustc_middle/ty/error.rs +++ b/src/librustc_middle/ty/error.rs @@ -403,7 +403,7 @@ impl<'tcx> TyCtxt<'tcx> { .type_param(p, self) .def_id .as_local() - .map(|id| hir.as_local_hir_id(id)) + .map(|id| hir.local_def_id_to_hir_id(id)) .and_then(|id| self.hir().find(self.hir().get_parent_node(id))) .as_ref() .and_then(|node| node.generics()) @@ -791,10 +791,11 @@ fn foo(&self) -> Self::T { String::new() } body_owner_def_id: DefId, found: Ty<'tcx>, ) -> bool { - let hir_id = match body_owner_def_id.as_local().map(|id| self.hir().as_local_hir_id(id)) { - Some(hir_id) => hir_id, - None => return false, - }; + let hir_id = + match body_owner_def_id.as_local().map(|id| self.hir().local_def_id_to_hir_id(id)) { + Some(hir_id) => hir_id, + None => return false, + }; // When `body_owner` is an `impl` or `trait` item, look in its associated types for // `expected` and point at it. let parent_id = self.hir().get_parent_item(hir_id); diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs index 6798addb8aaa3..85c4cd91ebdac 100644 --- a/src/librustc_middle/ty/mod.rs +++ b/src/librustc_middle/ty/mod.rs @@ -2845,12 +2845,12 @@ impl<'tcx> TyCtxt<'tcx> { pub fn opt_item_name(self, def_id: DefId) -> Option<Ident> { def_id .as_local() - .and_then(|def_id| self.hir().get(self.hir().as_local_hir_id(def_id)).ident()) + .and_then(|def_id| self.hir().get(self.hir().local_def_id_to_hir_id(def_id)).ident()) } pub fn opt_associated_item(self, def_id: DefId) -> Option<&'tcx AssocItem> { let is_associated_item = if let Some(def_id) = def_id.as_local() { - match self.hir().get(self.hir().as_local_hir_id(def_id)) { + match self.hir().get(self.hir().local_def_id_to_hir_id(def_id)) { Node::TraitItem(_) | Node::ImplItem(_) => true, _ => false, } @@ -3009,7 +3009,7 @@ impl<'tcx> TyCtxt<'tcx> { /// Gets the attributes of a definition. pub fn get_attrs(self, did: DefId) -> Attributes<'tcx> { if let Some(did) = did.as_local() { - self.hir().attrs(self.hir().as_local_hir_id(did)) + self.hir().attrs(self.hir().local_def_id_to_hir_id(did)) } else { self.item_attrs(did) } @@ -3048,7 +3048,7 @@ impl<'tcx> TyCtxt<'tcx> { /// with the name of the crate containing the impl. pub fn span_of_impl(self, impl_did: DefId) -> Result<Span, Symbol> { if let Some(impl_did) = impl_did.as_local() { - let hir_id = self.hir().as_local_hir_id(impl_did); + let hir_id = self.hir().local_def_id_to_hir_id(impl_did); Ok(self.hir().span(hir_id)) } else { Err(self.crate_name(impl_did.krate)) @@ -3109,7 +3109,7 @@ pub struct AdtSizedConstraint<'tcx>(pub &'tcx [Ty<'tcx>]); /// Yields the parent function's `DefId` if `def_id` is an `impl Trait` definition. pub fn is_impl_trait_defn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> { if let Some(def_id) = def_id.as_local() { - if let Node::Item(item) = tcx.hir().get(tcx.hir().as_local_hir_id(def_id)) { + if let Node::Item(item) = tcx.hir().get(tcx.hir().local_def_id_to_hir_id(def_id)) { if let hir::ItemKind::OpaqueTy(ref opaque_ty) = item.kind { return opaque_ty.impl_trait_fn; } diff --git a/src/librustc_middle/ty/print/pretty.rs b/src/librustc_middle/ty/print/pretty.rs index 87944db60de66..fc444d4cc5b52 100644 --- a/src/librustc_middle/ty/print/pretty.rs +++ b/src/librustc_middle/ty/print/pretty.rs @@ -610,7 +610,7 @@ pub trait PrettyPrinter<'tcx>: // FIXME(eddyb) should use `def_span`. if let Some(did) = did.as_local() { - let hir_id = self.tcx().hir().as_local_hir_id(did); + let hir_id = self.tcx().hir().local_def_id_to_hir_id(did); let span = self.tcx().hir().span(hir_id); p!(write("@{}", self.tcx().sess.source_map().span_to_string(span))); @@ -656,7 +656,7 @@ pub trait PrettyPrinter<'tcx>: // FIXME(eddyb) should use `def_span`. if let Some(did) = did.as_local() { - let hir_id = self.tcx().hir().as_local_hir_id(did); + let hir_id = self.tcx().hir().local_def_id_to_hir_id(did); if self.tcx().sess.opts.debugging_opts.span_free_formats { p!(write("@"), print_def_path(did.to_def_id(), substs)); } else { diff --git a/src/librustc_middle/ty/util.rs b/src/librustc_middle/ty/util.rs index 07221082048fb..3e5eaca5417f7 100644 --- a/src/librustc_middle/ty/util.rs +++ b/src/librustc_middle/ty/util.rs @@ -879,7 +879,7 @@ impl<'tcx> ty::TyS<'tcx> { let span = match field .did .as_local() - .map(|id| tcx.hir().as_local_hir_id(id)) + .map(|id| tcx.hir().local_def_id_to_hir_id(id)) .and_then(|id| tcx.hir().find(id)) { Some(hir::Node::Field(field)) => field.ty.span, diff --git a/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs b/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs index 8e7c97c4a1bac..7a50bdfeef625 100644 --- a/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs +++ b/src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs @@ -263,7 +263,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { let needs_note = match ty.kind { ty::Closure(id, _) => { let tables = self.infcx.tcx.typeck(id.expect_local()); - let hir_id = self.infcx.tcx.hir().as_local_hir_id(id.expect_local()); + let hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(id.expect_local()); tables.closure_kind_origins().get(hir_id).is_none() } @@ -954,7 +954,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { format!("`{}` would have to be valid for `{}`...", name, region_name), ); - let fn_hir_id = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id); + let fn_hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(self.mir_def_id); err.span_label( drop_span, format!( @@ -1863,7 +1863,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { ) -> Option<AnnotatedBorrowFnSignature<'tcx>> { debug!("annotate_fn_sig: did={:?} sig={:?}", did, sig); let is_closure = self.infcx.tcx.is_closure(did); - let fn_hir_id = self.infcx.tcx.hir().as_local_hir_id(did.as_local()?); + let fn_hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(did.as_local()?); let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(fn_hir_id)?; // We need to work out which arguments to highlight. We do this by looking diff --git a/src/librustc_mir/borrow_check/diagnostics/mod.rs b/src/librustc_mir/borrow_check/diagnostics/mod.rs index ba74ffaa8d620..daffdec2a83be 100644 --- a/src/librustc_mir/borrow_check/diagnostics/mod.rs +++ b/src/librustc_mir/borrow_check/diagnostics/mod.rs @@ -104,7 +104,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { debug!("add_moved_or_invoked_closure_note: closure={:?}", closure); if let ty::Closure(did, _) = self.body.local_decls[closure].ty.kind { let did = did.expect_local(); - let hir_id = self.infcx.tcx.hir().as_local_hir_id(did); + let hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(did); if let Some((span, name)) = self.infcx.tcx.typeck(did).closure_kind_origins().get(hir_id) @@ -127,7 +127,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { if let Some(target) = target { if let ty::Closure(did, _) = self.body.local_decls[target].ty.kind { let did = did.expect_local(); - let hir_id = self.infcx.tcx.hir().as_local_hir_id(did); + let hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(did); if let Some((span, name)) = self.infcx.tcx.typeck(did).closure_kind_origins().get(hir_id) @@ -937,7 +937,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { "closure_span: def_id={:?} target_place={:?} places={:?}", def_id, target_place, places ); - let hir_id = self.infcx.tcx.hir().as_local_hir_id(def_id.as_local()?); + let hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(def_id.as_local()?); let expr = &self.infcx.tcx.hir().expect_expr(hir_id).kind; debug!("closure_span: hir_id={:?} expr={:?}", hir_id, expr); if let hir::ExprKind::Closure(.., body_id, args_span, _) = expr { diff --git a/src/librustc_mir/borrow_check/diagnostics/mutability_errors.rs b/src/librustc_mir/borrow_check/diagnostics/mutability_errors.rs index ef0fe71abecb2..d26436ff1de54 100644 --- a/src/librustc_mir/borrow_check/diagnostics/mutability_errors.rs +++ b/src/librustc_mir/borrow_check/diagnostics/mutability_errors.rs @@ -492,7 +492,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { err.span_label(sp, format!("cannot {}", act)); let hir = self.infcx.tcx.hir(); - let closure_id = hir.as_local_hir_id(self.mir_def_id); + let closure_id = hir.local_def_id_to_hir_id(self.mir_def_id); let fn_call_id = hir.get_parent_node(closure_id); let node = hir.get(fn_call_id); let item_id = hir.enclosing_body_owner(fn_call_id); @@ -691,7 +691,7 @@ fn annotate_struct_field( if let ty::Adt(def, _) = ty.kind { let field = def.all_fields().nth(field.index())?; // Use the HIR types to construct the diagnostic message. - let hir_id = tcx.hir().as_local_hir_id(field.did.as_local()?); + let hir_id = tcx.hir().local_def_id_to_hir_id(field.did.as_local()?); let node = tcx.hir().find(hir_id)?; // Now we're dealing with the actual struct that we're going to suggest a change to, // we can expect a field that is an immutable reference to a type. diff --git a/src/librustc_mir/borrow_check/diagnostics/region_name.rs b/src/librustc_mir/borrow_check/diagnostics/region_name.rs index 32195adc60ef8..2603b1e048d81 100644 --- a/src/librustc_mir/borrow_check/diagnostics/region_name.rs +++ b/src/librustc_mir/borrow_check/diagnostics/region_name.rs @@ -266,7 +266,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { } ty::BoundRegion::BrEnv => { - let mir_hir_id = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id); + let mir_hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(self.mir_def_id); let def_ty = self.regioncx.universal_regions().defining_ty; if let DefiningTy::Closure(_, substs) = def_ty { @@ -361,7 +361,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { &self, argument_index: usize, ) -> Option<&hir::Ty<'tcx>> { - let mir_hir_id = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id); + let mir_hir_id = self.infcx.tcx.hir().local_def_id_to_hir_id(self.mir_def_id); let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(mir_hir_id)?; let argument_hir_ty: &hir::Ty<'_> = fn_decl.inputs.get(argument_index)?; match argument_hir_ty.kind { @@ -648,7 +648,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { highlight.highlighting_region_vid(fr, *self.next_region_name.try_borrow().unwrap()); let type_name = self.infcx.extract_type_name(&return_ty, Some(highlight)).0; - let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id); + let mir_hir_id = tcx.hir().local_def_id_to_hir_id(self.mir_def_id); let (return_span, mir_description) = match tcx.hir().get(mir_hir_id) { hir::Node::Expr(hir::Expr { @@ -700,7 +700,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { highlight.highlighting_region_vid(fr, *self.next_region_name.try_borrow().unwrap()); let type_name = self.infcx.extract_type_name(&yield_ty, Some(highlight)).0; - let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id); + let mir_hir_id = tcx.hir().local_def_id_to_hir_id(self.mir_def_id); let yield_span = match tcx.hir().get(mir_hir_id) { hir::Node::Expr(hir::Expr { diff --git a/src/librustc_mir/borrow_check/mod.rs b/src/librustc_mir/borrow_check/mod.rs index 6e211b42a052e..f7031b2a59848 100644 --- a/src/librustc_mir/borrow_check/mod.rs +++ b/src/librustc_mir/borrow_check/mod.rs @@ -129,7 +129,7 @@ fn do_mir_borrowck<'a, 'tcx>( let tcx = infcx.tcx; let param_env = tcx.param_env(def.did); - let id = tcx.hir().as_local_hir_id(def.did); + let id = tcx.hir().local_def_id_to_hir_id(def.did); let mut local_names = IndexVec::from_elem(None, &input_body.local_decls); for var_debug_info in &input_body.var_debug_info { diff --git a/src/librustc_mir/borrow_check/universal_regions.rs b/src/librustc_mir/borrow_check/universal_regions.rs index c5aa5c5ebc7f2..cd6b75cf55602 100644 --- a/src/librustc_mir/borrow_check/universal_regions.rs +++ b/src/librustc_mir/borrow_check/universal_regions.rs @@ -231,7 +231,7 @@ impl<'tcx> UniversalRegions<'tcx> { param_env: ty::ParamEnv<'tcx>, ) -> Self { let tcx = infcx.tcx; - let mir_hir_id = tcx.hir().as_local_hir_id(mir_def.did); + let mir_hir_id = tcx.hir().local_def_id_to_hir_id(mir_def.did); UniversalRegionsBuilder { infcx, mir_def, mir_hir_id, param_env }.build() } diff --git a/src/librustc_mir/const_eval/eval_queries.rs b/src/librustc_mir/const_eval/eval_queries.rs index 7fbe5c409d3ce..21915767057d0 100644 --- a/src/librustc_mir/const_eval/eval_queries.rs +++ b/src/librustc_mir/const_eval/eval_queries.rs @@ -347,7 +347,7 @@ pub fn const_eval_raw_provider<'tcx>( // validation thus preventing such a hard error from being a backwards // compatibility hazard DefKind::Const | DefKind::AssocConst => { - let hir_id = tcx.hir().as_local_hir_id(def.did); + let hir_id = tcx.hir().local_def_id_to_hir_id(def.did); err.report_as_lint( tcx.at(tcx.def_span(def.did)), "any use of this value will cause an error", @@ -370,7 +370,7 @@ pub fn const_eval_raw_provider<'tcx>( err.report_as_lint( tcx.at(span), "reaching this expression at runtime will panic or abort", - tcx.hir().as_local_hir_id(def.did), + tcx.hir().local_def_id_to_hir_id(def.did), Some(err.span), ) } diff --git a/src/librustc_mir/const_eval/fn_queries.rs b/src/librustc_mir/const_eval/fn_queries.rs index 70ddd79ee40b0..9ef63b3322dd5 100644 --- a/src/librustc_mir/const_eval/fn_queries.rs +++ b/src/librustc_mir/const_eval/fn_queries.rs @@ -90,7 +90,7 @@ pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool { /// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether /// said intrinsic has a `rustc_const_{un,}stable` attribute. fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool { - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let node = tcx.hir().get(hir_id); diff --git a/src/librustc_mir/transform/check_consts/validation.rs b/src/librustc_mir/transform/check_consts/validation.rs index f64c72e7b362d..526c896df093c 100644 --- a/src/librustc_mir/transform/check_consts/validation.rs +++ b/src/librustc_mir/transform/check_consts/validation.rs @@ -215,7 +215,7 @@ impl Validator<'mir, 'tcx> { && !tcx.is_thread_local_static(def_id.to_def_id()); if should_check_for_sync { - let hir_id = tcx.hir().as_local_hir_id(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); check_return_ty_is_sync(tcx, &body, hir_id); } } diff --git a/src/librustc_mir/transform/check_unsafety.rs b/src/librustc_mir/transform/check_unsafety.rs index 9c06e173bcd57..d2a5616b8ed34 100644 --- a/src/librustc_mir/transform/check_unsafety.rs +++ b/src/librustc_mir/transform/check_unsafety.rs @@ -483,7 +483,7 @@ fn check_unused_unsafe( used_unsafe: &FxHashSet<hir::HirId>, unsafe_blocks: &mut Vec<(hir::HirId, bool)>, ) { - let body_id = tcx.hir().maybe_body_owned_by(tcx.hir().as_local_hir_id(def_id)); + let body_id = tcx.hir().maybe_body_owned_by(tcx.hir().local_def_id_to_hir_id(def_id)); let body_id = match body_id { Some(body) => body, @@ -511,7 +511,7 @@ fn unsafety_check_result<'tcx>( let param_env = tcx.param_env(def.did); - let id = tcx.hir().as_local_hir_id(def.did); + let id = tcx.hir().local_def_id_to_hir_id(def.did); let (const_context, min_const_fn) = match tcx.hir().body_owner_kind(id) { hir::BodyOwnerKind::Closure => (false, false), hir::BodyOwnerKind::Fn => { @@ -532,7 +532,7 @@ fn unsafety_check_result<'tcx>( } fn unsafe_derive_on_repr_packed(tcx: TyCtxt<'_>, def_id: LocalDefId) { - let lint_hir_id = tcx.hir().as_local_hir_id(def_id); + let lint_hir_id = tcx.hir().local_def_id_to_hir_id(def_id); tcx.struct_span_lint_hir(SAFE_PACKED_BORROWS, lint_hir_id, tcx.def_span(def_id), |lint| { // FIXME: when we make this a hard error, this should have its diff --git a/src/librustc_mir/transform/const_prop.rs b/src/librustc_mir/transform/const_prop.rs index 3b39d5f66b78f..7e8a94f181f63 100644 --- a/src/librustc_mir/transform/const_prop.rs +++ b/src/librustc_mir/transform/const_prop.rs @@ -67,7 +67,7 @@ impl<'tcx> MirPass<'tcx> for ConstProp { } use rustc_middle::hir::map::blocks::FnLikeNode; - let hir_id = tcx.hir().as_local_hir_id(source.def_id().expect_local()); + let hir_id = tcx.hir().local_def_id_to_hir_id(source.def_id().expect_local()); let is_fn_like = FnLikeNode::from_node(tcx.hir().get(hir_id)).is_some(); let is_assoc_const = tcx.def_kind(source.def_id()) == DefKind::AssocConst; diff --git a/src/librustc_mir/transform/inline.rs b/src/librustc_mir/transform/inline.rs index 92ea162e419db..315d4fa9d477d 100644 --- a/src/librustc_mir/transform/inline.rs +++ b/src/librustc_mir/transform/inline.rs @@ -75,7 +75,7 @@ impl Inliner<'tcx> { let param_env = self.tcx.param_env_reveal_all_normalized(self.source.def_id()); // Only do inlining into fn bodies. - let id = self.tcx.hir().as_local_hir_id(self.source.def_id().expect_local()); + let id = self.tcx.hir().local_def_id_to_hir_id(self.source.def_id().expect_local()); if self.tcx.hir().body_owner_kind(id).is_fn_or_closure() && self.source.promoted.is_none() { for (bb, bb_data) in caller_body.basic_blocks().iter_enumerated() { if let Some(callsite) = @@ -101,9 +101,9 @@ impl Inliner<'tcx> { } let callee_body = if let Some(callee_def_id) = callsite.callee.as_local() { - let callee_hir_id = self.tcx.hir().as_local_hir_id(callee_def_id); + let callee_hir_id = self.tcx.hir().local_def_id_to_hir_id(callee_def_id); let self_hir_id = - self.tcx.hir().as_local_hir_id(self.source.def_id().expect_local()); + self.tcx.hir().local_def_id_to_hir_id(self.source.def_id().expect_local()); // Avoid a cycle here by only using `optimized_mir` only if we have // a lower `HirId` than the callee. This ensures that the callee will // not inline us. This trick only works without incremental compilation. diff --git a/src/librustc_mir/transform/qualify_min_const_fn.rs b/src/librustc_mir/transform/qualify_min_const_fn.rs index dfd01e27d57c9..7abb09885ffd3 100644 --- a/src/librustc_mir/transform/qualify_min_const_fn.rs +++ b/src/librustc_mir/transform/qualify_min_const_fn.rs @@ -14,7 +14,7 @@ type McfResult = Result<(), (Span, Cow<'static, str>)>; pub fn is_min_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'a Body<'tcx>) -> McfResult { // Prevent const trait methods from being annotated as `stable`. if tcx.features().staged_api { - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); if crate::const_eval::is_parent_const_impl_raw(tcx, hir_id) { return Err((body.span, "trait methods cannot be stable const fn".into())); } diff --git a/src/librustc_mir_build/build/mod.rs b/src/librustc_mir_build/build/mod.rs index f3f3c3e33a46d..4d879469d48b8 100644 --- a/src/librustc_mir_build/build/mod.rs +++ b/src/librustc_mir_build/build/mod.rs @@ -35,7 +35,7 @@ crate fn mir_built<'tcx>( /// Construct the MIR for a given `DefId`. fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_> { - let id = tcx.hir().as_local_hir_id(def.did); + let id = tcx.hir().local_def_id_to_hir_id(def.did); // Figure out what primary body this item has. let (body_id, return_ty_span) = match tcx.hir().get(id) { diff --git a/src/librustc_mir_build/lints.rs b/src/librustc_mir_build/lints.rs index ac5d128a1baa2..662b6c7735747 100644 --- a/src/librustc_mir_build/lints.rs +++ b/src/librustc_mir_build/lints.rs @@ -11,7 +11,7 @@ use rustc_session::lint::builtin::UNCONDITIONAL_RECURSION; use rustc_span::Span; crate fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, def_id: LocalDefId) { - let hir_id = tcx.hir().as_local_hir_id(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); if let Some(fn_like_node) = FnLikeNode::from_node(tcx.hir().get(hir_id)) { if let FnKind::Closure(_) = fn_like_node.kind() { @@ -37,7 +37,7 @@ crate fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, def_id: LocalDefId) { vis.reachable_recursive_calls.sort(); - let hir_id = tcx.hir().as_local_hir_id(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let sp = tcx.sess.source_map().guess_head_span(tcx.hir().span(hir_id)); tcx.struct_span_lint_hir(UNCONDITIONAL_RECURSION, hir_id, sp, |lint| { let mut db = lint.build("function cannot return without recursing"); diff --git a/src/librustc_mir_build/thir/cx/expr.rs b/src/librustc_mir_build/thir/cx/expr.rs index ea41a66b3e43d..c51c3bcf56288 100644 --- a/src/librustc_mir_build/thir/cx/expr.rs +++ b/src/librustc_mir_build/thir/cx/expr.rs @@ -781,7 +781,7 @@ fn convert_path_expr<'a, 'tcx>( } Res::Def(DefKind::ConstParam, def_id) => { - let hir_id = cx.tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let item_id = cx.tcx.hir().get_parent_node(hir_id); let item_def_id = cx.tcx.hir().local_def_id(item_id); let generics = cx.tcx.generics_of(item_def_id); diff --git a/src/librustc_mir_build/thir/pattern/check_match.rs b/src/librustc_mir_build/thir/pattern/check_match.rs index 744f319205f94..1687286093d9c 100644 --- a/src/librustc_mir_build/thir/pattern/check_match.rs +++ b/src/librustc_mir_build/thir/pattern/check_match.rs @@ -23,7 +23,7 @@ use std::slice; crate fn check_match(tcx: TyCtxt<'_>, def_id: DefId) { let body_id = match def_id.as_local() { None => return, - Some(id) => tcx.hir().body_owned_by(tcx.hir().as_local_hir_id(id)), + Some(id) => tcx.hir().body_owned_by(tcx.hir().local_def_id_to_hir_id(id)), }; let mut visitor = MatchVisitor { diff --git a/src/librustc_passes/dead.rs b/src/librustc_passes/dead.rs index 0e5298acc2caf..29939c7cfafb0 100644 --- a/src/librustc_passes/dead.rs +++ b/src/librustc_passes/dead.rs @@ -62,7 +62,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> { fn check_def_id(&mut self, def_id: DefId) { if let Some(def_id) = def_id.as_local() { - let hir_id = self.tcx.hir().as_local_hir_id(def_id); + let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); if should_explore(self.tcx, hir_id) || self.struct_constructors.contains_key(&hir_id) { self.worklist.push(hir_id); } @@ -72,7 +72,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> { fn insert_def_id(&mut self, def_id: DefId) { if let Some(def_id) = def_id.as_local() { - let hir_id = self.tcx.hir().as_local_hir_id(def_id); + let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); debug_assert!(!should_explore(self.tcx, hir_id)); self.live_symbols.insert(hir_id); } @@ -461,7 +461,7 @@ fn create_and_seed_worklist<'tcx>( ) .chain( // Seed entry point - tcx.entry_fn(LOCAL_CRATE).map(|(def_id, _)| tcx.hir().as_local_hir_id(def_id)), + tcx.entry_fn(LOCAL_CRATE).map(|(def_id, _)| tcx.hir().local_def_id_to_hir_id(def_id)), ) .collect::<Vec<_>>(); @@ -546,7 +546,7 @@ impl DeadVisitor<'tcx> { for &impl_did in inherent_impls.iter() { for &item_did in &self.tcx.associated_item_def_ids(impl_did)[..] { if let Some(did) = item_did.as_local() { - let item_hir_id = self.tcx.hir().as_local_hir_id(did); + let item_hir_id = self.tcx.hir().local_def_id_to_hir_id(did); if self.live_symbols.contains(&item_hir_id) { return true; } diff --git a/src/librustc_passes/reachable.rs b/src/librustc_passes/reachable.rs index c71dbdf515aa9..18fa4ada4dadd 100644 --- a/src/librustc_passes/reachable.rs +++ b/src/librustc_passes/reachable.rs @@ -53,7 +53,7 @@ fn method_might_be_inlined( return true; } } - match tcx.hir().find(tcx.hir().as_local_hir_id(impl_src)) { + match tcx.hir().find(tcx.hir().local_def_id_to_hir_id(impl_src)) { Some(Node::Item(item)) => item_might_be_inlined(tcx, &item, codegen_fn_attrs), Some(..) | None => span_bug!(impl_item.span, "impl did is not an item"), } @@ -106,7 +106,9 @@ impl<'tcx> Visitor<'tcx> for ReachableContext<'tcx> { } Some(res) => { if let Some((hir_id, def_id)) = res.opt_def_id().and_then(|def_id| { - def_id.as_local().map(|def_id| (self.tcx.hir().as_local_hir_id(def_id), def_id)) + def_id + .as_local() + .map(|def_id| (self.tcx.hir().local_def_id_to_hir_id(def_id), def_id)) }) { if self.def_id_represents_local_inlined_item(def_id.to_def_id()) { self.worklist.push(hir_id); @@ -149,7 +151,7 @@ impl<'tcx> ReachableContext<'tcx> { // eligible for inlining and false otherwise. fn def_id_represents_local_inlined_item(&self, def_id: DefId) -> bool { let hir_id = match def_id.as_local() { - Some(def_id) => self.tcx.hir().as_local_hir_id(def_id), + Some(def_id) => self.tcx.hir().local_def_id_to_hir_id(def_id), None => { return false; } @@ -181,7 +183,7 @@ impl<'tcx> ReachableContext<'tcx> { // Check the impl. If the generics on the self // type of the impl require inlining, this method // does too. - let impl_hir_id = self.tcx.hir().as_local_hir_id(impl_did); + let impl_hir_id = self.tcx.hir().local_def_id_to_hir_id(impl_did); match self.tcx.hir().expect_item(impl_hir_id).kind { hir::ItemKind::Impl { .. } => { let generics = self.tcx.generics_of(impl_did); @@ -366,10 +368,10 @@ impl<'a, 'tcx> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a, 'tcx // FIXME(#53488) remove `let` let tcx = self.tcx; - self.worklist.extend( - tcx.provided_trait_methods(trait_def_id) - .map(|assoc| tcx.hir().as_local_hir_id(assoc.def_id.expect_local())), - ); + self.worklist + .extend(tcx.provided_trait_methods(trait_def_id).map(|assoc| { + tcx.hir().local_def_id_to_hir_id(assoc.def_id.expect_local()) + })); } } } @@ -406,7 +408,7 @@ fn reachable_set<'tcx>(tcx: TyCtxt<'tcx>, crate_num: CrateNum) -> &'tcx HirIdSet reachable_context.worklist.extend(access_levels.map.iter().map(|(id, _)| *id)); for item in tcx.lang_items().items().iter() { if let Some(did) = *item { - if let Some(hir_id) = did.as_local().map(|did| tcx.hir().as_local_hir_id(did)) { + if let Some(hir_id) = did.as_local().map(|did| tcx.hir().local_def_id_to_hir_id(did)) { reachable_context.worklist.push(hir_id); } } diff --git a/src/librustc_passes/region.rs b/src/librustc_passes/region.rs index b2a89651881e5..1af79abe4b911 100644 --- a/src/librustc_passes/region.rs +++ b/src/librustc_passes/region.rs @@ -807,7 +807,7 @@ fn region_scope_tree(tcx: TyCtxt<'_>, def_id: DefId) -> &ScopeTree { return tcx.region_scope_tree(closure_base_def_id); } - let id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let scope_tree = if let Some(body_id) = tcx.hir().maybe_body_owned_by(id) { let mut visitor = RegionResolutionVisitor { tcx, diff --git a/src/librustc_passes/upvars.rs b/src/librustc_passes/upvars.rs index 3aed4942563dd..91b8ae07637df 100644 --- a/src/librustc_passes/upvars.rs +++ b/src/librustc_passes/upvars.rs @@ -15,7 +15,7 @@ pub fn provide(providers: &mut Providers) { return None; } - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let body = tcx.hir().body(tcx.hir().maybe_body_owned_by(hir_id)?); let mut local_collector = LocalCollector::default(); diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index 3ba5acd00a09a..a5536942581be 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -226,7 +226,7 @@ fn def_id_visibility<'tcx>( tcx: TyCtxt<'tcx>, def_id: DefId, ) -> (ty::Visibility, Span, &'static str) { - match def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) { + match def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)) { Some(hir_id) => { let vis = match tcx.hir().get(hir_id) { Node::Item(item) => &item.vis, @@ -431,7 +431,7 @@ impl VisibilityLike for Option<AccessLevel> { fn new_min(find: &FindMin<'_, '_, Self>, def_id: DefId) -> Self { cmp::min( if let Some(def_id) = def_id.as_local() { - let hir_id = find.tcx.hir().as_local_hir_id(def_id); + let hir_id = find.tcx.hir().local_def_id_to_hir_id(def_id); find.access_levels.map.get(&hir_id).cloned() } else { Self::MAX @@ -533,7 +533,7 @@ impl EmbargoVisitor<'tcx> { if let Res::Def(def_kind, def_id) = export.res { let vis = def_id_visibility(self.tcx, def_id).0; if let Some(def_id) = def_id.as_local() { - let hir_id = self.tcx.hir().as_local_hir_id(def_id); + let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); self.update_macro_reachable_def(hir_id, def_kind, vis, defining_mod); } } @@ -650,7 +650,7 @@ impl EmbargoVisitor<'tcx> { // there will be no corresponding item. .filter(|def_id| def_id.index != CRATE_DEF_INDEX || def_id.krate != LOCAL_CRATE) .and_then(|def_id| { - def_id.as_local().map(|def_id| self.tcx.hir().as_local_hir_id(def_id)) + def_id.as_local().map(|def_id| self.tcx.hir().local_def_id_to_hir_id(def_id)) }) .map(|module_hir_id| self.tcx.hir().expect_item(module_hir_id)) { @@ -913,7 +913,7 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> { if export.vis == ty::Visibility::Public { if let Some(def_id) = export.res.opt_def_id() { if let Some(def_id) = def_id.as_local() { - let hir_id = self.tcx.hir().as_local_hir_id(def_id); + let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); self.update(hir_id, Some(AccessLevel::Exported)); } } @@ -1004,7 +1004,7 @@ impl DefIdVisitor<'tcx> for ReachEverythingInTheInterfaceVisitor<'_, 'tcx> { } fn visit_def_id(&mut self, def_id: DefId, _kind: &str, _descr: &dyn fmt::Display) -> bool { if let Some(def_id) = def_id.as_local() { - let hir_id = self.ev.tcx.hir().as_local_hir_id(def_id); + let hir_id = self.ev.tcx.hir().local_def_id_to_hir_id(def_id); if let ((ty::Visibility::Public, ..), _) | (_, Some(AccessLevel::ReachableFromImplTrait)) = (def_id_visibility(self.tcx(), def_id.to_def_id()), self.access_level) @@ -1437,7 +1437,7 @@ impl<'a, 'tcx> ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> { if let Some(did) = did.as_local() { // .. and it corresponds to a private type in the AST (this returns // `None` for type parameters). - match self.tcx.hir().find(self.tcx.hir().as_local_hir_id(did)) { + match self.tcx.hir().find(self.tcx.hir().local_def_id_to_hir_id(did)) { Some(Node::Item(ref item)) => !item.vis.node.is_pub(), Some(_) | None => false, } @@ -1556,7 +1556,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> { let did = tr.path.res.def_id(); if let Some(did) = did.as_local() { - self.trait_is_public(self.tcx.hir().as_local_hir_id(did)) + self.trait_is_public(self.tcx.hir().local_def_id_to_hir_id(did)) } else { true // external traits must be public } @@ -1815,7 +1815,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'tcx> { } let hir_id = match def_id.as_local() { - Some(def_id) => self.tcx.hir().as_local_hir_id(def_id), + Some(def_id) => self.tcx.hir().local_def_id_to_hir_id(def_id), None => return false, }; diff --git a/src/librustc_resolve/late/diagnostics.rs b/src/librustc_resolve/late/diagnostics.rs index f0ea325d2ab74..a7d3697405751 100644 --- a/src/librustc_resolve/late/diagnostics.rs +++ b/src/librustc_resolve/late/diagnostics.rs @@ -16,8 +16,8 @@ use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc_hir::PrimTy; use rustc_session::config::nightly_options; use rustc_span::hygiene::MacroKind; -use rustc_span::symbol::{kw, sym, Ident}; -use rustc_span::{BytePos, Span}; +use rustc_span::symbol::{kw, sym, Ident, Symbol}; +use rustc_span::{BytePos, Span, DUMMY_SP}; use log::debug; @@ -33,6 +33,7 @@ enum AssocSuggestion { crate enum MissingLifetimeSpot<'tcx> { Generics(&'tcx hir::Generics<'tcx>), HigherRanked { span: Span, span_type: ForLifetimeSpanType }, + Static, } crate enum ForLifetimeSpanType { @@ -1195,6 +1196,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { https://doc.rust-lang.org/nomicon/hrtb.html", ); } + _ => {} } } if nightly_options::is_nightly_build() @@ -1253,7 +1255,8 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { err: &mut DiagnosticBuilder<'_>, span: Span, count: usize, - lifetime_names: &FxHashSet<Ident>, + lifetime_names: &FxHashSet<Symbol>, + lifetime_spans: Vec<Span>, params: &[ElisionFailureInfo], ) { let snippet = self.tcx.sess.source_map().span_to_snippet(span).ok(); @@ -1267,11 +1270,60 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { ), ); - let suggest_existing = |err: &mut DiagnosticBuilder<'_>, sugg| { + let suggest_existing = |err: &mut DiagnosticBuilder<'_>, + name: &str, + formatter: &dyn Fn(&str) -> String| { + if let Some(MissingLifetimeSpot::HigherRanked { span: for_span, span_type }) = + self.missing_named_lifetime_spots.iter().rev().next() + { + // When we have `struct S<'a>(&'a dyn Fn(&X) -> &X);` we want to not only suggest + // using `'a`, but also introduce the concept of HRLTs by suggesting + // `struct S<'a>(&'a dyn for<'b> Fn(&X) -> &'b X);`. (#72404) + let mut introduce_suggestion = vec![]; + + let a_to_z_repeat_n = |n| { + (b'a'..=b'z').map(move |c| { + let mut s = '\''.to_string(); + s.extend(std::iter::repeat(char::from(c)).take(n)); + s + }) + }; + + // If all single char lifetime names are present, we wrap around and double the chars. + let lt_name = (1..) + .flat_map(a_to_z_repeat_n) + .find(|lt| !lifetime_names.contains(&Symbol::intern(<))) + .unwrap(); + let msg = format!( + "consider making the {} lifetime-generic with a new `{}` lifetime", + span_type.descr(), + lt_name, + ); + err.note( + "for more information on higher-ranked polymorphism, visit \ + https://doc.rust-lang.org/nomicon/hrtb.html", + ); + let for_sugg = span_type.suggestion(<_name); + for param in params { + if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(param.span) { + if snippet.starts_with('&') && !snippet.starts_with("&'") { + introduce_suggestion + .push((param.span, format!("&{} {}", lt_name, &snippet[1..]))); + } else if snippet.starts_with("&'_ ") { + introduce_suggestion + .push((param.span, format!("&{} {}", lt_name, &snippet[4..]))); + } + } + } + introduce_suggestion.push((*for_span, for_sugg.to_string())); + introduce_suggestion.push((span, formatter(<_name))); + err.multipart_suggestion(&msg, introduce_suggestion, Applicability::MaybeIncorrect); + } + err.span_suggestion_verbose( span, &format!("consider using the `{}` lifetime", lifetime_names.iter().next().unwrap()), - sugg, + formatter(name), Applicability::MaybeIncorrect, ); }; @@ -1282,6 +1334,15 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { let should_break; introduce_suggestion.push(match missing { MissingLifetimeSpot::Generics(generics) => { + if generics.span == DUMMY_SP { + // Account for malformed generics in the HIR. This shouldn't happen, + // but if we make a mistake elsewhere, mainly by keeping something in + // `missing_named_lifetime_spots` that we shouldn't, like associated + // `const`s or making a mistake in the AST lowering we would provide + // non-sensical suggestions. Guard against that by skipping these. + // (#74264) + continue; + } msg = "consider introducing a named lifetime parameter".to_string(); should_break = true; if let Some(param) = generics.params.iter().find(|p| match p.kind { @@ -1308,6 +1369,42 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { ); (*span, span_type.suggestion("'a")) } + MissingLifetimeSpot::Static => { + let (span, sugg) = match snippet.as_deref() { + Some("&") => (span.shrink_to_hi(), "'static ".to_owned()), + Some("'_") => (span, "'static".to_owned()), + Some(snippet) if !snippet.ends_with('>') => { + if snippet == "" { + ( + span, + std::iter::repeat("'static") + .take(count) + .collect::<Vec<_>>() + .join(", "), + ) + } else { + ( + span.shrink_to_hi(), + format!( + "<{}>", + std::iter::repeat("'static") + .take(count) + .collect::<Vec<_>>() + .join(", ") + ), + ) + } + } + _ => continue, + }; + err.span_suggestion_verbose( + span, + "consider using the `'static` lifetime", + sugg.to_string(), + Applicability::MaybeIncorrect, + ); + continue; + } }); for param in params { if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(param.span) { @@ -1328,19 +1425,19 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { } }; - match (lifetime_names.len(), lifetime_names.iter().next(), snippet.as_deref()) { - (1, Some(name), Some("&")) => { - suggest_existing(err, format!("&{} ", name)); + let lifetime_names: Vec<_> = lifetime_names.into_iter().collect(); + match (&lifetime_names[..], snippet.as_deref()) { + ([name], Some("&")) => { + suggest_existing(err, &name.as_str()[..], &|name| format!("&{} ", name)); } - (1, Some(name), Some("'_")) => { - suggest_existing(err, name.to_string()); + ([name], Some("'_")) => { + suggest_existing(err, &name.as_str()[..], &|n| n.to_string()); } - (1, Some(name), Some("")) => { - suggest_existing(err, format!("{}, ", name).repeat(count)); + ([name], Some("")) => { + suggest_existing(err, &name.as_str()[..], &|n| format!("{}, ", n).repeat(count)); } - (1, Some(name), Some(snippet)) if !snippet.ends_with('>') => { - suggest_existing( - err, + ([name], Some(snippet)) if !snippet.ends_with('>') => { + let f = |name: &str| { format!( "{}<{}>", snippet, @@ -1348,21 +1445,37 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { .take(count) .collect::<Vec<_>>() .join(", ") - ), - ); + ) + }; + suggest_existing(err, &name.as_str()[..], &f); } - (0, _, Some("&")) if count == 1 => { + ([], Some("&")) if count == 1 => { suggest_new(err, "&'a "); } - (0, _, Some("'_")) if count == 1 => { + ([], Some("'_")) if count == 1 => { suggest_new(err, "'a"); } - (0, _, Some(snippet)) if !snippet.ends_with('>') && count == 1 => { - suggest_new(err, &format!("{}<'a>", snippet)); + ([], Some(snippet)) if !snippet.ends_with('>') => { + if snippet == "" { + // This happens when we have `type Bar<'a> = Foo<T>` where we point at the space + // before `T`. We will suggest `type Bar<'a> = Foo<'a, T>`. + suggest_new( + err, + &std::iter::repeat("'a, ").take(count).collect::<Vec<_>>().join(""), + ); + } else { + suggest_new( + err, + &format!( + "{}<{}>", + snippet, + std::iter::repeat("'a").take(count).collect::<Vec<_>>().join(", ") + ), + ); + } } - (n, ..) if n > 1 => { - let spans: Vec<Span> = lifetime_names.iter().map(|lt| lt.span).collect(); - err.span_note(spans, "these named lifetimes are available to use"); + (lts, ..) if lts.len() > 1 => { + err.span_note(lifetime_spans, "these named lifetimes are available to use"); if Some("") == snippet.as_deref() { // This happens when we have `Foo<T>` where we point at the space before `T`, // but this can be confusing so we give a suggestion with placeholders. diff --git a/src/librustc_resolve/late/lifetimes.rs b/src/librustc_resolve/late/lifetimes.rs index 0b881b089deaa..4dfd68aaf858b 100644 --- a/src/librustc_resolve/late/lifetimes.rs +++ b/src/librustc_resolve/late/lifetimes.rs @@ -607,7 +607,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { let def = self.map.defs.get(&lifetime.hir_id).cloned(); if let Some(Region::LateBound(_, def_id, _)) = def { if let Some(def_id) = def_id.as_local() { - let hir_id = self.tcx.hir().as_local_hir_id(def_id); + let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); // Ensure that the parent of the def is an item, not HRTB let parent_id = self.tcx.hir().get_parent_node(hir_id); let parent_impl_id = hir::ImplItemId { hir_id: parent_id }; @@ -711,9 +711,9 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) { use self::hir::TraitItemKind::*; - self.missing_named_lifetime_spots.push((&trait_item.generics).into()); match trait_item.kind { Fn(ref sig, _) => { + self.missing_named_lifetime_spots.push((&trait_item.generics).into()); let tcx = self.tcx; self.visit_early_late( Some(tcx.hir().get_parent_item(trait_item.hir_id)), @@ -721,8 +721,10 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { &trait_item.generics, |this| intravisit::walk_trait_item(this, trait_item), ); + self.missing_named_lifetime_spots.pop(); } Type(bounds, ref ty) => { + self.missing_named_lifetime_spots.push((&trait_item.generics).into()); let generics = &trait_item.generics; let mut index = self.next_early_index(); debug!("visit_ty: index = {}", index); @@ -757,31 +759,35 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { this.visit_ty(ty); } }); + self.missing_named_lifetime_spots.pop(); } Const(_, _) => { // Only methods and types support generics. assert!(trait_item.generics.params.is_empty()); + self.missing_named_lifetime_spots.push(MissingLifetimeSpot::Static); intravisit::walk_trait_item(self, trait_item); + self.missing_named_lifetime_spots.pop(); } } - self.missing_named_lifetime_spots.pop(); } fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) { use self::hir::ImplItemKind::*; - self.missing_named_lifetime_spots.push((&impl_item.generics).into()); match impl_item.kind { Fn(ref sig, _) => { + self.missing_named_lifetime_spots.push((&impl_item.generics).into()); let tcx = self.tcx; self.visit_early_late( Some(tcx.hir().get_parent_item(impl_item.hir_id)), &sig.decl, &impl_item.generics, |this| intravisit::walk_impl_item(this, impl_item), - ) + ); + self.missing_named_lifetime_spots.pop(); } TyAlias(ref ty) => { let generics = &impl_item.generics; + self.missing_named_lifetime_spots.push(generics.into()); let mut index = self.next_early_index(); let mut non_lifetime_count = 0; debug!("visit_ty: index = {}", index); @@ -810,14 +816,16 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { this.visit_generics(generics); this.visit_ty(ty); }); + self.missing_named_lifetime_spots.pop(); } Const(_, _) => { // Only methods and types support generics. assert!(impl_item.generics.params.is_empty()); + self.missing_named_lifetime_spots.push(MissingLifetimeSpot::Static); intravisit::walk_impl_item(self, impl_item); + self.missing_named_lifetime_spots.pop(); } } - self.missing_named_lifetime_spots.pop(); } fn visit_lifetime(&mut self, lifetime_ref: &'tcx hir::Lifetime) { @@ -1146,7 +1154,8 @@ fn extract_labels(ctxt: &mut LifetimeContext<'_, '_>, body: &hir::Body<'_>) { if let Some(def) = lifetimes.get(&hir::ParamName::Plain(label.normalize_to_macros_2_0())) { - let hir_id = tcx.hir().as_local_hir_id(def.id().unwrap().expect_local()); + let hir_id = + tcx.hir().local_def_id_to_hir_id(def.id().unwrap().expect_local()); signal_shadowing_problem( tcx, @@ -1517,7 +1526,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { match lifetimeuseset { Some(LifetimeUseSet::One(lifetime)) => { - let hir_id = self.tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); debug!("hir id first={:?}", hir_id); if let Some((id, span, name)) = match self.tcx.hir().get(hir_id) { Node::Lifetime(hir_lifetime) => Some(( @@ -1537,7 +1546,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { if let Some(parent_def_id) = self.tcx.parent(def_id) { if let Some(def_id) = parent_def_id.as_local() { - let parent_hir_id = self.tcx.hir().as_local_hir_id(def_id); + let parent_hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); // lifetimes in `derive` expansions don't count (Issue #53738) if self.tcx.hir().attrs(parent_hir_id).iter().any(|attr| { self.tcx.sess.check_name(attr, sym::automatically_derived) @@ -1575,7 +1584,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { debug!("not one use lifetime"); } None => { - let hir_id = self.tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); if let Some((id, span, name)) = match self.tcx.hir().get(hir_id) { Node::Lifetime(hir_lifetime) => Some(( hir_lifetime.hir_id, @@ -1931,7 +1940,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { let map = &self.map; let unsubst = if let Some(def_id) = def_id.as_local() { - let id = self.tcx.hir().as_local_hir_id(def_id); + let id = self.tcx.hir().local_def_id_to_hir_id(def_id); &map.object_lifetime_defaults[&id] } else { let tcx = self.tcx; @@ -2315,6 +2324,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { let mut late_depth = 0; let mut scope = self.scope; let mut lifetime_names = FxHashSet::default(); + let mut lifetime_spans = vec![]; let error = loop { match *scope { // Do not assign any resolution, it will be inferred. @@ -2326,7 +2336,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { // collect named lifetimes for suggestions for name in lifetimes.keys() { if let hir::ParamName::Plain(name) = name { - lifetime_names.insert(*name); + lifetime_names.insert(name.name); + lifetime_spans.push(name.span); } } late_depth += 1; @@ -2344,12 +2355,24 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { } Elide::Exact(l) => l.shifted(late_depth), Elide::Error(ref e) => { - if let Scope::Binder { ref lifetimes, .. } = s { - // collect named lifetimes for suggestions - for name in lifetimes.keys() { - if let hir::ParamName::Plain(name) = name { - lifetime_names.insert(*name); + let mut scope = s; + loop { + match scope { + Scope::Binder { ref lifetimes, s, .. } => { + // Collect named lifetimes for suggestions. + for name in lifetimes.keys() { + if let hir::ParamName::Plain(name) = name { + lifetime_names.insert(name.name); + lifetime_spans.push(name.span); + } + } + scope = s; + } + Scope::ObjectLifetimeDefault { ref s, .. } + | Scope::Elision { ref s, .. } => { + scope = s; } + _ => break, } } break Some(e); @@ -2373,7 +2396,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { if let Some(params) = error { // If there's no lifetime available, suggest `'static`. if self.report_elision_failure(&mut err, params) && lifetime_names.is_empty() { - lifetime_names.insert(Ident::with_dummy_span(kw::StaticLifetime)); + lifetime_names.insert(kw::StaticLifetime); } } self.add_missing_lifetime_specifiers_label( @@ -2381,6 +2404,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { span, lifetime_refs.len(), &lifetime_names, + lifetime_spans, error.map(|p| &p[..]).unwrap_or(&[]), ); err.emit(); @@ -2614,7 +2638,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { Scope::Binder { ref lifetimes, s, .. } => { if let Some(&def) = lifetimes.get(¶m.name.normalize_to_macros_2_0()) { let hir_id = - self.tcx.hir().as_local_hir_id(def.id().unwrap().expect_local()); + self.tcx.hir().local_def_id_to_hir_id(def.id().unwrap().expect_local()); signal_shadowing_problem( self.tcx, diff --git a/src/librustc_serialize/collection_impls.rs b/src/librustc_serialize/collection_impls.rs index c602de37b14f7..49b8094abd0eb 100644 --- a/src/librustc_serialize/collection_impls.rs +++ b/src/librustc_serialize/collection_impls.rs @@ -86,11 +86,9 @@ where { fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> { e.emit_map(self.len(), |e| { - let mut i = 0; - for (key, val) in self { + for (i, (key, val)) in self.iter().enumerate() { e.emit_map_elt_key(i, |e| key.encode(e))?; e.emit_map_elt_val(i, |e| val.encode(e))?; - i += 1; } Ok(()) }) @@ -121,10 +119,8 @@ where { fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> { s.emit_seq(self.len(), |s| { - let mut i = 0; - for e in self { + for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s))?; - i += 1; } Ok(()) }) @@ -154,11 +150,9 @@ where { fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> { e.emit_map(self.len(), |e| { - let mut i = 0; - for (key, val) in self { + for (i, (key, val)) in self.iter().enumerate() { e.emit_map_elt_key(i, |e| key.encode(e))?; e.emit_map_elt_val(i, |e| val.encode(e))?; - i += 1; } Ok(()) }) @@ -192,10 +186,8 @@ where { fn encode<E: Encoder>(&self, s: &mut E) -> Result<(), E::Error> { s.emit_seq(self.len(), |s| { - let mut i = 0; - for e in self { + for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s))?; - i += 1; } Ok(()) }) @@ -227,11 +219,9 @@ where { fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> { e.emit_map(self.len(), |e| { - let mut i = 0; - for (key, val) in self { + for (i, (key, val)) in self.iter().enumerate() { e.emit_map_elt_key(i, |e| key.encode(e))?; e.emit_map_elt_val(i, |e| val.encode(e))?; - i += 1; } Ok(()) }) @@ -265,10 +255,8 @@ where { fn encode<E: Encoder>(&self, s: &mut E) -> Result<(), E::Error> { s.emit_seq(self.len(), |s| { - let mut i = 0; - for e in self { + for (i, e) in self.iter().enumerate() { s.emit_seq_elt(i, |s| e.encode(s))?; - i += 1; } Ok(()) }) diff --git a/src/librustc_serialize/json.rs b/src/librustc_serialize/json.rs index 820ebdc9baa52..ab7f6975325bc 100644 --- a/src/librustc_serialize/json.rs +++ b/src/librustc_serialize/json.rs @@ -78,19 +78,17 @@ //! data_vector: Vec<u8>, //! } //! -//! fn main() { -//! let object = TestStruct { -//! data_int: 1, -//! data_str: "homura".to_string(), -//! data_vector: vec![2,3,4,5], -//! }; +//! let object = TestStruct { +//! data_int: 1, +//! data_str: "homura".to_string(), +//! data_vector: vec![2,3,4,5], +//! }; //! -//! // Serialize using `json::encode` -//! let encoded = json::encode(&object).unwrap(); +//! // Serialize using `json::encode` +//! let encoded = json::encode(&object).unwrap(); //! -//! // Deserialize using `json::decode` -//! let decoded: TestStruct = json::decode(&encoded[..]).unwrap(); -//! } +//! // Deserialize using `json::decode` +//! let decoded: TestStruct = json::decode(&encoded[..]).unwrap(); //! ``` //! //! ## Using the `ToJson` trait @@ -125,16 +123,14 @@ //! val: Json, //! } //! -//! fn main() { -//! let num = ComplexNum { a: 0.0001, b: 12.539 }; -//! let data: String = json::encode(&ComplexNumRecord{ -//! uid: 1, -//! dsc: "test".to_string(), -//! val: num.to_json(), -//! }).unwrap(); -//! println!("data: {}", data); -//! // data: {"uid":1,"dsc":"test","val":"0.0001+12.539i"}; -//! } +//! let num = ComplexNum { a: 0.0001, b: 12.539 }; +//! let data: String = json::encode(&ComplexNumRecord{ +//! uid: 1, +//! dsc: "test".to_string(), +//! val: num.to_json(), +//! }).unwrap(); +//! println!("data: {}", data); +//! // data: {"uid":1,"dsc":"test","val":"0.0001+12.539i"}; //! ``` //! //! ### Verbose example of `ToJson` usage @@ -164,19 +160,17 @@ //! } //! } //! -//! fn main() { -//! // Serialize using `ToJson` -//! let input_data = TestStruct { -//! data_int: 1, -//! data_str: "madoka".to_string(), -//! data_vector: vec![2,3,4,5], -//! }; -//! let json_obj: Json = input_data.to_json(); -//! let json_str: String = json_obj.to_string(); +//! // Serialize using `ToJson` +//! let input_data = TestStruct { +//! data_int: 1, +//! data_str: "madoka".to_string(), +//! data_vector: vec![2,3,4,5], +//! }; +//! let json_obj: Json = input_data.to_json(); +//! let json_str: String = json_obj.to_string(); //! -//! // Deserialize like before -//! let decoded: TestStruct = json::decode(&json_str).unwrap(); -//! } +//! // Deserialize like before +//! let decoded: TestStruct = json::decode(&json_str).unwrap(); //! ``` use self::DecoderError::*; @@ -1269,34 +1263,22 @@ impl Json { /// Returns `true` if the Json value is a `Number`. pub fn is_number(&self) -> bool { - match *self { - Json::I64(_) | Json::U64(_) | Json::F64(_) => true, - _ => false, - } + matches!(*self, Json::I64(_) | Json::U64(_) | Json::F64(_)) } /// Returns `true` if the Json value is a `i64`. pub fn is_i64(&self) -> bool { - match *self { - Json::I64(_) => true, - _ => false, - } + matches!(*self, Json::I64(_)) } /// Returns `true` if the Json value is a `u64`. pub fn is_u64(&self) -> bool { - match *self { - Json::U64(_) => true, - _ => false, - } + matches!(*self, Json::U64(_)) } /// Returns `true` if the Json value is a `f64`. pub fn is_f64(&self) -> bool { - match *self { - Json::F64(_) => true, - _ => false, - } + matches!(*self, Json::F64(_)) } /// If the Json value is a number, returns or cast it to a `i64`; @@ -1416,6 +1398,7 @@ enum ParserState { /// structure of the JSON stream. /// /// An example is `foo.bar[3].x`. +#[derive(Default)] pub struct Stack { stack: Vec<InternalStackElement>, str_buffer: Vec<u8>, @@ -1442,7 +1425,7 @@ enum InternalStackElement { impl Stack { pub fn new() -> Stack { - Stack { stack: Vec::new(), str_buffer: Vec::new() } + Self::default() } /// Returns The number of elements in the Stack. @@ -1547,10 +1530,7 @@ impl Stack { // Used by Parser to test whether the top-most element is an index. fn last_is_index(&self) -> bool { - match self.stack.last() { - Some(InternalIndex(_)) => true, - _ => false, - } + matches!(self.stack.last(), Some(InternalIndex(_))) } // Used by Parser to increment the index of the top-most element. diff --git a/src/librustc_serialize/opaque.rs b/src/librustc_serialize/opaque.rs index 39f3abb75271b..fa4423e261d1c 100644 --- a/src/librustc_serialize/opaque.rs +++ b/src/librustc_serialize/opaque.rs @@ -118,13 +118,13 @@ impl serialize::Encoder for Encoder { #[inline] fn emit_f64(&mut self, v: f64) -> EncodeResult { - let as_u64: u64 = unsafe { ::std::mem::transmute(v) }; + let as_u64: u64 = v.to_bits(); self.emit_u64(as_u64) } #[inline] fn emit_f32(&mut self, v: f32) -> EncodeResult { - let as_u32: u32 = unsafe { ::std::mem::transmute(v) }; + let as_u32: u32 = v.to_bits(); self.emit_u32(as_u32) } diff --git a/src/librustc_symbol_mangling/lib.rs b/src/librustc_symbol_mangling/lib.rs index 2579cf53d3d50..32e5aef00f8b8 100644 --- a/src/librustc_symbol_mangling/lib.rs +++ b/src/librustc_symbol_mangling/lib.rs @@ -173,7 +173,7 @@ fn compute_symbol_name( let disambiguator = tcx.sess.local_crate_disambiguator(); return tcx.sess.generate_proc_macro_decls_symbol(disambiguator); } - let hir_id = tcx.hir().as_local_hir_id(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); match tcx.hir().get(hir_id) { Node::ForeignItem(_) => true, _ => false, diff --git a/src/librustc_trait_selection/opaque_types.rs b/src/librustc_trait_selection/opaque_types.rs index b84ad93341e81..379c976df69e3 100644 --- a/src/librustc_trait_selection/opaque_types.rs +++ b/src/librustc_trait_selection/opaque_types.rs @@ -1039,7 +1039,7 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> { // } // ``` if let Some(def_id) = def_id.as_local() { - let opaque_hir_id = tcx.hir().as_local_hir_id(def_id); + let opaque_hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let parent_def_id = self.parent_def_id; let def_scope_default = || { let opaque_parent_hir_id = tcx.hir().get_parent_item(opaque_hir_id); @@ -1205,7 +1205,7 @@ pub fn may_define_opaque_type( def_id: LocalDefId, opaque_hir_id: hir::HirId, ) -> bool { - let mut hir_id = tcx.hir().as_local_hir_id(def_id); + let mut hir_id = tcx.hir().local_def_id_to_hir_id(def_id); // Named opaque types can be defined by any siblings or children of siblings. let scope = tcx.hir().get_defining_scope(opaque_hir_id); diff --git a/src/librustc_trait_selection/traits/chalk_fulfill.rs b/src/librustc_trait_selection/traits/chalk_fulfill.rs index a75240042ad76..0097097707f32 100644 --- a/src/librustc_trait_selection/traits/chalk_fulfill.rs +++ b/src/librustc_trait_selection/traits/chalk_fulfill.rs @@ -41,7 +41,7 @@ fn environment<'tcx>( let clauses = predicates.into_iter().map(ChalkEnvironmentClause::Predicate); - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let node = tcx.hir().get(hir_id); enum NodeKind { diff --git a/src/librustc_trait_selection/traits/error_reporting/mod.rs b/src/librustc_trait_selection/traits/error_reporting/mod.rs index 349fa68a4da99..e597843e6ce91 100644 --- a/src/librustc_trait_selection/traits/error_reporting/mod.rs +++ b/src/librustc_trait_selection/traits/error_reporting/mod.rs @@ -568,7 +568,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { self.tcx.sess.source_map().guess_head_span( self.tcx.hir().span_if_local(closure_def_id).unwrap(), ); - let hir_id = self.tcx.hir().as_local_hir_id(closure_def_id.expect_local()); + let hir_id = + self.tcx.hir().local_def_id_to_hir_id(closure_def_id.expect_local()); let mut err = struct_span_err!( self.tcx.sess, closure_span, diff --git a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs index e29e740f13669..7513ff6b37ef8 100644 --- a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs +++ b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs @@ -535,7 +535,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { }; let hir = self.tcx.hir(); - let hir_id = hir.as_local_hir_id(def_id.as_local()?); + let hir_id = hir.local_def_id_to_hir_id(def_id.as_local()?); let parent_node = hir.get_parent_node(hir_id); match hir.find(parent_node) { Some(hir::Node::Stmt(hir::Stmt { kind: hir::StmtKind::Local(local), .. })) => { @@ -1383,7 +1383,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { let generator_body = generator_did .as_local() - .map(|def_id| hir.as_local_hir_id(def_id)) + .map(|def_id| hir.local_def_id_to_hir_id(def_id)) .and_then(|hir_id| hir.maybe_body_owned_by(hir_id)) .map(|body_id| hir.body(body_id)); let mut visitor = AwaitsVisitor::default(); @@ -1535,7 +1535,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { .tcx .parent(generator_did) .and_then(|parent_did| parent_did.as_local()) - .map(|parent_did| hir.as_local_hir_id(parent_did)) + .map(|parent_did| hir.local_def_id_to_hir_id(parent_did)) .and_then(|parent_hir_id| hir.opt_name(parent_hir_id)) .map(|name| { format!("future returned by `{}` is not {}", name, trait_name) diff --git a/src/librustc_trait_selection/traits/specialize/mod.rs b/src/librustc_trait_selection/traits/specialize/mod.rs index 9b737d464174a..4d81a3baa0edc 100644 --- a/src/librustc_trait_selection/traits/specialize/mod.rs +++ b/src/librustc_trait_selection/traits/specialize/mod.rs @@ -451,7 +451,7 @@ fn report_conflicting_impls( }; tcx.struct_span_lint_hir( lint, - tcx.hir().as_local_hir_id(impl_def_id), + tcx.hir().local_def_id_to_hir_id(impl_def_id), impl_span, decorate, ) diff --git a/src/librustc_ty/ty.rs b/src/librustc_ty/ty.rs index b31f9f3c7b14f..0f1dee7e2e006 100644 --- a/src/librustc_ty/ty.rs +++ b/src/librustc_ty/ty.rs @@ -126,7 +126,7 @@ fn associated_item_from_impl_item_ref( } fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem { - let id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let parent_id = tcx.hir().get_parent_item(id); let parent_def_id = tcx.hir().local_def_id(parent_id); let parent_item = tcx.hir().expect_item(parent_id); @@ -164,7 +164,7 @@ fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem { } fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness { - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let item = tcx.hir().expect_item(hir_id); if let hir::ItemKind::Impl { defaultness, .. } = item.kind { defaultness @@ -198,7 +198,7 @@ fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AdtSizedConstrain } fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] { - let id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let item = tcx.hir().expect_item(id); match item.kind { hir::ItemKind::Trait(.., ref trait_item_refs) => tcx.arena.alloc_from_iter( @@ -268,7 +268,7 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> { let body_id = def_id .as_local() - .map(|def_id| tcx.hir().as_local_hir_id(def_id)) + .map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)) .map_or(hir::CRATE_HIR_ID, |id| { tcx.hir().maybe_body_owned_by(id).map_or(id, |body| body.hir_id) }); @@ -360,7 +360,7 @@ fn issue33140_self_ty(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Ty<'_>> { /// Check if a function is async. fn asyncness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::IsAsync { - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let node = tcx.hir().get(hir_id); diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 2be100ae33662..b6730328a28a6 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -170,7 +170,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { def: Option<&ty::GenericParamDef>, ) -> ty::Region<'tcx> { let tcx = self.tcx(); - let lifetime_name = |def_id| tcx.hir().name(tcx.hir().as_local_hir_id(def_id)); + let lifetime_name = |def_id| tcx.hir().name(tcx.hir().local_def_id_to_hir_id(def_id)); let r = match tcx.named_region(lifetime.hir_id) { Some(rl::Region::Static) => tcx.lifetimes.re_static, @@ -2099,7 +2099,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { debug!("find_bound_for_assoc_item: predicates={:#?}", predicates); - let param_hir_id = tcx.hir().as_local_hir_id(ty_param_def_id); + let param_hir_id = tcx.hir().local_def_id_to_hir_id(ty_param_def_id); let param_name = tcx.hir().ty_param_name(param_hir_id); self.one_bound_for_assoc_type( || { @@ -2483,7 +2483,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { let parent_def_id = def_id .and_then(|def_id| { - def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)) + def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)) }) .map(|hir_id| tcx.hir().get_parent_did(hir_id).to_def_id()); @@ -2819,7 +2819,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { assert_eq!(opt_self_ty, None); self.prohibit_generics(path.segments); - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let item_id = tcx.hir().get_parent_node(hir_id); let item_def_id = tcx.hir().local_def_id(item_id); let generics = tcx.generics_of(item_def_id); diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index db8cdfc5b20d6..f0802c45ae030 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -1502,7 +1502,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { let ty = AstConv::ast_ty_to_ty(fcx, ty); // Get the `impl Trait`'s `DefId`. if let ty::Opaque(def_id, _) = ty.kind { - let hir_id = fcx.tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = fcx.tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); // Get the `impl Trait`'s `Item` so that we can get its trait bounds and // get the `Trait`'s `DefId`. if let hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds, .. }) = diff --git a/src/librustc_typeck/check/compare_method.rs b/src/librustc_typeck/check/compare_method.rs index b739e2fe1fbc4..7adcd7b472e0d 100644 --- a/src/librustc_typeck/check/compare_method.rs +++ b/src/librustc_typeck/check/compare_method.rs @@ -77,7 +77,7 @@ fn compare_predicate_entailment<'tcx>( // This node-id should be used for the `body_id` field on each // `ObligationCause` (and the `FnCtxt`). This is what // `regionck_item` expects. - let impl_m_hir_id = tcx.hir().as_local_hir_id(impl_m.def_id.expect_local()); + let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m.def_id.expect_local()); // We sometimes modify the span further down. let mut cause = ObligationCause::new( @@ -401,7 +401,7 @@ fn extract_spans_for_error_reporting<'a, 'tcx>( trait_sig: ty::FnSig<'tcx>, ) -> (Span, Option<Span>) { let tcx = infcx.tcx; - let impl_m_hir_id = tcx.hir().as_local_hir_id(impl_m.def_id.expect_local()); + let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m.def_id.expect_local()); let (impl_m_output, impl_m_iter) = match tcx.hir().expect_impl_item(impl_m_hir_id).kind { ImplItemKind::Fn(ref impl_m_sig, _) => { (&impl_m_sig.decl.output, impl_m_sig.decl.inputs.iter()) @@ -412,7 +412,7 @@ fn extract_spans_for_error_reporting<'a, 'tcx>( match *terr { TypeError::Mutability => { if let Some(def_id) = trait_m.def_id.as_local() { - let trait_m_hir_id = tcx.hir().as_local_hir_id(def_id); + let trait_m_hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let trait_m_iter = match tcx.hir().expect_trait_item(trait_m_hir_id).kind { TraitItemKind::Fn(ref trait_m_sig, _) => trait_m_sig.decl.inputs.iter(), _ => bug!("{:?} is not a TraitItemKind::Fn", trait_m), @@ -440,7 +440,7 @@ fn extract_spans_for_error_reporting<'a, 'tcx>( } TypeError::Sorts(ExpectedFound { .. }) => { if let Some(def_id) = trait_m.def_id.as_local() { - let trait_m_hir_id = tcx.hir().as_local_hir_id(def_id); + let trait_m_hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let (trait_m_output, trait_m_iter) = match tcx.hir().expect_trait_item(trait_m_hir_id).kind { TraitItemKind::Fn(ref trait_m_sig, _) => { @@ -589,7 +589,7 @@ fn compare_number_of_generics<'tcx>( err_occurred = true; let (trait_spans, impl_trait_spans) = if let Some(def_id) = trait_.def_id.as_local() { - let trait_hir_id = tcx.hir().as_local_hir_id(def_id); + let trait_hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let trait_item = tcx.hir().expect_trait_item(trait_hir_id); if trait_item.generics.params.is_empty() { (Some(vec![trait_item.generics.span]), vec![]) @@ -614,7 +614,7 @@ fn compare_number_of_generics<'tcx>( (trait_span.map(|s| vec![s]), vec![]) }; - let impl_hir_id = tcx.hir().as_local_hir_id(impl_.def_id.expect_local()); + let impl_hir_id = tcx.hir().local_def_id_to_hir_id(impl_.def_id.expect_local()); let impl_item = tcx.hir().expect_impl_item(impl_hir_id); let impl_item_impl_trait_spans: Vec<Span> = impl_item .generics @@ -706,7 +706,7 @@ fn compare_number_of_method_arguments<'tcx>( let impl_number_args = impl_m_fty.inputs().skip_binder().len(); if trait_number_args != impl_number_args { let trait_span = if let Some(def_id) = trait_m.def_id.as_local() { - let trait_id = tcx.hir().as_local_hir_id(def_id); + let trait_id = tcx.hir().local_def_id_to_hir_id(def_id); match tcx.hir().expect_trait_item(trait_id).kind { TraitItemKind::Fn(ref trait_m_sig, _) => { let pos = if trait_number_args > 0 { trait_number_args - 1 } else { 0 }; @@ -729,7 +729,7 @@ fn compare_number_of_method_arguments<'tcx>( } else { trait_item_span }; - let impl_m_hir_id = tcx.hir().as_local_hir_id(impl_m.def_id.expect_local()); + let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m.def_id.expect_local()); let impl_span = match tcx.hir().expect_impl_item(impl_m_hir_id).kind { ImplItemKind::Fn(ref impl_m_sig, _) => { let pos = if impl_number_args > 0 { impl_number_args - 1 } else { 0 }; @@ -811,7 +811,7 @@ fn compare_synthetic_generics<'tcx>( impl_m_type_params.zip(trait_m_type_params) { if impl_synthetic != trait_synthetic { - let impl_hir_id = tcx.hir().as_local_hir_id(impl_def_id.expect_local()); + let impl_hir_id = tcx.hir().local_def_id_to_hir_id(impl_def_id.expect_local()); let impl_span = tcx.hir().span(impl_hir_id); let trait_span = tcx.def_span(trait_def_id); let mut err = struct_span_err!( @@ -832,10 +832,10 @@ fn compare_synthetic_generics<'tcx>( // FIXME: this is obviously suboptimal since the name can already be used // as another generic argument let new_name = tcx.sess.source_map().span_to_snippet(trait_span).ok()?; - let trait_m = tcx.hir().as_local_hir_id(trait_m.def_id.as_local()?); + let trait_m = tcx.hir().local_def_id_to_hir_id(trait_m.def_id.as_local()?); let trait_m = tcx.hir().trait_item(hir::TraitItemId { hir_id: trait_m }); - let impl_m = tcx.hir().as_local_hir_id(impl_m.def_id.as_local()?); + let impl_m = tcx.hir().local_def_id_to_hir_id(impl_m.def_id.as_local()?); let impl_m = tcx.hir().impl_item(hir::ImplItemId { hir_id: impl_m }); // in case there are no generics, take the spot between the function name @@ -869,7 +869,7 @@ fn compare_synthetic_generics<'tcx>( (None, Some(hir::SyntheticTyParamKind::ImplTrait)) => { err.span_label(impl_span, "expected `impl Trait`, found generic parameter"); (|| { - let impl_m = tcx.hir().as_local_hir_id(impl_m.def_id.as_local()?); + let impl_m = tcx.hir().local_def_id_to_hir_id(impl_m.def_id.as_local()?); let impl_m = tcx.hir().impl_item(hir::ImplItemId { hir_id: impl_m }); let input_tys = match impl_m.kind { hir::ImplItemKind::Fn(ref sig, _) => sig.decl.inputs, @@ -962,7 +962,7 @@ crate fn compare_const_impl<'tcx>( // Create a parameter environment that represents the implementation's // method. - let impl_c_hir_id = tcx.hir().as_local_hir_id(impl_c.def_id.expect_local()); + let impl_c_hir_id = tcx.hir().local_def_id_to_hir_id(impl_c.def_id.expect_local()); // Compute placeholder form of impl and trait const tys. let impl_ty = tcx.type_of(impl_c.def_id); @@ -1011,7 +1011,7 @@ crate fn compare_const_impl<'tcx>( ); let trait_c_hir_id = - trait_c.def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id)); + trait_c.def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)); let trait_c_span = trait_c_hir_id.map(|trait_c_hir_id| { // Add a label to the Span containing just the type of the const match tcx.hir().expect_trait_item(trait_c_hir_id).kind { @@ -1101,7 +1101,7 @@ fn compare_type_predicate_entailment<'tcx>( // This `HirId` should be used for the `body_id` field on each // `ObligationCause` (and the `FnCtxt`). This is what // `regionck_item` expects. - let impl_ty_hir_id = tcx.hir().as_local_hir_id(impl_ty.def_id.expect_local()); + let impl_ty_hir_id = tcx.hir().local_def_id_to_hir_id(impl_ty.def_id.expect_local()); let cause = ObligationCause::new( impl_ty_span, impl_ty_hir_id, @@ -1240,7 +1240,7 @@ fn compare_projection_bounds<'tcx>( let infcx = &inh.infcx; let mut selcx = traits::SelectionContext::new(&infcx); - let impl_ty_hir_id = tcx.hir().as_local_hir_id(impl_ty.def_id.expect_local()); + let impl_ty_hir_id = tcx.hir().local_def_id_to_hir_id(impl_ty.def_id.expect_local()); let normalize_cause = traits::ObligationCause::misc(impl_ty_span, impl_ty_hir_id); let cause = ObligationCause::new( impl_ty_span, diff --git a/src/librustc_typeck/check/dropck.rs b/src/librustc_typeck/check/dropck.rs index 9ef9164191675..434886538fb4d 100644 --- a/src/librustc_typeck/check/dropck.rs +++ b/src/librustc_typeck/check/dropck.rs @@ -70,7 +70,7 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>( drop_impl_ty: Ty<'tcx>, self_type_did: DefId, ) -> Result<(), ErrorReported> { - let drop_impl_hir_id = tcx.hir().as_local_hir_id(drop_impl_did); + let drop_impl_hir_id = tcx.hir().local_def_id_to_hir_id(drop_impl_did); // check that the impl type can be made to match the trait type. @@ -183,7 +183,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>( // absent. So we report an error that the Drop impl injected a // predicate that is not present on the struct definition. - let self_type_hir_id = tcx.hir().as_local_hir_id(self_type_did); + let self_type_hir_id = tcx.hir().local_def_id_to_hir_id(self_type_did); // We can assume the predicates attached to struct/enum definition // hold. diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index e7eaca62bdd8e..e88f13a1f3ab6 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -1606,7 +1606,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } let param_def_id = generic_param.def_id; let param_hir_id = match param_def_id.as_local() { - Some(x) => self.tcx.hir().as_local_hir_id(x), + Some(x) => self.tcx.hir().local_def_id_to_hir_id(x), None => return, }; let param_span = self.tcx.hir().span(param_hir_id); diff --git a/src/librustc_typeck/check/method/probe.rs b/src/librustc_typeck/check/method/probe.rs index 106df847a05cf..9078dc40041aa 100644 --- a/src/librustc_typeck/check/method/probe.rs +++ b/src/librustc_typeck/check/method/probe.rs @@ -649,6 +649,10 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { self.assemble_inherent_impl_for_primitive(lang_def_id); } } + ty::Array(_, _) => { + let lang_def_id = lang_items.array_impl(); + self.assemble_inherent_impl_for_primitive(lang_def_id); + } ty::RawPtr(ty::TypeAndMut { ty: _, mutbl }) => { let (lang_def_id1, lang_def_id2) = match mutbl { hir::Mutability::Not => { diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index e69102d1995d3..e941c844a6d90 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -579,7 +579,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { { if let ty::Adt(def, _) = p.trait_ref.self_ty().kind { let node = def.did.as_local().map(|def_id| { - self.tcx.hir().get(self.tcx.hir().as_local_hir_id(def_id)) + self.tcx + .hir() + .get(self.tcx.hir().local_def_id_to_hir_id(def_id)) }); if let Some(hir::Node::Item(hir::Item { kind, .. })) = node { if let Some(g) = kind.generics() { @@ -859,7 +861,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { candidates: Vec<DefId>, ) { let module_did = self.tcx.parent_module(self.body_id); - let module_id = self.tcx.hir().as_local_hir_id(module_did); + let module_id = self.tcx.hir().local_def_id_to_hir_id(module_did); let krate = self.tcx.hir().krate(); let (span, found_use) = UsePlacementFinder::check(self.tcx, krate, module_id); if let Some(span) = span { @@ -975,7 +977,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let id = item .def_id .as_local() - .map(|def_id| self.tcx.hir().as_local_hir_id(def_id)); + .map(|def_id| self.tcx.hir().local_def_id_to_hir_id(def_id)); if let Some(hir::Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Fn(fn_sig, method), .. @@ -1062,7 +1064,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let type_param = generics.type_param(param, self.tcx); let hir = &self.tcx.hir(); if let Some(def_id) = type_param.def_id.as_local() { - let id = hir.as_local_hir_id(def_id); + let id = hir.local_def_id_to_hir_id(def_id); // Get the `hir::Param` to verify whether it already has any bounds. // We do this to avoid suggesting code that ends up as `T: FooBar`, // instead we suggest `T: Foo + Bar` in that case. diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index a40b6860f7765..66400743e9528 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -974,7 +974,7 @@ fn typeck<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &ty::TypeckResults<'tc /// Currently only used for type inference of `static`s and `const`s to avoid type cycle errors. fn diagnostic_only_typeck<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &ty::TypeckResults<'tcx> { let fallback = move || { - let span = tcx.hir().span(tcx.hir().as_local_hir_id(def_id)); + let span = tcx.hir().span(tcx.hir().local_def_id_to_hir_id(def_id)); tcx.ty_error_with_message(span, "diagnostic only typeck table used") }; typeck_with_fallback(tcx, def_id, fallback) @@ -992,7 +992,7 @@ fn typeck_with_fallback<'tcx>( return tcx.typeck(outer_def_id); } - let id = tcx.hir().as_local_hir_id(def_id); + let id = tcx.hir().local_def_id_to_hir_id(def_id); let span = tcx.hir().span(id); // Figure out what primary body this item has. @@ -1333,7 +1333,7 @@ fn check_fn<'a, 'tcx>( } let outer_def_id = tcx.closure_base_def_id(hir.local_def_id(fn_id).to_def_id()).expect_local(); - let outer_hir_id = hir.as_local_hir_id(outer_def_id); + let outer_hir_id = hir.local_def_id_to_hir_id(outer_def_id); GatherLocalsVisitor { fcx: &fcx, parent_id: outer_hir_id }.visit_body(body); // C-variadic fns also have a `VaList` input that's not listed in `fn_sig` @@ -1444,7 +1444,7 @@ fn check_fn<'a, 'tcx>( // Check that the main return type implements the termination trait. if let Some(term_id) = tcx.lang_items().termination() { if let Some((def_id, EntryFnType::Main)) = tcx.entry_fn(LOCAL_CRATE) { - let main_id = hir.as_local_hir_id(def_id); + let main_id = hir.local_def_id_to_hir_id(def_id); if main_id == fn_id { let substs = tcx.mk_substs_trait(declared_ret_ty, &[]); let trait_ref = ty::TraitRef::new(term_id, substs); @@ -1622,7 +1622,7 @@ fn check_opaque<'tcx>( /// Checks that an opaque type does not use `Self` or `T::Foo` projections that would result /// in "inheriting lifetimes". fn check_opaque_for_inheriting_lifetimes(tcx: TyCtxt<'tcx>, def_id: LocalDefId, span: Span) { - let item = tcx.hir().expect_item(tcx.hir().as_local_hir_id(def_id)); + let item = tcx.hir().expect_item(tcx.hir().local_def_id_to_hir_id(def_id)); debug!( "check_opaque_for_inheriting_lifetimes: def_id={:?} span={:?} item={:?}", def_id, span, item @@ -1729,7 +1729,7 @@ fn get_owner_return_paths( tcx: TyCtxt<'tcx>, def_id: LocalDefId, ) -> Option<(hir::HirId, ReturnsVisitor<'tcx>)> { - let hir_id = tcx.hir().as_local_hir_id(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let id = tcx.hir().get_parent_item(hir_id); tcx.hir() .find(id) @@ -1833,7 +1833,7 @@ fn binding_opaque_type_cycle_error( let mut err = struct_span_err!(tcx.sess, span, E0720, "cannot resolve opaque type"); err.span_label(span, "cannot resolve opaque type"); // Find the the owner that declared this `impl Trait` type. - let hir_id = tcx.hir().as_local_hir_id(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let mut prev_hir_id = hir_id; let mut hir_id = tcx.hir().get_parent_node(hir_id); while let Some(node) = tcx.hir().find(hir_id) { @@ -1858,7 +1858,7 @@ fn binding_opaque_type_cycle_error( source: hir::LocalSource::Normal, .. }) => { - let hir_id = tcx.hir().as_local_hir_id(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let typeck_results = tcx.typeck(tcx.hir().local_def_id(tcx.hir().get_parent_item(hir_id))); if let Some(ty) = typeck_results.node_type_opt(expr.hir_id) { @@ -2874,7 +2874,7 @@ pub fn check_enum<'tcx>( // Check for duplicate discriminant values if let Some(i) = disr_vals.iter().position(|&x| x.val == discr.val) { let variant_did = def.variants[VariantIdx::new(i)].def_id; - let variant_i_hir_id = tcx.hir().as_local_hir_id(variant_did.expect_local()); + let variant_i_hir_id = tcx.hir().local_def_id_to_hir_id(variant_did.expect_local()); let variant_i = tcx.hir().expect_variant(variant_i_hir_id); let i_span = match variant_i.disr_expr { Some(ref expr) => tcx.hir().span(expr.hir_id), @@ -2935,7 +2935,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> { fn get_type_parameter_bounds(&self, _: Span, def_id: DefId) -> ty::GenericPredicates<'tcx> { let tcx = self.tcx; - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let item_id = tcx.hir().ty_param_owner(hir_id); let item_def_id = tcx.hir().local_def_id(item_id); let generics = tcx.generics_of(item_def_id); diff --git a/src/librustc_typeck/check/op.rs b/src/librustc_typeck/check/op.rs index fd516c88ec61a..66fb01a54f574 100644 --- a/src/librustc_typeck/check/op.rs +++ b/src/librustc_typeck/check/op.rs @@ -933,7 +933,7 @@ fn suggest_constraining_param( let param_def_id = generics.type_param(&p, tcx).def_id; if let Some(generics) = param_def_id .as_local() - .map(|id| hir.as_local_hir_id(id)) + .map(|id| hir.local_def_id_to_hir_id(id)) .and_then(|id| hir.find(hir.get_parent_item(id))) .as_ref() .and_then(|node| node.generics()) diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index 50d9a1ebd2c24..2bde5d2c78cc1 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -71,7 +71,7 @@ impl<'tcx> CheckWfFcxBuilder<'tcx> { /// not included it frequently leads to confusing errors in fn bodies. So it's better to check /// the types first. pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) { - let hir_id = tcx.hir().as_local_hir_id(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let item = tcx.hir().expect_item(hir_id); debug!( @@ -190,7 +190,7 @@ pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) { } pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) { - let hir_id = tcx.hir().as_local_hir_id(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let trait_item = tcx.hir().expect_trait_item(hir_id); let method_sig = match trait_item.kind { @@ -264,7 +264,7 @@ fn check_object_unsafe_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem } pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: LocalDefId) { - let hir_id = tcx.hir().as_local_hir_id(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let impl_item = tcx.hir().expect_impl_item(hir_id); let method_sig = match impl_item.kind { @@ -902,7 +902,7 @@ fn check_opaque_types<'fcx, 'tcx>( let generics = tcx.generics_of(def_id); let opaque_hir_id = if let Some(local_id) = def_id.as_local() { - tcx.hir().as_local_hir_id(local_id) + tcx.hir().local_def_id_to_hir_id(local_id) } else { // Opaque types from other crates won't have defining uses in this crate. return ty; diff --git a/src/librustc_typeck/check/writeback.rs b/src/librustc_typeck/check/writeback.rs index 82ee48f0b5346..50e2d6a94bb71 100644 --- a/src/librustc_typeck/check/writeback.rs +++ b/src/librustc_typeck/check/writeback.rs @@ -433,7 +433,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> { fn visit_opaque_types(&mut self, span: Span) { for (&def_id, opaque_defn) in self.fcx.opaque_types.borrow().iter() { - let hir_id = self.tcx().hir().as_local_hir_id(def_id.expect_local()); + let hir_id = self.tcx().hir().local_def_id_to_hir_id(def_id.expect_local()); let instantiated_ty = self.resolve(&opaque_defn.concrete_ty, &hir_id); debug_assert!(!instantiated_ty.has_escaping_bound_vars()); diff --git a/src/librustc_typeck/check_unused.rs b/src/librustc_typeck/check_unused.rs index 81daf064bb368..4fda8932e213b 100644 --- a/src/librustc_typeck/check_unused.rs +++ b/src/librustc_typeck/check_unused.rs @@ -88,7 +88,7 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) { // Note that if we carry through to the `extern_mod_stmt_cnum` query // below it'll cause a panic because `def_id` is actually bogus at this // point in time otherwise. - if tcx.hir().find(tcx.hir().as_local_hir_id(def_id)).is_none() { + if tcx.hir().find(tcx.hir().local_def_id_to_hir_id(def_id)).is_none() { return false; } true @@ -113,7 +113,7 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) { for extern_crate in &crates_to_lint { let def_id = extern_crate.def_id.expect_local(); - let id = tcx.hir().as_local_hir_id(def_id); + let id = tcx.hir().local_def_id_to_hir_id(def_id); let item = tcx.hir().expect_item(id); // If the crate is fully unused, we suggest removing it altogether. diff --git a/src/librustc_typeck/coherence/builtin.rs b/src/librustc_typeck/coherence/builtin.rs index 8c6161a626473..56a737964c047 100644 --- a/src/librustc_typeck/coherence/builtin.rs +++ b/src/librustc_typeck/coherence/builtin.rs @@ -54,7 +54,7 @@ fn visit_implementation_of_drop(tcx: TyCtxt<'_>, impl_did: LocalDefId) { return; } - let impl_hir_id = tcx.hir().as_local_hir_id(impl_did); + let impl_hir_id = tcx.hir().local_def_id_to_hir_id(impl_did); let sp = match tcx.hir().expect_item(impl_hir_id).kind { ItemKind::Impl { self_ty, .. } => self_ty.span, _ => bug!("expected Drop impl item"), @@ -73,7 +73,7 @@ fn visit_implementation_of_drop(tcx: TyCtxt<'_>, impl_did: LocalDefId) { fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) { debug!("visit_implementation_of_copy: impl_did={:?}", impl_did); - let impl_hir_id = tcx.hir().as_local_hir_id(impl_did); + let impl_hir_id = tcx.hir().local_def_id_to_hir_id(impl_did); let self_type = tcx.type_of(impl_did); debug!("visit_implementation_of_copy: self_type={:?} (bound)", self_type); @@ -146,7 +146,7 @@ fn visit_implementation_of_coerce_unsized(tcx: TyCtxt<'tcx>, impl_did: LocalDefI fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDefId) { debug!("visit_implementation_of_dispatch_from_dyn: impl_did={:?}", impl_did); - let impl_hir_id = tcx.hir().as_local_hir_id(impl_did); + let impl_hir_id = tcx.hir().local_def_id_to_hir_id(impl_did); let span = tcx.hir().span(impl_hir_id); let dispatch_from_dyn_trait = tcx.require_lang_item(DispatchFromDynTraitLangItem, Some(span)); @@ -315,7 +315,7 @@ pub fn coerce_unsized_info(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUnsizedI debug!("compute_coerce_unsized_info(impl_did={:?})", impl_did); // this provider should only get invoked for local def-ids - let impl_hir_id = tcx.hir().as_local_hir_id(impl_did.expect_local()); + let impl_hir_id = tcx.hir().local_def_id_to_hir_id(impl_did.expect_local()); let span = tcx.hir().span(impl_hir_id); let coerce_unsized_trait = tcx.require_lang_item(CoerceUnsizedTraitLangItem, Some(span)); diff --git a/src/librustc_typeck/coherence/inherent_impls.rs b/src/librustc_typeck/coherence/inherent_impls.rs index 93ee87f6c572e..cd7429f166f26 100644 --- a/src/librustc_typeck/coherence/inherent_impls.rs +++ b/src/librustc_typeck/coherence/inherent_impls.rs @@ -112,6 +112,16 @@ impl ItemLikeVisitor<'v> for InherentCollect<'tcx> { item.span, ); } + ty::Array(_, _) => { + self.check_primitive_impl( + def_id, + lang_items.array_impl(), + None, + "array", + "[T; N]", + item.span, + ); + } ty::RawPtr(ty::TypeAndMut { ty: inner, mutbl: hir::Mutability::Not }) if matches!(inner.kind, ty::Slice(_)) => { diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index b47ef34600404..da1f3ea62f239 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -275,7 +275,7 @@ impl ItemCtxt<'tcx> { } pub fn hir_id(&self) -> hir::HirId { - self.tcx.hir().as_local_hir_id(self.item_def_id.expect_local()) + self.tcx.hir().local_def_id_to_hir_id(self.item_def_id.expect_local()) } pub fn node(&self) -> hir::Node<'tcx> { @@ -490,7 +490,7 @@ fn type_param_predicates( // written inline like `<T: Foo>` or in a where-clause like // `where T: Foo`. - let param_id = tcx.hir().as_local_hir_id(def_id); + let param_id = tcx.hir().local_def_id_to_hir_id(def_id); let param_owner = tcx.hir().ty_param_owner(param_id); let param_owner_def_id = tcx.hir().local_def_id(param_owner); let generics = tcx.generics_of(param_owner_def_id); @@ -512,7 +512,7 @@ fn type_param_predicates( .unwrap_or_default(); let mut extend = None; - let item_hir_id = tcx.hir().as_local_hir_id(item_def_id.expect_local()); + let item_hir_id = tcx.hir().local_def_id_to_hir_id(item_def_id.expect_local()); let ast_generics = match tcx.hir().get(item_hir_id) { Node::TraitItem(item) => &item.generics, @@ -824,7 +824,7 @@ fn convert_variant( parent_did: LocalDefId, ) -> ty::VariantDef { let mut seen_fields: FxHashMap<Ident, Span> = Default::default(); - let hir_id = tcx.hir().as_local_hir_id(variant_did.unwrap_or(parent_did)); + let hir_id = tcx.hir().local_def_id_to_hir_id(variant_did.unwrap_or(parent_did)); let fields = def .fields() .iter() @@ -878,7 +878,7 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::AdtDef { use rustc_hir::*; let def_id = def_id.expect_local(); - let hir_id = tcx.hir().as_local_hir_id(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let item = match tcx.hir().get(hir_id) { Node::Item(item) => item, _ => bug!(), @@ -965,7 +965,7 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::AdtDef { /// the transitive super-predicates are converted. fn super_predicates_of(tcx: TyCtxt<'_>, trait_def_id: DefId) -> ty::GenericPredicates<'_> { debug!("super_predicates(trait_def_id={:?})", trait_def_id); - let trait_hir_id = tcx.hir().as_local_hir_id(trait_def_id.expect_local()); + let trait_hir_id = tcx.hir().local_def_id_to_hir_id(trait_def_id.expect_local()); let item = match tcx.hir().get(trait_hir_id) { Node::Item(item) => item, @@ -1016,7 +1016,7 @@ fn super_predicates_of(tcx: TyCtxt<'_>, trait_def_id: DefId) -> ty::GenericPredi } fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef { - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let item = tcx.hir().expect_item(hir_id); let (is_auto, unsafety) = match item.kind { @@ -1194,7 +1194,7 @@ impl<'v> Visitor<'v> for AnonConstInParamListDetector { fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics { use rustc_hir::*; - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let node = tcx.hir().get(hir_id); let parent_def_id = match node { @@ -1499,7 +1499,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> { use rustc_hir::*; let def_id = def_id.expect_local(); - let hir_id = tcx.hir().as_local_hir_id(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let icx = ItemCtxt::new(tcx, def_id.to_def_id()); @@ -1597,7 +1597,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> { fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::TraitRef<'_>> { let icx = ItemCtxt::new(tcx, def_id); - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); match tcx.hir().expect_item(hir_id).kind { hir::ItemKind::Impl { ref of_trait, .. } => of_trait.as_ref().map(|ast_trait_ref| { let selfty = tcx.type_of(def_id); @@ -1608,7 +1608,7 @@ fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::TraitRef<'_>> { } fn impl_polarity(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ImplPolarity { - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl); let item = tcx.hir().expect_item(hir_id); match &item.kind { @@ -1738,7 +1738,7 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat } } - let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let node = tcx.hir().get(hir_id); let mut is_trait = None; @@ -2590,7 +2590,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { if !codegen_fn_attrs.no_sanitize.is_empty() { if codegen_fn_attrs.inline == InlineAttr::Always { if let (Some(no_sanitize_span), Some(inline_span)) = (no_sanitize_span, inline_span) { - let hir_id = tcx.hir().as_local_hir_id(id.expect_local()); + let hir_id = tcx.hir().local_def_id_to_hir_id(id.expect_local()); tcx.struct_span_lint_hir( lint::builtin::INLINE_NO_SANITIZE, hir_id, @@ -2718,7 +2718,7 @@ fn check_target_feature_safe_fn(tcx: TyCtxt<'_>, id: DefId, attr_span: Span) { /// Checks the function annotated with `#[target_feature]` is not a safe /// trait method implementation, reporting an error if it is. fn check_target_feature_trait_unsafe(tcx: TyCtxt<'_>, id: LocalDefId, attr_span: Span) { - let hir_id = tcx.hir().as_local_hir_id(id); + let hir_id = tcx.hir().local_def_id_to_hir_id(id); let node = tcx.hir().get(hir_id); if let Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) = node { let parent_id = tcx.hir().get_parent_item(hir_id); diff --git a/src/librustc_typeck/collect/type_of.rs b/src/librustc_typeck/collect/type_of.rs index 17444c6d0ac95..f1478c8c952f8 100644 --- a/src/librustc_typeck/collect/type_of.rs +++ b/src/librustc_typeck/collect/type_of.rs @@ -23,7 +23,7 @@ use super::{bad_placeholder_type, is_suggestable_infer_ty}; pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<DefId> { use hir::*; - let hir_id = tcx.hir().as_local_hir_id(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); if let Node::AnonConst(_) = tcx.hir().get(hir_id) { let parent_node_id = tcx.hir().get_parent_node(hir_id); @@ -138,7 +138,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { let def_id = def_id.expect_local(); use rustc_hir::*; - let hir_id = tcx.hir().as_local_hir_id(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let icx = ItemCtxt::new(tcx, def_id.to_def_id()); @@ -564,7 +564,7 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> { } } - let hir_id = tcx.hir().as_local_hir_id(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let scope = tcx.hir().get_defining_scope(hir_id); let mut locator = ConstraintLocator { def_id: def_id.to_def_id(), tcx, found: None }; @@ -617,7 +617,7 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> { /// by the time we borrow check, and it's not clear how we should handle /// those. fn let_position_impl_trait_type(tcx: TyCtxt<'_>, opaque_ty_id: LocalDefId) -> Ty<'_> { - let scope = tcx.hir().get_defining_scope(tcx.hir().as_local_hir_id(opaque_ty_id)); + let scope = tcx.hir().get_defining_scope(tcx.hir().local_def_id_to_hir_id(opaque_ty_id)); let scope_def_id = tcx.hir().local_def_id(scope); let opaque_ty_def_id = opaque_ty_id.to_def_id(); @@ -653,7 +653,7 @@ fn let_position_impl_trait_type(tcx: TyCtxt<'_>, opaque_ty_id: LocalDefId) -> Ty if concrete_ty.has_erased_regions() { // FIXME(impl_trait_in_bindings) Handle this case. tcx.sess.span_fatal( - tcx.hir().span(tcx.hir().as_local_hir_id(opaque_ty_id)), + tcx.hir().span(tcx.hir().local_def_id_to_hir_id(opaque_ty_id)), "lifetimes in impl Trait types in bindings are not currently supported", ); } diff --git a/src/librustc_typeck/impl_wf_check/min_specialization.rs b/src/librustc_typeck/impl_wf_check/min_specialization.rs index 8257c6ce92547..3746e5778aacd 100644 --- a/src/librustc_typeck/impl_wf_check/min_specialization.rs +++ b/src/librustc_typeck/impl_wf_check/min_specialization.rs @@ -336,7 +336,7 @@ fn check_predicates<'tcx>( if let Some(obligations) = wf::obligations( infcx, tcx.param_env(impl1_def_id), - tcx.hir().as_local_hir_id(impl1_def_id), + tcx.hir().local_def_id_to_hir_id(impl1_def_id), arg, span, ) { diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index 49843fa43dd8c..41639c53b7991 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -153,7 +153,7 @@ fn require_same_types<'tcx>( } fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: LocalDefId) { - let main_id = tcx.hir().as_local_hir_id(main_def_id); + let main_id = tcx.hir().local_def_id_to_hir_id(main_def_id); let main_span = tcx.def_span(main_def_id); let main_t = tcx.type_of(main_def_id); match main_t.kind { @@ -249,7 +249,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: LocalDefId) { } fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: LocalDefId) { - let start_id = tcx.hir().as_local_hir_id(start_def_id); + let start_id = tcx.hir().local_def_id_to_hir_id(start_def_id); let start_span = tcx.def_span(start_def_id); let start_t = tcx.type_of(start_def_id); match start_t.kind { diff --git a/src/librustc_typeck/outlives/implicit_infer.rs b/src/librustc_typeck/outlives/implicit_infer.rs index 15c72f8704f65..762d4216f7060 100644 --- a/src/librustc_typeck/outlives/implicit_infer.rs +++ b/src/librustc_typeck/outlives/implicit_infer.rs @@ -57,7 +57,7 @@ impl<'cx, 'tcx> ItemLikeVisitor<'tcx> for InferVisitor<'cx, 'tcx> { debug!("InferVisitor::visit_item(item={:?})", item_did); - let hir_id = self.tcx.hir().as_local_hir_id(item_did); + let hir_id = self.tcx.hir().local_def_id_to_hir_id(item_did); let item = match self.tcx.hir().get(hir_id) { Node::Item(item) => item, _ => bug!(), diff --git a/src/librustc_typeck/outlives/mod.rs b/src/librustc_typeck/outlives/mod.rs index 5dc7ac9fa0d4e..94926f480e23e 100644 --- a/src/librustc_typeck/outlives/mod.rs +++ b/src/librustc_typeck/outlives/mod.rs @@ -18,7 +18,7 @@ pub fn provide(providers: &mut Providers) { } fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[(ty::Predicate<'_>, Span)] { - let id = tcx.hir().as_local_hir_id(item_def_id.expect_local()); + let id = tcx.hir().local_def_id_to_hir_id(item_def_id.expect_local()); match tcx.hir().get(id) { Node::Item(item) => match item.kind { diff --git a/src/librustc_typeck/variance/constraints.rs b/src/librustc_typeck/variance/constraints.rs index b810c9824ce66..535530a2ed494 100644 --- a/src/librustc_typeck/variance/constraints.rs +++ b/src/librustc_typeck/variance/constraints.rs @@ -137,7 +137,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { return; } - let id = tcx.hir().as_local_hir_id(def_id); + let id = tcx.hir().local_def_id_to_hir_id(def_id); let inferred_start = self.terms_cx.inferred_starts[&id]; let current_item = &CurrentItem { inferred_start }; match tcx.type_of(def_id).kind { @@ -375,7 +375,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { } let (local, remote) = if let Some(def_id) = def_id.as_local() { - let id = self.tcx().hir().as_local_hir_id(def_id); + let id = self.tcx().hir().local_def_id_to_hir_id(def_id); (Some(self.terms_cx.inferred_starts[&id]), None) } else { (None, Some(self.tcx().variances_of(def_id))) diff --git a/src/librustc_typeck/variance/mod.rs b/src/librustc_typeck/variance/mod.rs index b307363dc3ab0..a893f69c48ada 100644 --- a/src/librustc_typeck/variance/mod.rs +++ b/src/librustc_typeck/variance/mod.rs @@ -39,7 +39,7 @@ fn crate_variances(tcx: TyCtxt<'_>, crate_num: CrateNum) -> CrateVariancesMap<'_ } fn variances_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[ty::Variance] { - let id = tcx.hir().as_local_hir_id(item_def_id.expect_local()); + let id = tcx.hir().local_def_id_to_hir_id(item_def_id.expect_local()); let unsupported = || { // Variance not relevant. span_bug!(tcx.hir().span(id), "asked to compute variance for wrong kind of item") diff --git a/src/librustc_typeck/variance/terms.rs b/src/librustc_typeck/variance/terms.rs index 6e15485756d98..f61a783de694b 100644 --- a/src/librustc_typeck/variance/terms.rs +++ b/src/librustc_typeck/variance/terms.rs @@ -94,7 +94,9 @@ fn lang_items(tcx: TyCtxt<'_>) -> Vec<(hir::HirId, Vec<ty::Variance>)> { all.into_iter() // iterating over (Option<DefId>, Variance) .filter(|&(ref d, _)| d.is_some()) .map(|(d, v)| (d.unwrap(), v)) // (DefId, Variance) - .filter_map(|(d, v)| d.as_local().map(|d| tcx.hir().as_local_hir_id(d)).map(|n| (n, v))) // (HirId, Variance) + .filter_map(|(d, v)| { + d.as_local().map(|d| tcx.hir().local_def_id_to_hir_id(d)).map(|n| (n, v)) + }) // (HirId, Variance) .collect() } diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 1f576a17dd9d6..cb7c62e3a5ac6 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -356,7 +356,7 @@ pub fn build_impl( } let for_ = if let Some(did) = did.as_local() { - let hir_id = tcx.hir().as_local_hir_id(did); + let hir_id = tcx.hir().local_def_id_to_hir_id(did); match tcx.hir().expect_item(hir_id).kind { hir::ItemKind::Impl { self_ty, .. } => self_ty.clean(cx), _ => panic!("did given to build_impl was not an impl"), @@ -377,7 +377,7 @@ pub fn build_impl( let predicates = tcx.explicit_predicates_of(did); let (trait_items, generics) = if let Some(did) = did.as_local() { - let hir_id = tcx.hir().as_local_hir_id(did); + let hir_id = tcx.hir().local_def_id_to_hir_id(did); match tcx.hir().expect_item(hir_id).kind { hir::ItemKind::Impl { ref generics, ref items, .. } => ( items.iter().map(|item| tcx.hir().impl_item(item.id).clean(cx)).collect::<Vec<_>>(), @@ -500,7 +500,7 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>) pub fn print_inlined_const(cx: &DocContext<'_>, did: DefId) -> String { if let Some(did) = did.as_local() { - let hir_id = cx.tcx.hir().as_local_hir_id(did); + let hir_id = cx.tcx.hir().local_def_id_to_hir_id(did); rustc_hir_pretty::id_to_string(&cx.tcx.hir(), hir_id) } else { cx.tcx.rendered_const(did) @@ -513,7 +513,7 @@ fn build_const(cx: &DocContext<'_>, did: DefId) -> clean::Constant { expr: print_inlined_const(cx, did), value: clean::utils::print_evaluated_const(cx, did), is_literal: did.as_local().map_or(false, |did| { - clean::utils::is_literal_expr(cx, cx.tcx.hir().as_local_hir_id(did)) + clean::utils::is_literal_expr(cx, cx.tcx.hir().local_def_id_to_hir_id(did)) }), } } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 801d06e610169..3ad357e583cf1 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1382,7 +1382,7 @@ impl Clean<Type> for hir::Ty<'_> { if let Res::Def(DefKind::TyAlias, def_id) = path.res { // Substitute private type aliases if let Some(def_id) = def_id.as_local() { - let hir_id = cx.tcx.hir().as_local_hir_id(def_id); + let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id); if !cx.renderinfo.borrow().access_levels.is_exported(def_id.to_def_id()) { alias = Some(&cx.tcx.hir().expect_item(hir_id).kind); } diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index c22538f21f69f..a7d03fcabf546 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -388,7 +388,7 @@ pub fn build_deref_target_impls(cx: &DocContext<'_>, items: &[Item], ret: &mut V Bool => tcx.lang_items().bool_impl(), Str => tcx.lang_items().str_impl(), Slice => tcx.lang_items().slice_impl(), - Array => tcx.lang_items().slice_impl(), + Array => tcx.lang_items().array_impl(), Tuple => None, Unit => None, RawPointer => tcx.lang_items().const_ptr_impl(), @@ -471,7 +471,7 @@ pub fn print_const(cx: &DocContext<'_>, n: &'tcx ty::Const<'_>) -> String { match n.val { ty::ConstKind::Unevaluated(def, _, promoted) => { let mut s = if let Some(def) = def.as_local() { - let hir_id = cx.tcx.hir().as_local_hir_id(def.did); + let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def.did); print_const_expr(cx, cx.tcx.hir().body_owned_by(hir_id)) } else { inline::print_inlined_const(cx, def.did) diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index b13acaae1bf23..c21fd8da0ed9a 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -143,13 +143,13 @@ impl<'tcx> DocContext<'tcx> { def_id } - /// Like the function of the same name on the HIR map, but skips calling it on fake DefIds. + /// Like `hir().local_def_id_to_hir_id()`, but skips calling it on fake DefIds. /// (This avoids a slice-index-out-of-bounds panic.) pub fn as_local_hir_id(&self, def_id: DefId) -> Option<HirId> { if self.all_fake_def_ids.borrow().contains(&def_id) { None } else { - def_id.as_local().map(|def_id| self.tcx.hir().as_local_hir_id(def_id)) + def_id.as_local().map(|def_id| self.tcx.hir().local_def_id_to_hir_id(def_id)) } } @@ -400,7 +400,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt } let hir = tcx.hir(); - let body = hir.body(hir.body_owned_by(hir.as_local_hir_id(def_id))); + let body = hir.body(hir.body_owned_by(hir.local_def_id_to_hir_id(def_id))); debug!("visiting body for {:?}", def_id); EmitIgnoredResolutionErrors::new(tcx).visit_body(body); (rustc_interface::DEFAULT_QUERY_PROVIDERS.typeck)(tcx, def_id) diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 4cfd81ffbce9d..b2589e5b806e8 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -655,7 +655,7 @@ impl<'a, 'b> ExtraInfo<'a, 'b> { (Some(h), _) => h, (None, Some(item_did)) => { match item_did.as_local() { - Some(item_did) => self.tcx.hir().as_local_hir_id(item_did), + Some(item_did) => self.tcx.hir().local_def_id_to_hir_id(item_did), None => { // If non-local, no need to check anything. return; diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 062bd61a7d002..f7fc3579d67a7 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -813,8 +813,8 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { { use rustc_hir::def_id::LOCAL_CRATE; - let hir_src = self.cx.tcx.hir().as_local_hir_id(src_id); - let hir_dst = self.cx.tcx.hir().as_local_hir_id(dst_id); + let hir_src = self.cx.tcx.hir().local_def_id_to_hir_id(src_id); + let hir_dst = self.cx.tcx.hir().local_def_id_to_hir_id(dst_id); if self.cx.tcx.privacy_access_levels(LOCAL_CRATE).is_exported(hir_src) && !self.cx.tcx.privacy_access_levels(LOCAL_CRATE).is_exported(hir_dst) diff --git a/src/librustdoc/passes/collect_trait_impls.rs b/src/librustdoc/passes/collect_trait_impls.rs index 3000afde0c25d..a40b45f9a7e2c 100644 --- a/src/librustdoc/passes/collect_trait_impls.rs +++ b/src/librustdoc/passes/collect_trait_impls.rs @@ -55,6 +55,7 @@ pub fn collect_trait_impls(krate: Crate, cx: &DocContext<'_>) -> Crate { lang_items.bool_impl(), lang_items.char_impl(), lang_items.str_impl(), + lang_items.array_impl(), lang_items.slice_impl(), lang_items.slice_u8_impl(), lang_items.str_alloc_impl(), diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index cf57ffd0b4b30..be80193db8c3b 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -323,7 +323,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { } let res_hir_id = match res_did.as_local() { - Some(n) => tcx.hir().as_local_hir_id(n), + Some(n) => tcx.hir().local_def_id_to_hir_id(n), None => return false, }; diff --git a/src/test/ui/__check/what.fixed b/src/test/ui/__check/what.fixed new file mode 100644 index 0000000000000..c3c6fbf0c43ae --- /dev/null +++ b/src/test/ui/__check/what.fixed @@ -0,0 +1,9 @@ +// check-pass +// run-rustfix +#![warn(unused_parens)] +fn test(_: u32) {} + +fn main() { + test(7); + //~^^^ WARN unnecessary parentheses around function argument +} diff --git a/src/test/ui/__check/what.rs b/src/test/ui/__check/what.rs new file mode 100644 index 0000000000000..1189142806568 --- /dev/null +++ b/src/test/ui/__check/what.rs @@ -0,0 +1,11 @@ +// check-pass +// run-rustfix +#![warn(unused_parens)] +fn test(_: u32) {} + +fn main() { + test(( + 7 + )); + //~^^^ WARN unnecessary parentheses around function argument +} diff --git a/src/test/ui/__check/what.stderr b/src/test/ui/__check/what.stderr new file mode 100644 index 0000000000000..d3ef8e76584ca --- /dev/null +++ b/src/test/ui/__check/what.stderr @@ -0,0 +1,17 @@ +warning: unnecessary parentheses around function argument + --> $DIR/what.rs:7:10 + | +LL | test(( + | __________^ +LL | | 7 +LL | | )); + | |_____^ help: remove these parentheses + | +note: the lint level is defined here + --> $DIR/what.rs:3:9 + | +LL | #![warn(unused_parens)] + | ^^^^^^^^^^^^^ + +warning: 1 warning emitted + diff --git a/src/test/ui/associated-types/bound-lifetime-in-binding-only.elision.stderr b/src/test/ui/associated-types/bound-lifetime-in-binding-only.elision.stderr index 00f44129cc8b7..6c68cc7bc61aa 100644 --- a/src/test/ui/associated-types/bound-lifetime-in-binding-only.elision.stderr +++ b/src/test/ui/associated-types/bound-lifetime-in-binding-only.elision.stderr @@ -5,6 +5,11 @@ LL | fn elision<T: Fn() -> &i32>() { | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from + = note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html +help: consider making the bound lifetime-generic with a new `'a` lifetime + | +LL | fn elision<T: for<'a> Fn() -> &'a i32>() { + | ^^^^^^^ ^^^ help: consider using the `'static` lifetime | LL | fn elision<T: Fn() -> &'static i32>() { diff --git a/src/test/ui/associated-types/bound-lifetime-in-return-only.elision.stderr b/src/test/ui/associated-types/bound-lifetime-in-return-only.elision.stderr index a5242707c7105..93d2f8e7911f0 100644 --- a/src/test/ui/associated-types/bound-lifetime-in-return-only.elision.stderr +++ b/src/test/ui/associated-types/bound-lifetime-in-return-only.elision.stderr @@ -5,6 +5,11 @@ LL | fn elision(_: fn() -> &i32) { | ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from + = note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html +help: consider making the type lifetime-generic with a new `'a` lifetime + | +LL | fn elision(_: for<'a> fn() -> &'a i32) { + | ^^^^^^^ ^^^ help: consider using the `'static` lifetime | LL | fn elision(_: fn() -> &'static i32) { diff --git a/src/test/ui/error-codes/E0106.stderr b/src/test/ui/error-codes/E0106.stderr index a23bcbfd71a56..ac70e887626ab 100644 --- a/src/test/ui/error-codes/E0106.stderr +++ b/src/test/ui/error-codes/E0106.stderr @@ -51,6 +51,15 @@ error[E0106]: missing lifetime specifiers | LL | buzz: Buzz, | ^^^^ expected 2 lifetime parameters + | +help: consider introducing a named lifetime parameter + | +LL | struct Quux<'a> { +LL | baz: Baz, +LL | +LL | +LL | buzz: Buzz<'a, 'a>, + | error: aborting due to 5 previous errors diff --git a/src/test/ui/issues/issue-74739.rs b/src/test/ui/issues/issue-74739.rs new file mode 100644 index 0000000000000..03622358ae1cd --- /dev/null +++ b/src/test/ui/issues/issue-74739.rs @@ -0,0 +1,14 @@ +// compile-flags: -O +// run-pass + +struct Foo { + x: i32, +} + +pub fn main() { + let mut foo = Foo { x: 42 }; + let x = &mut foo.x; + *x = 13; + let y = foo; + assert_eq!(y.x, 13); // used to print 42 due to mir-opt bug +} diff --git a/src/test/ui/mismatched_types/issue-74918-missing-lifetime.stderr b/src/test/ui/mismatched_types/issue-74918-missing-lifetime.stderr index da3056eac9009..d260addef4813 100644 --- a/src/test/ui/mismatched_types/issue-74918-missing-lifetime.stderr +++ b/src/test/ui/mismatched_types/issue-74918-missing-lifetime.stderr @@ -6,8 +6,8 @@ LL | type Item = IteratorChunk<T, S>; | help: consider introducing a named lifetime parameter | -LL | type Item<'a> = IteratorChunk<<'a>T, S>; - | ^^^^ ^^^^ +LL | type Item<'a> = IteratorChunk<'a, T, S>; + | ^^^^ ^^^ error: `impl` item signature doesn't match `trait` item signature --> $DIR/issue-74918-missing-lifetime.rs:11:5 diff --git a/src/test/ui/suggestions/missing-lifetime-in-assoc-const-type.rs b/src/test/ui/suggestions/missing-lifetime-in-assoc-const-type.rs new file mode 100644 index 0000000000000..38332627f4c87 --- /dev/null +++ b/src/test/ui/suggestions/missing-lifetime-in-assoc-const-type.rs @@ -0,0 +1,16 @@ +trait ZstAssert: Sized { + const A: &str = ""; //~ ERROR missing lifetime specifier + const B: S = S { s: &() }; //~ ERROR missing lifetime specifier + const C: &'_ str = ""; //~ ERROR missing lifetime specifier + const D: T = T { a: &(), b: &() }; //~ ERROR missing lifetime specifier +} + +struct S<'a> { + s: &'a (), +} +struct T<'a, 'b> { + a: &'a (), + b: &'b (), +} + +fn main() {} diff --git a/src/test/ui/suggestions/missing-lifetime-in-assoc-const-type.stderr b/src/test/ui/suggestions/missing-lifetime-in-assoc-const-type.stderr new file mode 100644 index 0000000000000..b20778ce20817 --- /dev/null +++ b/src/test/ui/suggestions/missing-lifetime-in-assoc-const-type.stderr @@ -0,0 +1,73 @@ +error[E0106]: missing lifetime specifier + --> $DIR/missing-lifetime-in-assoc-const-type.rs:2:14 + | +LL | const A: &str = ""; + | ^ expected named lifetime parameter + | +help: consider using the `'static` lifetime + | +LL | const A: &'static str = ""; + | ^^^^^^^ +help: consider introducing a named lifetime parameter + | +LL | trait ZstAssert<'a>: Sized { +LL | const A: &'a str = ""; + | + +error[E0106]: missing lifetime specifier + --> $DIR/missing-lifetime-in-assoc-const-type.rs:3:14 + | +LL | const B: S = S { s: &() }; + | ^ expected named lifetime parameter + | +help: consider using the `'static` lifetime + | +LL | const B: S<'static> = S { s: &() }; + | ^^^^^^^^^ +help: consider introducing a named lifetime parameter + | +LL | trait ZstAssert<'a>: Sized { +LL | const A: &str = ""; +LL | const B: S<'a> = S { s: &() }; + | + +error[E0106]: missing lifetime specifier + --> $DIR/missing-lifetime-in-assoc-const-type.rs:4:15 + | +LL | const C: &'_ str = ""; + | ^^ expected named lifetime parameter + | +help: consider using the `'static` lifetime + | +LL | const C: &'static str = ""; + | ^^^^^^^ +help: consider introducing a named lifetime parameter + | +LL | trait ZstAssert<'a>: Sized { +LL | const A: &str = ""; +LL | const B: S = S { s: &() }; +LL | const C: &'a str = ""; + | + +error[E0106]: missing lifetime specifiers + --> $DIR/missing-lifetime-in-assoc-const-type.rs:5:14 + | +LL | const D: T = T { a: &(), b: &() }; + | ^ expected 2 lifetime parameters + | +help: consider using the `'static` lifetime + | +LL | const D: T<'static, 'static> = T { a: &(), b: &() }; + | ^^^^^^^^^^^^^^^^^^ +help: consider introducing a named lifetime parameter + | +LL | trait ZstAssert<'a>: Sized { +LL | const A: &str = ""; +LL | const B: S = S { s: &() }; +LL | const C: &'_ str = ""; +LL | const D: T<'a, 'a> = T { a: &(), b: &() }; + | + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0106`. diff --git a/src/test/ui/suggestions/missing-lt-for-hrtb.rs b/src/test/ui/suggestions/missing-lt-for-hrtb.rs new file mode 100644 index 0000000000000..a90a90122ad19 --- /dev/null +++ b/src/test/ui/suggestions/missing-lt-for-hrtb.rs @@ -0,0 +1,15 @@ +struct X<'a>(&'a ()); +struct S<'a>(&'a dyn Fn(&X) -> &X); +//~^ ERROR missing lifetime specifier +//~| ERROR missing lifetime specifier +struct V<'a>(&'a dyn for<'b> Fn(&X) -> &X); +//~^ ERROR missing lifetime specifier +//~| ERROR missing lifetime specifier + +fn main() { + let x = S(&|x| { + println!("hi"); + x + }); + x.0(&X(&())); +} diff --git a/src/test/ui/suggestions/missing-lt-for-hrtb.stderr b/src/test/ui/suggestions/missing-lt-for-hrtb.stderr new file mode 100644 index 0000000000000..2cb63500e48b9 --- /dev/null +++ b/src/test/ui/suggestions/missing-lt-for-hrtb.stderr @@ -0,0 +1,63 @@ +error[E0106]: missing lifetime specifier + --> $DIR/missing-lt-for-hrtb.rs:2:32 + | +LL | struct S<'a>(&'a dyn Fn(&X) -> &X); + | -- ^ expected named lifetime parameter + | + = help: this function's return type contains a borrowed value, but the signature does not say which one of argument 1's 2 lifetimes it is borrowed from + = note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html +help: consider making the bound lifetime-generic with a new `'b` lifetime + | +LL | struct S<'a>(&'a dyn for<'b> Fn(&'b X) -> &'b X); + | ^^^^^^^ ^^^^^ ^^^ +help: consider using the `'a` lifetime + | +LL | struct S<'a>(&'a dyn Fn(&X) -> &'a X); + | ^^^ + +error[E0106]: missing lifetime specifier + --> $DIR/missing-lt-for-hrtb.rs:2:33 + | +LL | struct S<'a>(&'a dyn Fn(&X) -> &X); + | -- ^ expected named lifetime parameter + | + = help: this function's return type contains a borrowed value, but the signature does not say which one of argument 1's 2 lifetimes it is borrowed from + = note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html +help: consider making the bound lifetime-generic with a new `'b` lifetime + | +LL | struct S<'a>(&'a dyn for<'b> Fn(&'b X) -> &X<'b>); + | ^^^^^^^ ^^^^^ ^^^^^ +help: consider using the `'a` lifetime + | +LL | struct S<'a>(&'a dyn Fn(&X) -> &X<'a>); + | ^^^^^ + +error[E0106]: missing lifetime specifier + --> $DIR/missing-lt-for-hrtb.rs:5:40 + | +LL | struct V<'a>(&'a dyn for<'b> Fn(&X) -> &X); + | -- ^ expected named lifetime parameter + | + = help: this function's return type contains a borrowed value, but the signature does not say which one of argument 1's 2 lifetimes it is borrowed from +note: these named lifetimes are available to use + --> $DIR/missing-lt-for-hrtb.rs:5:10 + | +LL | struct V<'a>(&'a dyn for<'b> Fn(&X) -> &X); + | ^^ ^^ + +error[E0106]: missing lifetime specifier + --> $DIR/missing-lt-for-hrtb.rs:5:41 + | +LL | struct V<'a>(&'a dyn for<'b> Fn(&X) -> &X); + | -- ^ expected named lifetime parameter + | + = help: this function's return type contains a borrowed value, but the signature does not say which one of argument 1's 2 lifetimes it is borrowed from +note: these named lifetimes are available to use + --> $DIR/missing-lt-for-hrtb.rs:5:10 + | +LL | struct V<'a>(&'a dyn for<'b> Fn(&X) -> &X); + | ^^ ^^ + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0106`. diff --git a/src/tools/cargo b/src/tools/cargo index 1653f35464483..ab32ee88dade1 160000 --- a/src/tools/cargo +++ b/src/tools/cargo @@ -1 +1 @@ -Subproject commit 1653f354644834073d6d2541e27fae94588e685e +Subproject commit ab32ee88dade1b50c77347599e82ca2de3fb8a51 diff --git a/src/tools/clippy/clippy_lints/src/derive.rs b/src/tools/clippy/clippy_lints/src/derive.rs index 80a0675898240..58b0704294b56 100644 --- a/src/tools/clippy/clippy_lints/src/derive.rs +++ b/src/tools/clippy/clippy_lints/src/derive.rs @@ -224,7 +224,7 @@ fn check_hash_peq<'tcx>( mess, |diag| { if let Some(local_def_id) = impl_id.as_local() { - let hir_id = cx.tcx.hir().as_local_hir_id(local_def_id); + let hir_id = cx.tcx.hir().local_def_id_to_hir_id(local_def_id); diag.span_note( cx.tcx.hir().span(hir_id), "`PartialEq` implemented here" @@ -278,7 +278,7 @@ fn check_ord_partial_ord<'tcx>( mess, |diag| { if let Some(local_def_id) = impl_id.as_local() { - let hir_id = cx.tcx.hir().as_local_hir_id(local_def_id); + let hir_id = cx.tcx.hir().local_def_id_to_hir_id(local_def_id); diag.span_note( cx.tcx.hir().span(hir_id), "`PartialOrd` implemented here" @@ -341,7 +341,7 @@ fn check_unsafe_derive_deserialize<'tcx>( ty: Ty<'tcx>, ) { fn item_from_def_id<'tcx>(cx: &LateContext<'tcx>, def_id: DefId) -> &'tcx Item<'tcx> { - let hir_id = cx.tcx.hir().as_local_hir_id(def_id.expect_local()); + let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); cx.tcx.hir().expect_item(hir_id) } @@ -355,7 +355,7 @@ fn check_unsafe_derive_deserialize<'tcx>( if match_path(&trait_ref.path, &paths::SERDE_DESERIALIZE); if let ty::Adt(def, _) = ty.kind; if let Some(local_def_id) = def.did.as_local(); - let adt_hir_id = cx.tcx.hir().as_local_hir_id(local_def_id); + let adt_hir_id = cx.tcx.hir().local_def_id_to_hir_id(local_def_id); if !is_allowed(cx, UNSAFE_DERIVE_DESERIALIZE, adt_hir_id); if cx.tcx.inherent_impls(def.did) .iter() diff --git a/src/tools/clippy/clippy_lints/src/new_without_default.rs b/src/tools/clippy/clippy_lints/src/new_without_default.rs index 621ebdef2f0b1..28d1322e94626 100644 --- a/src/tools/clippy/clippy_lints/src/new_without_default.rs +++ b/src/tools/clippy/clippy_lints/src/new_without_default.rs @@ -103,7 +103,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault { cx.tcx.for_each_impl(default_trait_id, |d| { if let Some(ty_def) = cx.tcx.type_of(d).ty_adt_def() { if let Some(local_def_id) = ty_def.did.as_local() { - impls.insert(cx.tcx.hir().as_local_hir_id(local_def_id)); + impls.insert(cx.tcx.hir().local_def_id_to_hir_id(local_def_id)); } } }); diff --git a/src/tools/tier-check/Cargo.toml b/src/tools/tier-check/Cargo.toml new file mode 100644 index 0000000000000..9917b383aab37 --- /dev/null +++ b/src/tools/tier-check/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "tier-check" +version = "0.1.0" +authors = ["Eric Huss"] +edition = "2018" +license = "MIT OR Apache-2.0" + +[dependencies] diff --git a/src/tools/tier-check/src/main.rs b/src/tools/tier-check/src/main.rs new file mode 100644 index 0000000000000..b8d60a5e2fef9 --- /dev/null +++ b/src/tools/tier-check/src/main.rs @@ -0,0 +1,52 @@ +//! This is a script for validating the platform support page in the rustc book. +//! +//! The script takes two arguments, the path to the Platform Support source +//! page, and the second argument is the path to `rustc`. + +use std::collections::HashSet; + +fn main() { + let mut args = std::env::args().skip(1); + let src = args.next().expect("expected source file as first argument"); + let filename = std::path::Path::new(&src).file_name().unwrap().to_str().unwrap(); + let rustc = args.next().expect("expected rustc as second argument"); + let output = std::process::Command::new(rustc) + .arg("--print=target-list") + .output() + .expect("rustc should run"); + if !output.status.success() { + eprintln!("rustc failed to run"); + std::process::exit(0); + } + let stdout = std::str::from_utf8(&output.stdout).expect("utf8"); + let target_list: HashSet<_> = stdout.lines().collect(); + + let doc_targets_md = std::fs::read_to_string(&src).expect("failed to read input source"); + let doc_targets: HashSet<_> = doc_targets_md + .lines() + .filter(|line| line.starts_with('`') && line.contains('|')) + // These platforms only exist on macos. + .filter(|line| !line.contains("[^apple]") || cfg!(target_os = "macos")) + .map(|line| line.split('`').skip(1).next().expect("expected target code span")) + .collect(); + + let missing: Vec<_> = target_list.difference(&doc_targets).collect(); + let extra: Vec<_> = doc_targets.difference(&target_list).collect(); + for target in &missing { + eprintln!( + "error: target `{}` is missing from {}\n\ + If this is a new target, please add it to {}.", + target, filename, src + ); + } + for target in &extra { + eprintln!( + "error: target `{}` is in {}, but does not appear in the rustc target list\n\ + If the target has been removed, please edit {} and remove the target.", + target, filename, src + ); + } + if !missing.is_empty() || !extra.is_empty() { + std::process::exit(1); + } +}