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
use cairo_lang_defs::ids::ModuleFileId;
use itertools::Itertools;

use self::analysis::{Analyzer, StatementLocation};
pub use self::demand::Demand;
use self::demand::DemandReporter;
use crate::borrow_check::analysis::BackAnalysis;
use crate::diagnostic::LoweringDiagnosticKind::*;
use crate::diagnostic::LoweringDiagnostics;
use crate::{BlockId, FlatLowered, MatchInfo, Statement, VarRemapping, VariableId};

pub mod analysis;
pub mod demand;

pub type LoweredDemand = Demand<VariableId>;
pub struct BorrowChecker<'a> {
    diagnostics: &'a mut LoweringDiagnostics,
    lowered: &'a FlatLowered,
    success: bool,
}

impl<'a> DemandReporter<VariableId> for BorrowChecker<'a> {
    type IntroducePosition = ReportPosition;
    type UsePosition = ();

    fn drop(&mut self, position: ReportPosition, var: VariableId) {
        let var = &self.lowered.variables[var];
        if matches!(position, ReportPosition::Report) && !var.droppable {
            self.diagnostics.report_by_location(var.location, VariableNotDropped);
            // Currently some reporting is disabled, since Drop is not properly implemented
            // everywhere.

            // TODO(spapini): self.success = false;
        }
    }

    fn dup(&mut self, _position: (), var: VariableId) {
        let var = &self.lowered.variables[var];
        if !var.duplicatable {
            self.diagnostics.report_by_location(var.location, VariableMoved);
            self.success = false;
        }
    }

    fn last_use(&mut self, _position: Self::UsePosition, _var_index: usize, _var: VariableId) {}

    fn unused_mapped_var(&mut self, _var: VariableId) {}
}

/// The position associated with the demand reporting. This is provided at every demand computation,
/// and passed to the reporter when needed. This is used here to specify if we want diagnostics
/// to be reported at the location or not.
#[derive(Copy, Clone)]
pub enum ReportPosition {
    Report,
    // Currently some reporting is disabled, since Drop is not properly implemented everywhere.
    // TODO(spapini): Fix this.
    DoNotReport,
}

impl<'a> Analyzer for BorrowChecker<'a> {
    type Info = LoweredDemand;

    fn visit_block_start(
        &mut self,
        info: &mut Self::Info,
        _block_id: BlockId,
        block: &crate::FlatBlock,
    ) {
        info.variables_introduced(self, &block.inputs, ReportPosition::Report);
    }

    fn visit_stmt(
        &mut self,
        info: &mut Self::Info,
        _statement_location: StatementLocation,
        stmt: &Statement,
    ) {
        if let Statement::Desnap(stmt) = stmt {
            let var = &self.lowered.variables[stmt.output];
            if !var.duplicatable {
                self.diagnostics.report_by_location(var.location, DesnappingANonCopyableType);
            }
        }
        info.variables_introduced(self, &stmt.outputs(), ReportPosition::Report);
        info.variables_used(self, &stmt.inputs(), ());
    }

    fn visit_remapping(
        &mut self,
        info: &mut Self::Info,
        _block_id: BlockId,
        _target_block_id: BlockId,
        remapping: &VarRemapping,
    ) {
        info.apply_remapping(self, remapping.iter().map(|(dst, src)| (*dst, *src)));
    }

    fn merge_match(
        &mut self,
        _statement_location: StatementLocation,
        match_info: &MatchInfo,
        arms: &[(BlockId, Self::Info)],
    ) -> Self::Info {
        let arm_demands = arms
            .iter()
            .map(|(_block_id, demand)| (demand.clone(), ReportPosition::DoNotReport))
            .collect_vec();
        let mut demand = LoweredDemand::merge_demands(&arm_demands, self);
        demand.variables_used(self, &match_info.inputs(), ());
        demand
    }

    fn info_from_return(
        &mut self,
        _statement_location: StatementLocation,
        vars: &[VariableId],
    ) -> Self::Info {
        let mut info = LoweredDemand::default();
        info.variables_used(self, vars, ());
        info
    }

    fn info_from_panic(
        &mut self,
        _statement_location: StatementLocation,
        data: &VariableId,
    ) -> Self::Info {
        let mut info = LoweredDemand::default();
        info.variables_used(self, &[*data], ());
        info
    }
}

/// Report borrow checking diagnostics.
pub fn borrow_check(module_file_id: ModuleFileId, lowered: &mut FlatLowered) {
    let mut diagnostics = LoweringDiagnostics::new(module_file_id);
    diagnostics.diagnostics.extend(std::mem::take(&mut lowered.diagnostics));

    if lowered.blocks.has_root().is_ok() {
        let checker = BorrowChecker { diagnostics: &mut diagnostics, lowered, success: true };
        let mut analysis =
            BackAnalysis { lowered: &*lowered, cache: Default::default(), analyzer: checker };
        let root_demand = analysis.get_root_info();
        let success = analysis.analyzer.success;
        assert!(root_demand.finalize(), "Undefined variable should not happen at this stage");
        if !success {
            lowered.blocks.0.clear();
        }
    }

    lowered.diagnostics = diagnostics.build();
}