sway_core/language/ty/declaration/
variable.rs1use crate::{
2 engine_threading::*,
3 language::{parsed::VariableDeclaration, ty::*},
4 type_system::*,
5};
6use serde::{Deserialize, Serialize};
7use std::hash::{Hash, Hasher};
8use sway_types::{Ident, Named, Spanned};
9
10#[derive(Clone, Debug, Serialize, Deserialize)]
11pub struct TyVariableDecl {
12 pub name: Ident,
13 pub body: TyExpression,
14 pub mutability: VariableMutability,
15 pub return_type: TypeId,
16 pub type_ascription: TypeArgument,
17}
18
19impl TyDeclParsedType for TyVariableDecl {
20 type ParsedType = VariableDeclaration;
21}
22
23impl Named for TyVariableDecl {
24 fn name(&self) -> &sway_types::BaseIdent {
25 &self.name
26 }
27}
28
29impl Spanned for TyVariableDecl {
30 fn span(&self) -> sway_types::Span {
31 self.name.span()
32 }
33}
34
35impl EqWithEngines for TyVariableDecl {}
36impl PartialEqWithEngines for TyVariableDecl {
37 fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
38 let type_engine = ctx.engines().te();
39 self.name == other.name
40 && self.body.eq(&other.body, ctx)
41 && self.mutability == other.mutability
42 && type_engine
43 .get(self.return_type)
44 .eq(&type_engine.get(other.return_type), ctx)
45 && self.type_ascription.eq(&other.type_ascription, ctx)
46 }
47}
48
49impl HashWithEngines for TyVariableDecl {
50 fn hash<H: Hasher>(&self, state: &mut H, engines: &Engines) {
51 let TyVariableDecl {
52 name,
53 body,
54 mutability,
55 return_type,
56 type_ascription,
57 } = self;
58 let type_engine = engines.te();
59 name.hash(state);
60 body.hash(state, engines);
61 type_engine.get(*return_type).hash(state, engines);
62 type_ascription.hash(state, engines);
63 mutability.hash(state);
64 }
65}
66
67impl SubstTypes for TyVariableDecl {
68 fn subst_inner(&mut self, ctx: &SubstTypesContext) -> HasChanges {
69 self.return_type.subst(ctx);
70 self.type_ascription.subst(ctx);
71 self.body.subst(ctx)
72 }
73}