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