1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use super::*;
use crate::{
    decl_engine::DeclRef,
    language::{
        parsed::CodeBlock,
        ty::{self, TyAstNodeContent, TyCodeBlock},
    },
};

impl ty::TyCodeBlock {
    pub(crate) fn type_check(
        handler: &Handler,
        mut ctx: TypeCheckContext,
        code_block: &CodeBlock,
    ) -> Result<Self, ErrorEmitted> {
        // Create a temp namespace for checking within the code block scope.
        let mut code_block_namespace = ctx.namespace.clone();
        let evaluated_contents = code_block
            .contents
            .iter()
            .filter_map(|node| {
                let ctx = ctx.by_ref().scoped(&mut code_block_namespace);
                ty::TyAstNode::type_check(handler, ctx, node.clone()).ok()
            })
            .collect::<Vec<ty::TyAstNode>>();

        Ok(ty::TyCodeBlock {
            contents: evaluated_contents,
            whole_block_span: code_block.whole_block_span.clone(),
        })
    }

    pub fn compute_return_type_and_span(
        ctx: &TypeCheckContext,
        code_block: &TyCodeBlock,
    ) -> (TypeId, Span) {
        let engines = ctx.engines();
        let decl_engine = engines.de();

        let implicit_return_span = code_block
            .contents
            .iter()
            .find_map(|x| match &x.content {
                TyAstNodeContent::ImplicitReturnExpression(expr) => Some(Some(expr.span.clone())),
                _ => None,
            })
            .flatten();
        let span = implicit_return_span.unwrap_or_else(|| code_block.whole_block_span.clone());

        // find the implicit return, if any, and use it as the code block's return type.
        // The fact that there is at most one implicit return is an invariant held by the parser.
        // If any node diverges then the entire block has unknown type.
        let mut node_deterministically_aborts = false;
        let block_type = code_block
            .contents
            .iter()
            .find_map(|node| {
                if node.deterministically_aborts(decl_engine, true) {
                    node_deterministically_aborts = true;
                };
                match node {
                    ty::TyAstNode {
                        content:
                            ty::TyAstNodeContent::ImplicitReturnExpression(ty::TyExpression {
                                ref return_type,
                                ..
                            }),
                        ..
                    } => Some(*return_type),
                    _ => None,
                }
            })
            .unwrap_or_else(|| {
                if node_deterministically_aborts {
                    let never_mod_path = vec![
                        Ident::new_with_override("core".into(), span.clone()),
                        Ident::new_with_override("never".into(), span.clone()),
                    ];
                    let never_ident = Ident::new_with_override("Never".into(), span.clone());

                    let never_decl_opt = ctx
                        .namespace
                        .root()
                        .resolve_symbol(
                            &Handler::default(),
                            engines,
                            &never_mod_path,
                            &never_ident,
                            None,
                        )
                        .ok();

                    if let Some(ty::TyDecl::EnumDecl(ty::EnumDecl {
                        name,
                        decl_id,
                        subst_list: _,
                        decl_span,
                    })) = never_decl_opt
                    {
                        return ctx.engines().te().insert(
                            engines,
                            TypeInfo::Enum(DeclRef::new(name.clone(), decl_id, decl_span.clone())),
                        );
                    }

                    ctx.engines.te().insert(engines, TypeInfo::Unknown)
                } else {
                    ctx.engines
                        .te()
                        .insert(engines, TypeInfo::Tuple(Vec::new()))
                }
            });
        (block_type, span)
    }
}

impl TypeCheckAnalysis for ty::TyCodeBlock {
    fn type_check_analyze(
        &self,
        handler: &Handler,
        ctx: &mut TypeCheckAnalysisContext,
    ) -> Result<(), ErrorEmitted> {
        for node in self.contents.iter() {
            node.type_check_analyze(handler, ctx)?;
        }
        Ok(())
    }
}

impl TypeCheckFinalization for ty::TyCodeBlock {
    fn type_check_finalize(
        &mut self,
        handler: &Handler,
        ctx: &mut TypeCheckFinalizationContext,
    ) -> Result<(), ErrorEmitted> {
        handler.scope(|handler| {
            for node in self.contents.iter_mut() {
                let _ = node.type_check_finalize(handler, ctx);
            }
            Ok(())
        })
    }
}

impl TypeCheckUnification for ty::TyCodeBlock {
    fn type_check_unify(
        &mut self,
        handler: &Handler,
        ctx: &mut TypeCheckUnificationContext,
    ) -> Result<(), ErrorEmitted> {
        handler.scope(|handler| {
            let type_check_ctx = &ctx.type_check_ctx;
            let (block_implicit_return, span) =
                TyCodeBlock::compute_return_type_and_span(type_check_ctx, self);
            let return_type_id = match ctx.type_id {
                Some(type_id) => type_id,
                None => block_implicit_return,
            };
            type_check_ctx.unify_with_type_annotation(handler, return_type_id, &span);
            Ok(())
        })
    }
}