aboutsummaryrefslogtreecommitdiff
path: root/src/reflect/message/is_initialized_is_always_true.rs
blob: e40b56d41bfdc4d8d06b99ebd29aa799026c9036 (plain)
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
use std::collections::HashMap;
use std::collections::HashSet;

use crate::descriptor::field_descriptor_proto::Label;
use crate::descriptor::FileDescriptorProto;
use crate::reflect::field::index::FieldIndex;
use crate::reflect::field::index::ForwardProtobufFieldType;
use crate::reflect::field::index::ForwardProtobufTypeBox;
use crate::reflect::file::index::MessageIndices;
use crate::reflect::MessageDescriptor;
use crate::reflect::RuntimeType;
use crate::reflect::Syntax;

pub(crate) fn compute_is_initialized_is_always_true(
    messages: &mut [MessageIndices],
    file_fields: &[FieldIndex],
    file: &FileDescriptorProto,
) {
    for message in messages.iter_mut() {
        message.is_initialized_is_always_true =
            is_initialized_is_always_true_ignoring_deps(message, file);
    }

    // Map from a message to messages who include it. E.g. for:
    // ```
    // 0: message A {}
    // 1: message B { A a = 10; }
    // ```
    // This map will contain: `{0: [1]}`
    let mut rdeps: HashMap<usize, Vec<usize>> = HashMap::new();

    for i in 0..messages.len() {
        let message = &mut messages[i];

        if !message.is_initialized_is_always_true {
            continue;
        }

        let mut is_initialized_is_always_true = true;
        for ft in message_field_messages(message, file_fields) {
            match ft {
                MessageType::ThisFile(j) => {
                    rdeps.entry(j).or_default().push(i);
                }
                MessageType::OtherFile(m) => {
                    if !m.is_initialized_is_always_true() {
                        is_initialized_is_always_true = false;
                    }
                }
            }
        }
        message.is_initialized_is_always_true = is_initialized_is_always_true;
    }

    let mut invalidated: HashSet<usize> = HashSet::new();
    let mut invalidate_stack: Vec<usize> = Vec::new();

    for i in 0..messages.len() {
        let message = &messages[i];
        if message.is_initialized_is_always_true {
            continue;
        }

        invalidate_stack.push(i);
    }

    while let Some(i) = invalidate_stack.pop() {
        if !invalidated.insert(i) {
            continue;
        }

        messages[i].is_initialized_is_always_true = false;
        let next = rdeps.get(&i).map(|v| v.as_slice()).unwrap_or_default();
        for next in next {
            invalidate_stack.push(*next);
        }
    }
}

enum MessageType<'m> {
    ThisFile(usize),
    OtherFile(&'m MessageDescriptor),
}

fn message_field_messages<'a>(
    message: &'a MessageIndices,
    file_fields: &'a [FieldIndex],
) -> impl Iterator<Item = MessageType<'a>> + 'a {
    message_field_types(message, file_fields).filter_map(|f| match f {
        ForwardProtobufTypeBox::ProtobufTypeBox(t) => match t.runtime() {
            RuntimeType::Message(m) => Some(MessageType::OtherFile(m)),
            _ => None,
        },
        ForwardProtobufTypeBox::CurrentFileEnum(_) => None,
        ForwardProtobufTypeBox::CurrentFileMessage(i) => Some(MessageType::ThisFile(*i)),
    })
}

fn message_field_types<'a>(
    message: &'a MessageIndices,
    file_fields: &'a [FieldIndex],
) -> impl Iterator<Item = &'a ForwardProtobufTypeBox> {
    enum Either<A, B> {
        Left(A),
        Right(B),
    }

    impl<T, A: Iterator<Item = T>, B: Iterator<Item = T>> Iterator for Either<A, B> {
        type Item = T;

        fn next(&mut self) -> Option<T> {
            match self {
                Either::Left(a) => a.next(),
                Either::Right(b) => b.next(),
            }
        }
    }

    message
        .message_index
        .slice_fields(file_fields)
        .iter()
        .flat_map(|f| match &f.field_type {
            ForwardProtobufFieldType::Singular(t) => Either::Left([t].into_iter()),
            ForwardProtobufFieldType::Repeated(t) => Either::Left([t].into_iter()),
            ForwardProtobufFieldType::Map(k, v) => Either::Right([k, v].into_iter()),
        })
}

fn is_initialized_is_always_true_ignoring_deps(
    message: &MessageIndices,
    file: &FileDescriptorProto,
) -> bool {
    // Shortcut.
    if Syntax::of_file(file) == Syntax::Proto3 {
        return true;
    }

    // We don't support extensions properly but if we did,
    // extensions should have been checked for `is_initialized`.
    if !message.proto.extension_range.is_empty() {
        return false;
    }

    for field in &message.proto.field {
        if field.label() == Label::LABEL_REQUIRED {
            return false;
        }
    }
    true
}