2017-09-18 09:40:13 +00:00
|
|
|
//! Defines the set of legal keys that can be used in queries.
|
|
|
|
|
2023-04-26 18:53:51 +00:00
|
|
|
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId, ModDefId};
|
2022-07-22 16:48:36 +00:00
|
|
|
use rustc_hir::hir_id::{HirId, OwnerId};
|
2024-05-05 23:57:24 +00:00
|
|
|
use rustc_query_system::dep_graph::DepNodeIndex;
|
2024-03-26 11:03:23 +00:00
|
|
|
use rustc_query_system::query::{DefIdCache, DefaultCache, SingleCache, VecCache};
|
2020-12-03 23:10:55 +00:00
|
|
|
use rustc_span::symbol::{Ident, Symbol};
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::{DUMMY_SP, Span};
|
2017-09-18 09:40:13 +00:00
|
|
|
|
2024-10-15 16:43:41 +00:00
|
|
|
use crate::infer::canonical::CanonicalQueryInput;
|
2024-10-31 03:10:37 +00:00
|
|
|
use crate::mir::mono::CollectionMode;
|
2022-10-31 22:54:09 +00:00
|
|
|
use crate::ty::fast_reject::SimplifiedType;
|
2023-02-26 21:50:19 +00:00
|
|
|
use crate::ty::layout::{TyAndLayout, ValidityRequirement};
|
2023-07-11 21:35:29 +00:00
|
|
|
use crate::ty::{self, GenericArg, GenericArgsRef, Ty, TyCtxt};
|
2023-02-26 21:50:19 +00:00
|
|
|
use crate::{mir, traits};
|
2024-07-28 22:13:50 +00:00
|
|
|
|
2023-03-13 22:22:59 +00:00
|
|
|
/// Placeholder for `CrateNum`'s "local" counterpart
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct LocalCrate;
|
|
|
|
|
2017-09-18 09:40:13 +00:00
|
|
|
/// The `Key` trait controls what types can legally be used as the key
|
|
|
|
/// for a query.
|
2022-10-31 23:16:24 +00:00
|
|
|
pub trait Key: Sized {
|
2024-03-26 11:03:23 +00:00
|
|
|
// N.B. Most of the keys down below have `type Cache<V> = DefaultCache<Self, V>;`,
|
2022-12-16 15:10:48 +00:00
|
|
|
// it would be reasonable to use associated type defaults, to remove the duplication...
|
|
|
|
//
|
|
|
|
// ...But r-a doesn't support them yet and using a default here causes r-a to not infer
|
|
|
|
// return types of queries which is very annoying. Thus, until r-a support associated
|
2023-04-10 20:02:52 +00:00
|
|
|
// type defaults, please restrain from using them here <3
|
2022-12-16 15:10:48 +00:00
|
|
|
//
|
|
|
|
// r-a issue: <https://github.com/rust-lang/rust-analyzer/issues/13693>
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V>;
|
2022-10-31 23:16:24 +00:00
|
|
|
|
2017-09-18 09:40:13 +00:00
|
|
|
/// In the event that a cycle occurs, if no explicit span has been
|
|
|
|
/// given for a query with key `self`, what span should we use?
|
2019-06-13 21:48:52 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
|
2021-08-20 03:30:33 +00:00
|
|
|
|
|
|
|
/// If the key is a [`DefId`] or `DefId`--equivalent, return that `DefId`.
|
|
|
|
/// Otherwise, return `None`.
|
|
|
|
fn key_as_def_id(&self) -> Option<DefId> {
|
|
|
|
None
|
|
|
|
}
|
2022-08-15 19:11:11 +00:00
|
|
|
|
2023-11-08 06:56:06 +00:00
|
|
|
fn ty_def_id(&self) -> Option<DefId> {
|
2022-08-15 19:11:11 +00:00
|
|
|
None
|
|
|
|
}
|
2017-09-18 09:40:13 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 22:11:07 +00:00
|
|
|
pub trait AsLocalKey: Key {
|
|
|
|
type LocalKey;
|
|
|
|
|
|
|
|
/// Given an instance of this key, what crate is it referring to?
|
|
|
|
/// This is used to find the provider.
|
|
|
|
fn as_local_key(&self) -> Option<Self::LocalKey>;
|
|
|
|
}
|
|
|
|
|
2021-05-11 09:26:52 +00:00
|
|
|
impl Key for () {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = SingleCache<V>;
|
2021-05-11 09:26:52 +00:00
|
|
|
|
|
|
|
fn default_span(&self, _: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-17 01:35:16 +00:00
|
|
|
impl<'tcx> Key for ty::InstanceKind<'tcx> {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2017-09-18 09:40:13 +00:00
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
2017-09-18 09:40:13 +00:00
|
|
|
tcx.def_span(self.def_id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-17 01:35:16 +00:00
|
|
|
impl<'tcx> AsLocalKey for ty::InstanceKind<'tcx> {
|
2023-03-13 18:54:05 +00:00
|
|
|
type LocalKey = Self;
|
2022-12-09 12:26:01 +00:00
|
|
|
|
2021-05-15 08:55:53 +00:00
|
|
|
#[inline(always)]
|
2023-03-13 22:11:07 +00:00
|
|
|
fn as_local_key(&self) -> Option<Self::LocalKey> {
|
2023-03-13 18:54:05 +00:00
|
|
|
self.def_id().is_local().then(|| *self)
|
2017-09-18 09:40:13 +00:00
|
|
|
}
|
2023-03-13 22:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Key for ty::Instance<'tcx> {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2017-09-18 09:40:13 +00:00
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
2017-09-18 09:40:13 +00:00
|
|
|
tcx.def_span(self.def_id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 23:22:09 +00:00
|
|
|
impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2018-01-02 23:22:09 +00:00
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
2018-01-02 23:22:09 +00:00
|
|
|
self.instance.default_span(tcx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 09:29:01 +00:00
|
|
|
impl<'tcx> Key for (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2021-10-07 09:29:01 +00:00
|
|
|
|
|
|
|
fn default_span(&self, _: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 02:22:36 +00:00
|
|
|
impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2020-01-11 02:22:36 +00:00
|
|
|
|
|
|
|
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-18 09:40:13 +00:00
|
|
|
impl Key for CrateNum {
|
2024-05-05 23:57:24 +00:00
|
|
|
type Cache<V> = VecCache<Self, V, DepNodeIndex>;
|
2023-03-13 22:11:07 +00:00
|
|
|
|
|
|
|
fn default_span(&self, _: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsLocalKey for CrateNum {
|
2023-03-13 22:22:59 +00:00
|
|
|
type LocalKey = LocalCrate;
|
2022-10-31 23:16:24 +00:00
|
|
|
|
2021-05-15 08:55:53 +00:00
|
|
|
#[inline(always)]
|
2023-03-13 18:54:05 +00:00
|
|
|
fn as_local_key(&self) -> Option<Self::LocalKey> {
|
2023-03-13 22:22:59 +00:00
|
|
|
(*self == LOCAL_CRATE).then_some(LocalCrate)
|
2017-09-18 09:40:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 05:11:23 +00:00
|
|
|
impl Key for OwnerId {
|
2024-05-05 23:57:24 +00:00
|
|
|
type Cache<V> = VecCache<Self, V, DepNodeIndex>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2022-09-20 05:11:23 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
|
|
|
self.to_def_id().default_span(tcx)
|
|
|
|
}
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2022-09-20 05:11:23 +00:00
|
|
|
fn key_as_def_id(&self) -> Option<DefId> {
|
|
|
|
Some(self.to_def_id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 13:32:07 +00:00
|
|
|
impl Key for LocalDefId {
|
2024-05-05 23:57:24 +00:00
|
|
|
type Cache<V> = VecCache<Self, V, DepNodeIndex>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2019-10-31 13:32:07 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
|
|
|
self.to_def_id().default_span(tcx)
|
2017-09-18 09:40:13 +00:00
|
|
|
}
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2021-08-20 03:30:33 +00:00
|
|
|
fn key_as_def_id(&self) -> Option<DefId> {
|
|
|
|
Some(self.to_def_id())
|
|
|
|
}
|
2017-09-18 09:40:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Key for DefId {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefIdCache<V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
2017-09-18 09:40:13 +00:00
|
|
|
tcx.def_span(*self)
|
|
|
|
}
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2021-08-20 03:30:33 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn key_as_def_id(&self) -> Option<DefId> {
|
|
|
|
Some(*self)
|
|
|
|
}
|
2017-09-18 09:40:13 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 22:11:07 +00:00
|
|
|
impl AsLocalKey for DefId {
|
|
|
|
type LocalKey = LocalDefId;
|
2022-12-09 12:26:01 +00:00
|
|
|
|
2021-05-15 08:55:53 +00:00
|
|
|
#[inline(always)]
|
2023-03-13 22:11:07 +00:00
|
|
|
fn as_local_key(&self) -> Option<Self::LocalKey> {
|
|
|
|
self.as_local()
|
2020-07-03 14:39:02 +00:00
|
|
|
}
|
2023-03-13 22:11:07 +00:00
|
|
|
}
|
|
|
|
|
2023-04-26 18:53:51 +00:00
|
|
|
impl Key for LocalModDefId {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-04-26 18:53:51 +00:00
|
|
|
|
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
|
|
|
tcx.def_span(*self)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn key_as_def_id(&self) -> Option<DefId> {
|
|
|
|
Some(self.to_def_id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Key for ModDefId {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-04-26 18:53:51 +00:00
|
|
|
|
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
|
|
|
tcx.def_span(*self)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn key_as_def_id(&self) -> Option<DefId> {
|
|
|
|
Some(self.to_def_id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsLocalKey for ModDefId {
|
|
|
|
type LocalKey = LocalModDefId;
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn as_local_key(&self) -> Option<Self::LocalKey> {
|
|
|
|
self.as_local()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 15:30:30 +00:00
|
|
|
impl Key for SimplifiedType {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2022-03-15 15:30:30 +00:00
|
|
|
fn default_span(&self, _: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-18 09:40:13 +00:00
|
|
|
impl Key for (DefId, DefId) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
2020-04-17 20:58:04 +00:00
|
|
|
self.1.default_span(tcx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 17:13:07 +00:00
|
|
|
impl<'tcx> Key for (ty::Instance<'tcx>, LocalDefId) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2020-12-29 16:21:52 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
|
|
|
self.0.default_span(tcx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 20:58:04 +00:00
|
|
|
impl Key for (DefId, LocalDefId) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2020-04-17 20:58:04 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
2017-09-18 09:40:13 +00:00
|
|
|
self.1.default_span(tcx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 23:03:19 +00:00
|
|
|
impl Key for (LocalDefId, DefId) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2020-07-07 23:03:19 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
|
|
|
self.0.default_span(tcx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 14:10:22 +00:00
|
|
|
impl Key for (LocalDefId, LocalDefId) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2022-07-12 14:10:22 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
|
|
|
self.0.default_span(tcx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-02 18:50:25 +00:00
|
|
|
impl Key for (DefId, Ident) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2020-12-03 23:10:55 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
|
|
|
tcx.def_span(self.0)
|
|
|
|
}
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2021-08-21 21:19:59 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn key_as_def_id(&self) -> Option<DefId> {
|
|
|
|
Some(self.0)
|
|
|
|
}
|
2020-12-03 23:10:55 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 19:06:41 +00:00
|
|
|
impl Key for (LocalDefId, LocalDefId, Ident) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2020-12-03 23:10:55 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
|
|
|
self.1.default_span(tcx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-18 09:40:13 +00:00
|
|
|
impl Key for (CrateNum, DefId) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
2017-09-18 09:40:13 +00:00
|
|
|
self.1.default_span(tcx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 22:11:07 +00:00
|
|
|
impl AsLocalKey for (CrateNum, DefId) {
|
|
|
|
type LocalKey = DefId;
|
2022-12-09 12:26:01 +00:00
|
|
|
|
2022-03-15 15:30:30 +00:00
|
|
|
#[inline(always)]
|
2023-03-13 18:54:05 +00:00
|
|
|
fn as_local_key(&self) -> Option<Self::LocalKey> {
|
2023-03-13 22:11:07 +00:00
|
|
|
(self.0 == LOCAL_CRATE).then(|| self.1)
|
2022-03-15 15:30:30 +00:00
|
|
|
}
|
2023-03-13 22:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Key for (CrateNum, SimplifiedType) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2022-03-15 15:30:30 +00:00
|
|
|
fn default_span(&self, _: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-13 22:11:07 +00:00
|
|
|
impl AsLocalKey for (CrateNum, SimplifiedType) {
|
|
|
|
type LocalKey = SimplifiedType;
|
2022-12-09 12:26:01 +00:00
|
|
|
|
2021-05-15 08:55:53 +00:00
|
|
|
#[inline(always)]
|
2023-03-13 18:54:05 +00:00
|
|
|
fn as_local_key(&self) -> Option<Self::LocalKey> {
|
2023-03-13 22:11:07 +00:00
|
|
|
(self.0 == LOCAL_CRATE).then(|| self.1)
|
2017-09-18 09:40:13 +00:00
|
|
|
}
|
2023-03-13 22:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Key for (DefId, SimplifiedType) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
2017-09-18 09:40:13 +00:00
|
|
|
self.0.default_span(tcx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-11 21:35:29 +00:00
|
|
|
impl<'tcx> Key for GenericArgsRef<'tcx> {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2020-01-22 11:17:21 +00:00
|
|
|
fn default_span(&self, _: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-11 21:35:29 +00:00
|
|
|
impl<'tcx> Key for (DefId, GenericArgsRef<'tcx>) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
2017-09-18 09:40:13 +00:00
|
|
|
self.0.default_span(tcx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 10:34:23 +00:00
|
|
|
impl<'tcx> Key for (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2020-09-10 16:48:18 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
2022-05-08 13:53:19 +00:00
|
|
|
(self.0).def.default_span(tcx)
|
2020-09-10 16:48:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-11 21:35:29 +00:00
|
|
|
impl<'tcx> Key for (LocalDefId, DefId, GenericArgsRef<'tcx>) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2020-07-02 21:56:17 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
2020-07-07 23:03:19 +00:00
|
|
|
self.0.default_span(tcx)
|
2020-07-02 21:56:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-04 01:18:31 +00:00
|
|
|
impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::TraitRef<'tcx>) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
2023-07-04 01:18:31 +00:00
|
|
|
tcx.def_span(self.1.def_id)
|
2017-09-29 03:13:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-01 17:13:07 +00:00
|
|
|
impl<'tcx> Key for ty::ParamEnvAnd<'tcx, Ty<'tcx>> {
|
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
|
|
|
|
|
|
|
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-15 00:35:45 +00:00
|
|
|
impl<'tcx> Key for ty::TraitRef<'tcx> {
|
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
|
|
|
|
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
|
|
|
tcx.def_span(self.def_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 21:11:55 +00:00
|
|
|
impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
2017-10-07 21:55:09 +00:00
|
|
|
tcx.def_span(self.def_id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 22:04:28 +00:00
|
|
|
impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2021-09-01 22:04:28 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
|
|
|
tcx.def_span(self.def_id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-31 14:46:23 +00:00
|
|
|
impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2021-07-31 14:46:23 +00:00
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
|
|
|
tcx.def_span(self.0.def_id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 18:22:19 +00:00
|
|
|
impl<'tcx> Key for GenericArg<'tcx> {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2020-03-23 18:22:19 +00:00
|
|
|
fn default_span(&self, _: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 03:24:45 +00:00
|
|
|
impl<'tcx> Key for ty::Const<'tcx> {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
fn default_span(&self, _: TyCtxt<'_>) -> Span {
|
2018-05-03 16:29:14 +00:00
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-18 09:40:13 +00:00
|
|
|
impl<'tcx> Key for Ty<'tcx> {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2022-07-14 21:42:47 +00:00
|
|
|
fn default_span(&self, _: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2023-11-08 06:56:06 +00:00
|
|
|
fn ty_def_id(&self) -> Option<DefId> {
|
|
|
|
match *self.kind() {
|
2022-08-15 19:11:11 +00:00
|
|
|
ty::Adt(adt, _) => Some(adt.did()),
|
2023-11-08 06:56:06 +00:00
|
|
|
ty::Coroutine(def_id, ..) => Some(def_id),
|
2022-08-15 19:11:11 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2022-07-14 21:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Key for TyAndLayout<'tcx> {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2021-08-18 04:45:18 +00:00
|
|
|
fn default_span(&self, _: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Key for (Ty<'tcx>, Ty<'tcx>) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
fn default_span(&self, _: TyCtxt<'_>) -> Span {
|
2017-09-18 09:40:13 +00:00
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-24 21:49:31 +00:00
|
|
|
impl<'tcx> Key for ty::Clauses<'tcx> {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2020-04-11 04:50:02 +00:00
|
|
|
fn default_span(&self, _: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 09:55:18 +00:00
|
|
|
impl<'tcx> Key for ty::ParamEnv<'tcx> {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
fn default_span(&self, _: TyCtxt<'_>) -> Span {
|
2018-04-10 09:55:18 +00:00
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-15 12:53:31 +00:00
|
|
|
impl<'tcx, T: Key> Key for ty::PseudoCanonicalInput<'tcx, T> {
|
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
|
|
|
|
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
|
|
|
self.value.default_span(tcx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ty_def_id(&self) -> Option<DefId> {
|
|
|
|
self.value.ty_def_id()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 06:14:03 +00:00
|
|
|
impl Key for Symbol {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
|
2017-09-18 09:40:13 +00:00
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
2018-02-25 15:58:54 +00:00
|
|
|
|
2022-03-28 22:10:45 +00:00
|
|
|
impl Key for Option<Symbol> {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-03-13 18:54:05 +00:00
|
|
|
|
2022-03-28 22:10:45 +00:00
|
|
|
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 14:33:37 +00:00
|
|
|
/// Canonical query goals correspond to abstract trait operations that
|
|
|
|
/// are not tied to any crate in particular.
|
2024-10-15 16:43:41 +00:00
|
|
|
impl<'tcx, T: Clone> Key for CanonicalQueryInput<'tcx, T> {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2018-03-09 00:30:37 +00:00
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
|
2018-03-09 00:30:37 +00:00
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
2019-10-25 00:35:02 +00:00
|
|
|
|
|
|
|
impl Key for (Symbol, u32, u32) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2019-10-25 00:35:02 +00:00
|
|
|
|
|
|
|
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 15:07:46 +00:00
|
|
|
|
2023-07-11 21:35:29 +00:00
|
|
|
impl<'tcx> Key for (DefId, Ty<'tcx>, GenericArgsRef<'tcx>, ty::ParamEnv<'tcx>) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2020-05-14 15:07:46 +00:00
|
|
|
|
|
|
|
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
Add initial implementation of HIR-based WF checking for diagnostics
During well-formed checking, we walk through all types 'nested' in
generic arguments. For example, WF-checking `Option<MyStruct<u8>>`
will cause us to check `MyStruct<u8>` and `u8`. However, this is done
on a `rustc_middle::ty::Ty`, which has no span information. As a result,
any errors that occur will have a very general span (e.g. the
definintion of an associated item).
This becomes a problem when macros are involved. In general, an
associated type like `type MyType = Option<MyStruct<u8>>;` may
have completely different spans for each nested type in the HIR. Using
the span of the entire associated item might end up pointing to a macro
invocation, even though a user-provided span is available in one of the
nested types.
This PR adds a framework for HIR-based well formed checking. This check
is only run during error reporting, and is used to obtain a more precise
span for an existing error. This is accomplished by individually
checking each 'nested' type in the HIR for the type, allowing us to
find the most-specific type (and span) that produces a given error.
The majority of the changes are to the error-reporting code. However,
some of the general trait code is modified to pass through more
information.
Since this has no soundness implications, I've implemented a minimal
version to begin with, which can be extended over time. In particular,
this only works for HIR items with a corresponding `DefId` (e.g. it will
not work for WF-checking performed within function bodies).
2021-04-04 20:55:39 +00:00
|
|
|
|
2024-11-03 02:33:00 +00:00
|
|
|
impl<'tcx> Key for (Ty<'tcx>, rustc_abi::VariantIdx) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2024-03-20 17:45:14 +00:00
|
|
|
|
|
|
|
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Support HIR wf checking for function signatures
During function type-checking, we normalize any associated types in
the function signature (argument types + return type), and then
create WF obligations for each of the normalized types. The HIR wf code
does not currently support this case, so any errors that we get have
imprecise spans.
This commit extends `ObligationCauseCode::WellFormed` to support
recording a function parameter, allowing us to get the corresponding
HIR type if an error occurs. Function typechecking is modified to
pass this information during signature normalization and WF checking.
The resulting code is fairly verbose, due to the fact that we can
no longer normalize the entire signature with a single function call.
As part of the refactoring, we now perform HIR-based WF checking
for several other 'typed items' (statics, consts, and inherent impls).
As a result, WF and projection errors in a function signature now
have a precise span, which points directly at the responsible type.
If a function signature is constructed via a macro, this will allow
the error message to point at the code 'most responsible' for the error
(e.g. a user-supplied macro argument).
2021-07-18 16:33:49 +00:00
|
|
|
impl<'tcx> Key for (ty::Predicate<'tcx>, traits::WellFormedLoc) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
Add initial implementation of HIR-based WF checking for diagnostics
During well-formed checking, we walk through all types 'nested' in
generic arguments. For example, WF-checking `Option<MyStruct<u8>>`
will cause us to check `MyStruct<u8>` and `u8`. However, this is done
on a `rustc_middle::ty::Ty`, which has no span information. As a result,
any errors that occur will have a very general span (e.g. the
definintion of an associated item).
This becomes a problem when macros are involved. In general, an
associated type like `type MyType = Option<MyStruct<u8>>;` may
have completely different spans for each nested type in the HIR. Using
the span of the entire associated item might end up pointing to a macro
invocation, even though a user-provided span is available in one of the
nested types.
This PR adds a framework for HIR-based well formed checking. This check
is only run during error reporting, and is used to obtain a more precise
span for an existing error. This is accomplished by individually
checking each 'nested' type in the HIR for the type, allowing us to
find the most-specific type (and span) that produces a given error.
The majority of the changes are to the error-reporting code. However,
some of the general trait code is modified to pass through more
information.
Since this has no soundness implications, I've implemented a minimal
version to begin with, which can be extended over time. In particular,
this only works for HIR items with a corresponding `DefId` (e.g. it will
not work for WF-checking performed within function bodies).
2021-04-04 20:55:39 +00:00
|
|
|
|
|
|
|
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
2021-09-01 21:29:15 +00:00
|
|
|
|
|
|
|
impl<'tcx> Key for (ty::PolyFnSig<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2021-09-01 21:29:15 +00:00
|
|
|
|
|
|
|
fn default_span(&self, _: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2021-09-01 21:29:15 +00:00
|
|
|
|
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
|
|
|
self.0.default_span(tcx)
|
|
|
|
}
|
|
|
|
}
|
2022-04-21 14:37:24 +00:00
|
|
|
|
|
|
|
impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2022-04-21 14:37:24 +00:00
|
|
|
|
|
|
|
fn default_span(&self, _: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
2022-07-22 16:48:36 +00:00
|
|
|
|
|
|
|
impl Key for HirId {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2022-07-22 16:48:36 +00:00
|
|
|
|
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
2022-09-10 09:55:10 +00:00
|
|
|
tcx.hir().span(*self)
|
2022-07-22 16:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn key_as_def_id(&self) -> Option<DefId> {
|
2022-09-10 09:55:10 +00:00
|
|
|
None
|
2022-07-22 16:48:36 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-22 20:51:29 +00:00
|
|
|
|
Provide suggestion to dereference closure tail if appropriate
When encoutnering a case like
```rust
//@ run-rustfix
use std::collections::HashMap;
fn main() {
let vs = vec![0, 0, 1, 1, 3, 4, 5, 6, 3, 3, 3];
let mut counts = HashMap::new();
for num in vs {
let count = counts.entry(num).or_insert(0);
*count += 1;
}
let _ = counts.iter().max_by_key(|(_, v)| v);
```
produce the following suggestion
```
error: lifetime may not live long enough
--> $DIR/return-value-lifetime-error.rs:13:47
|
LL | let _ = counts.iter().max_by_key(|(_, v)| v);
| ------- ^ returning this value requires that `'1` must outlive `'2`
| | |
| | return type of closure is &'2 &i32
| has type `&'1 (&i32, &i32)`
|
help: dereference the return value
|
LL | let _ = counts.iter().max_by_key(|(_, v)| **v);
| ++
```
Fix #50195.
2024-03-08 22:59:53 +00:00
|
|
|
impl Key for (LocalDefId, HirId) {
|
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
|
|
|
|
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
|
|
|
tcx.hir().span(self.1)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn key_as_def_id(&self) -> Option<DefId> {
|
|
|
|
Some(self.0.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-15 12:53:31 +00:00
|
|
|
impl<'tcx> Key for (ValidityRequirement, ty::PseudoCanonicalInput<'tcx, Ty<'tcx>>) {
|
2024-03-26 11:03:23 +00:00
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
2023-02-22 20:51:29 +00:00
|
|
|
|
|
|
|
// Just forward to `Ty<'tcx>`
|
|
|
|
|
|
|
|
fn default_span(&self, _: TyCtxt<'_>) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
|
2023-11-08 06:56:06 +00:00
|
|
|
fn ty_def_id(&self) -> Option<DefId> {
|
2023-02-22 20:51:29 +00:00
|
|
|
match self.1.value.kind() {
|
|
|
|
ty::Adt(adt, _) => Some(adt.did()),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-31 03:10:37 +00:00
|
|
|
|
|
|
|
impl<'tcx> Key for (ty::Instance<'tcx>, CollectionMode) {
|
|
|
|
type Cache<V> = DefaultCache<Self, V>;
|
|
|
|
|
|
|
|
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
|
|
|
|
self.0.default_span(tcx)
|
|
|
|
}
|
|
|
|
}
|