Skip to content

Commit db2359b

Browse files
committed
Make View's Eq and Hash more correct
1 parent 8010cf8 commit db2359b

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

src/librustc/ty/view.rs

+34-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use std::{fmt, marker::PhantomData};
1+
use std::{
2+
fmt,
3+
hash::{Hash, Hasher},
4+
marker::PhantomData,
5+
};
26

37
use syntax::ast;
48

@@ -12,14 +16,37 @@ use crate::{
1216

1317
pub use self::ViewKind::*;
1418

15-
// TODO Forward eq/hash?
16-
#[derive(Eq, PartialEq, Hash, TypeFoldable, Lift)]
19+
#[derive(TypeFoldable, Lift)]
1720
pub struct View<'tcx, T> {
1821
ty: Ty<'tcx>,
1922
_marker: PhantomData<T>,
2023
}
2124

25+
impl<'tcx, T> PartialEq for View<'tcx, T>
26+
where
27+
T: PartialEq + TyDeref<'tcx>,
28+
{
29+
fn eq(&self, other: &Self) -> bool {
30+
**self == **other
31+
}
32+
}
33+
34+
impl<'tcx, T> Eq for View<'tcx, T> where T: Eq + TyDeref<'tcx> {}
35+
36+
impl<'tcx, T> Hash for View<'tcx, T>
37+
where
38+
T: Hash + TyDeref<'tcx>,
39+
{
40+
fn hash<H>(&self, hasher: &mut H)
41+
where
42+
H: Hasher,
43+
{
44+
(**self).hash(hasher)
45+
}
46+
}
47+
2248
impl<T> Copy for View<'_, T> {}
49+
2350
impl<T> Clone for View<'_, T> {
2451
fn clone(&self) -> Self {
2552
View { ty: self.ty, _marker: PhantomData }
@@ -37,15 +64,15 @@ where
3764

3865
impl<'tcx, T> fmt::Display for View<'tcx, T>
3966
where
40-
T: fmt::Display + TyDeref<'tcx> + 'tcx,
67+
T: fmt::Display + TyDeref<'tcx>,
4168
{
4269
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4370
(**self).fmt(f)
4471
}
4572
}
4673
impl<'tcx, T> std::ops::Deref for View<'tcx, T>
4774
where
48-
T: TyDeref<'tcx> + 'tcx,
75+
T: TyDeref<'tcx>,
4976
{
5077
type Target = T;
5178
fn deref(&self) -> &Self::Target {
@@ -59,7 +86,7 @@ where
5986

6087
impl<'tcx, T> View<'tcx, T>
6188
where
62-
T: TyDeref<'tcx> + 'tcx,
89+
T: TyDeref<'tcx>,
6390
{
6491
pub fn new(ty: Ty<'tcx>) -> Option<Self> {
6592
T::ty_deref(ty)?;
@@ -75,7 +102,7 @@ impl<'tcx, T> View<'tcx, T> {
75102

76103
/// SAFETY If `Some` is returned for `ty` then `Some` must always be returned for any subsequent
77104
/// call with the same `Ty` value
78-
pub unsafe trait TyDeref<'tcx>: Sized {
105+
pub unsafe trait TyDeref<'tcx>: Sized + 'tcx {
79106
fn ty_deref(ty: Ty<'tcx>) -> Option<&'tcx Self>;
80107
}
81108

0 commit comments

Comments
 (0)