Skip to content

Commit 8be9b84

Browse files
committed
Move ty::FieldDef to its own little module (cute)
1 parent 71e0bb4 commit 8be9b84

File tree

2 files changed

+67
-57
lines changed

2 files changed

+67
-57
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use std::hash::{Hash, Hasher};
2+
3+
use rustc_hir::def_id::DefId;
4+
use rustc_span::symbol::{Ident, Symbol};
5+
6+
use crate::ty::{SubstsRef, Ty, TyCtxt, Visibility};
7+
8+
#[derive(Debug, HashStable, TyEncodable, TyDecodable)]
9+
pub struct FieldDef {
10+
pub did: DefId,
11+
pub name: Symbol,
12+
pub vis: Visibility<DefId>,
13+
}
14+
15+
impl PartialEq for FieldDef {
16+
#[inline]
17+
fn eq(&self, other: &Self) -> bool {
18+
// There should be only one `FieldDef` for each `did`, therefore it is
19+
// fine to implement `PartialEq` only based on `did`.
20+
//
21+
// Below, we exhaustively destructure `self` so that if the definition
22+
// of `FieldDef` changes, a compile-error will be produced, reminding
23+
// us to revisit this assumption.
24+
25+
let Self { did: lhs_did, name: _, vis: _ } = &self;
26+
27+
let Self { did: rhs_did, name: _, vis: _ } = other;
28+
29+
lhs_did == rhs_did
30+
}
31+
}
32+
33+
impl Eq for FieldDef {}
34+
35+
impl Hash for FieldDef {
36+
#[inline]
37+
fn hash<H: Hasher>(&self, s: &mut H) {
38+
// There should be only one `FieldDef` for each `did`, therefore it is
39+
// fine to implement `Hash` only based on `did`.
40+
//
41+
// Below, we exhaustively destructure `self` so that if the definition
42+
// of `FieldDef` changes, a compile-error will be produced, reminding
43+
// us to revisit this assumption.
44+
45+
let Self { did, name: _, vis: _ } = &self;
46+
47+
did.hash(s)
48+
}
49+
}
50+
51+
impl<'tcx> FieldDef {
52+
/// Returns the type of this field. The resulting type is not normalized. The `subst` is
53+
/// typically obtained via the second field of [`TyKind::Adt`].
54+
///
55+
/// [`TyKind::Adt`]: crate::ty::TyKind::Adt
56+
pub fn ty(&self, tcx: TyCtxt<'tcx>, subst: SubstsRef<'tcx>) -> Ty<'tcx> {
57+
tcx.type_of(self.did).subst(tcx, subst)
58+
}
59+
60+
/// Computes the `Ident` of this variant by looking up the `Span`
61+
pub fn ident(&self, tcx: TyCtxt<'_>) -> Ident {
62+
Ident::new(self.name, tcx.def_ident_span(self.did).unwrap())
63+
}
64+
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub use subst::*;
5353
pub use vtable::*;
5454

5555
use std::fmt::Debug;
56-
use std::hash::{Hash, Hasher};
56+
use std::hash::Hash;
5757
use std::{fmt, str};
5858

5959
pub use crate::ty::diagnostics::*;
@@ -142,6 +142,7 @@ mod typeck_results;
142142

143143
mod alias_relation_direction;
144144
mod bound_constness;
145+
mod field_def;
145146
mod impl_polarity;
146147
mod opaque_hidden_type;
147148
mod param_env;
@@ -153,6 +154,7 @@ mod visibility;
153154

154155
pub use alias_relation_direction::AliasRelationDirection;
155156
pub use bound_constness::BoundConstness;
157+
pub use field_def::FieldDef;
156158
pub use impl_polarity::ImplPolarity;
157159
pub use opaque_hidden_type::OpaqueHiddenType;
158160
pub use param_env::{ParamEnv, ParamEnvAnd};
@@ -490,62 +492,6 @@ pub enum VariantDiscr {
490492
Relative(u32),
491493
}
492494

493-
#[derive(Debug, HashStable, TyEncodable, TyDecodable)]
494-
pub struct FieldDef {
495-
pub did: DefId,
496-
pub name: Symbol,
497-
pub vis: Visibility<DefId>,
498-
}
499-
500-
impl PartialEq for FieldDef {
501-
#[inline]
502-
fn eq(&self, other: &Self) -> bool {
503-
// There should be only one `FieldDef` for each `did`, therefore it is
504-
// fine to implement `PartialEq` only based on `did`.
505-
//
506-
// Below, we exhaustively destructure `self` so that if the definition
507-
// of `FieldDef` changes, a compile-error will be produced, reminding
508-
// us to revisit this assumption.
509-
510-
let Self { did: lhs_did, name: _, vis: _ } = &self;
511-
512-
let Self { did: rhs_did, name: _, vis: _ } = other;
513-
514-
lhs_did == rhs_did
515-
}
516-
}
517-
518-
impl Eq for FieldDef {}
519-
520-
impl Hash for FieldDef {
521-
#[inline]
522-
fn hash<H: Hasher>(&self, s: &mut H) {
523-
// There should be only one `FieldDef` for each `did`, therefore it is
524-
// fine to implement `Hash` only based on `did`.
525-
//
526-
// Below, we exhaustively destructure `self` so that if the definition
527-
// of `FieldDef` changes, a compile-error will be produced, reminding
528-
// us to revisit this assumption.
529-
530-
let Self { did, name: _, vis: _ } = &self;
531-
532-
did.hash(s)
533-
}
534-
}
535-
536-
impl<'tcx> FieldDef {
537-
/// Returns the type of this field. The resulting type is not normalized. The `subst` is
538-
/// typically obtained via the second field of [`TyKind::Adt`].
539-
pub fn ty(&self, tcx: TyCtxt<'tcx>, subst: SubstsRef<'tcx>) -> Ty<'tcx> {
540-
tcx.type_of(self.did).subst(tcx, subst)
541-
}
542-
543-
/// Computes the `Ident` of this variant by looking up the `Span`
544-
pub fn ident(&self, tcx: TyCtxt<'_>) -> Ident {
545-
Ident::new(self.name, tcx.def_ident_span(self.did).unwrap())
546-
}
547-
}
548-
549495
#[derive(Debug, PartialEq, Eq)]
550496
pub enum ImplOverlapKind {
551497
/// These impls are always allowed to overlap.

0 commit comments

Comments
 (0)