sway_core/language/ty/
code_block.rs

1use crate::{
2    decl_engine::*, engine_threading::*, language::ty::*, semantic_analysis::TypeCheckContext,
3    transform::AllowDeprecatedState, type_system::*,
4};
5use serde::{Deserialize, Serialize};
6use std::hash::Hasher;
7use sway_error::handler::{ErrorEmitted, Handler};
8use sway_types::Span;
9
10#[derive(Clone, Debug, Serialize, Deserialize)]
11pub struct TyCodeBlock {
12    pub contents: Vec<TyAstNode>,
13    pub(crate) whole_block_span: Span,
14}
15
16impl TyCodeBlock {
17    pub(crate) fn check_deprecated(
18        &self,
19        engines: &Engines,
20        handler: &Handler,
21        allow_deprecated: &mut AllowDeprecatedState,
22    ) {
23        for n in self.contents.iter() {
24            n.check_deprecated(engines, handler, allow_deprecated);
25        }
26    }
27}
28
29impl Default for TyCodeBlock {
30    fn default() -> Self {
31        Self {
32            contents: Default::default(),
33            whole_block_span: Span::dummy(),
34        }
35    }
36}
37
38impl EqWithEngines for TyCodeBlock {}
39impl PartialEqWithEngines for TyCodeBlock {
40    fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
41        self.contents.eq(&other.contents, ctx)
42    }
43}
44
45impl HashWithEngines for TyCodeBlock {
46    fn hash<H: Hasher>(&self, state: &mut H, engines: &Engines) {
47        let TyCodeBlock { contents, .. } = self;
48        contents.hash(state, engines);
49    }
50}
51
52impl SubstTypes for TyCodeBlock {
53    fn subst_inner(&mut self, ctx: &SubstTypesContext) -> HasChanges {
54        self.contents.subst(ctx)
55    }
56}
57
58impl ReplaceDecls for TyCodeBlock {
59    fn replace_decls_inner(
60        &mut self,
61        decl_mapping: &DeclMapping,
62        handler: &Handler,
63        ctx: &mut TypeCheckContext,
64    ) -> Result<bool, ErrorEmitted> {
65        handler.scope(|handler| {
66            let mut has_changes = false;
67            for node in self.contents.iter_mut() {
68                if let Ok(r) = node.replace_decls(decl_mapping, handler, ctx) {
69                    has_changes |= r;
70                }
71            }
72            Ok(has_changes)
73        })
74    }
75}
76
77impl UpdateConstantExpression for TyCodeBlock {
78    fn update_constant_expression(&mut self, engines: &Engines, implementing_type: &TyDecl) {
79        self.contents
80            .iter_mut()
81            .for_each(|x| x.update_constant_expression(engines, implementing_type));
82    }
83}
84
85impl MaterializeConstGenerics for TyCodeBlock {
86    fn materialize_const_generics(
87        &mut self,
88        engines: &Engines,
89        handler: &Handler,
90        name: &str,
91        value: &TyExpression,
92    ) -> Result<(), ErrorEmitted> {
93        self.contents
94            .iter_mut()
95            .try_for_each(|x| x.materialize_const_generics(engines, handler, name, value))
96    }
97}