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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use rustdoc_types::{Item, Trait};

use crate::IndexedCrate;

pub(crate) fn is_trait_sealed<'a>(indexed_crate: &IndexedCrate<'a>, item: &'a Item) -> bool {
    let trait_item = unwrap_trait(item);

    // If the trait is pub-in-priv, trivially sealed.
    if is_pub_in_priv_item(indexed_crate, &item.id) {
        return true;
    }

    // Does the trait have a method that:
    // - does not have a default impl, and
    // - takes at least one non-`self` argument that is pub-in-priv
    //
    // If so, the trait is method-sealed, per:
    // https://predr.ag/blog/definitive-guide-to-sealed-traits-in-rust/#sealing-traits-via-method-signatures
    if is_method_sealed(indexed_crate, trait_item) {
        return true;
    }

    // Does the trait have a supertrait that is all of the below:
    // - defined in this crate
    // - pub-in-priv, or otherwise sealed
    // - lacking a blanket impl whose bounds can be satisfied outside this crate
    if has_sealed_supertrait(indexed_crate, trait_item) {
        return true;
    }

    false
}

fn is_pub_in_priv_item<'a>(indexed_crate: &IndexedCrate<'a>, id: &'a rustdoc_types::Id) -> bool {
    // TODO: We don't need all names here, one is plenty. See if this is worth optimizing.
    indexed_crate.publicly_importable_names(id).is_empty()
}

fn unwrap_trait(item: &Item) -> &'_ Trait {
    match &item.inner {
        rustdoc_types::ItemEnum::Trait(t) => t,
        _ => unreachable!(),
    }
}

fn is_method_sealed<'a>(indexed_crate: &IndexedCrate<'a>, trait_inner: &'a Trait) -> bool {
    for inner_item_id in &trait_inner.items {
        let inner_item = &indexed_crate.inner.index[inner_item_id];
        if let rustdoc_types::ItemEnum::Function(func) = &inner_item.inner {
            if func.has_body {
                // This trait function has a default implementation.
                // An implementation is not required in order to implement this trait on a type.
                // Therefore, it cannot on its own cause the trait to be sealed.
                continue;
            }

            // Check for pub-in-priv function parameters.
            for (_, param) in &func.decl.inputs {
                if let rustdoc_types::Type::ResolvedPath(path) = param {
                    if is_local_pub_in_priv_path(indexed_crate, path) {
                        return true;
                    }
                }
            }
        }
    }

    false
}

fn has_sealed_supertrait<'a>(indexed_crate: &IndexedCrate<'a>, inner: &'a Trait) -> bool {
    for bound in &inner.bounds {
        let supertrait = match bound {
            rustdoc_types::GenericBound::TraitBound {
                trait_: trait_path, ..
            } => {
                match indexed_crate.inner.index.get(&trait_path.id) {
                    Some(item) => item,
                    None => {
                        // Item from another crate, so clearly not pub-in-priv.
                        //
                        // TODO: Once we have the ability to do cross-crate analysis, consider
                        //       whether this external trait is sealed. That can have
                        //       some interesting SemVer implications as well.
                        continue;
                    }
                }
            }
            rustdoc_types::GenericBound::Outlives(_) | rustdoc_types::GenericBound::Use(_) => {
                continue;
            }
        };

        // Otherwise, check if the supertrait is itself sealed.
        // This catches cases like:
        // ```rust
        // mod priv {
        //     pub trait Sealed {}
        // }
        //
        // pub trait First: Sealed {}
        //
        // pub trait Second: First {}
        // ```
        //
        // Here, both `First` and `Second` are sealed.
        //
        // N.B.: This cannot infinite-loop, since rustc denies cyclic trait bounds.
        if is_trait_sealed(indexed_crate, supertrait) {
            // The supertrait is sealed, so a downstream crate cannot add a direct impl for it
            // on any of its own types.
            //
            // However, this isn't the same thing as "downstream types cannot impl this trait"!
            // We must check the supertrait for blanket impls that might result in
            // a downstream type gaining an impl for that trait.
            if has_no_externally_satisfiable_blanket_impls(indexed_crate, supertrait) {
                return true;
            }
        }
    }

    false
}

fn has_no_externally_satisfiable_blanket_impls(
    indexed_crate: &IndexedCrate<'_>,
    trait_: &Item,
) -> bool {
    let trait_item = unwrap_trait(trait_);
    for impl_id in &trait_item.implementations {
        let impl_item = match indexed_crate.inner.index.get(impl_id).map(|item| {
            let rustdoc_types::ItemEnum::Impl(impl_item) = &item.inner else {
                panic!("impl Id {impl_id:?} did not refer to an impl item: {item:?}");
            };
            impl_item
        }) {
            Some(item) => item,
            None => {
                // Failed to find the impl item in the index.
                continue;
            }
        };

        if is_externally_satisfiable_blanket_impl(indexed_crate, impl_item) {
            return false;
        }
    }

    true
}

fn is_externally_satisfiable_blanket_impl(
    indexed_crate: &IndexedCrate<'_>,
    impl_item: &rustdoc_types::Impl,
) -> bool {
    let blanket_type = match get_impl_target_if_blanket_impl(impl_item) {
        None => {
            // Not a blanket impl, so not relevant here.
            return false;
        }
        Some(blanket) => blanket,
    };

    // Can the blanket cover a type that a downstream crate might define?
    // For example, `T` and `&T` count, whereas `Vec<T>`, `[T]`, and `*const T` do not.
    if !blanket_type_might_cover_types_in_downstream_crate(blanket_type) {
        // This blanket impl doesn't cover types of a downstream crate. It isn't relevant here.
        return false;
    }

    // Can the bounds on this impl be satisfied by downstream crates' types?
    for generic in &impl_item.generics.params {
        match &generic.kind {
            rustdoc_types::GenericParamDefKind::Type {
                bounds, synthetic, ..
            } => {
                if *synthetic {
                    // Synthetic bounds don't count. We also don't really expect to find one here.
                    continue;
                }

                // The blanket impl is only not externally satisfiable if at least one trait bound
                // references a trait where all of the following apply:
                // - The trait is local to the crate we're analyzing.
                // - The trait is sealed.
                // - The trait has no blanket impls that are externally satisfiable.
                //   (The same criterion we're in the middle of evaluating for another trait here.)
                for bound in bounds {
                    if is_bounded_on_local_sealed_trait_without_blankets(indexed_crate, bound) {
                        return false;
                    }
                }
            }
            rustdoc_types::GenericParamDefKind::Lifetime { .. }
            | rustdoc_types::GenericParamDefKind::Const { .. } => {
                // Lifetime and const generics aren't relevant here.
                continue;
            }
        }
    }

    true
}

fn get_impl_target_if_blanket_impl(
    impl_item: &rustdoc_types::Impl,
) -> Option<&rustdoc_types::Type> {
    let mut current_type = &impl_item.for_;

    loop {
        match current_type {
            rustdoc_types::Type::ResolvedPath { .. } |  // e.g. `Arc<T>`
            rustdoc_types::Type::DynTrait { .. } |      // e.g. `dyn Iterator`
            rustdoc_types::Type::Tuple { .. } |         // e.g. `(T,)`
            rustdoc_types::Type::Slice { .. } |         // e.g. `[T]`
            rustdoc_types::Type::Array { .. } |         // e.g. `[T; 1]`
            rustdoc_types::Type::Pat { .. } => {        // unstable feature, syntax isn't finalized
                // These are all specific types that simply have a generic parameter.
                // They are not blanket implementations.
                return None;
            }
            rustdoc_types::Type::Generic(..) => {
                // Blanket impl!
                break;
            }
            rustdoc_types::Type::Primitive { .. } |
            rustdoc_types::Type::FunctionPointer { .. } |
            rustdoc_types::Type::Infer |
            rustdoc_types::Type::ImplTrait { .. } |
            rustdoc_types::Type::QualifiedPath { .. } => {
                // Not a blanket impl.
                return None;
            }
            rustdoc_types::Type::RawPointer { type_, .. } |
            rustdoc_types::Type::BorrowedRef { type_, .. } => {
                current_type = type_;
            }
        }
    }

    Some(&impl_item.for_)
}

fn is_bounded_on_local_sealed_trait_without_blankets(
    indexed_crate: &IndexedCrate<'_>,
    bound: &rustdoc_types::GenericBound,
) -> bool {
    match bound {
        rustdoc_types::GenericBound::TraitBound { trait_, .. } => {
            let bound_trait_id = &trait_.id;
            let Some(item) = indexed_crate.inner.index.get(bound_trait_id) else {
                // Not a trait from this crate.
                return false;
            };

            // We cannot have an infinite loop here, since Rust won't allow cyclic bounds.
            if !is_trait_sealed(indexed_crate, item) {
                return false;
            }

            has_no_externally_satisfiable_blanket_impls(indexed_crate, item)
        }
        rustdoc_types::GenericBound::Outlives(_) | rustdoc_types::GenericBound::Use(_) => {
            // Other kinds of generic bounds aren't relevant here.
            false
        }
    }
}

fn blanket_type_might_cover_types_in_downstream_crate(blanket_type: &rustdoc_types::Type) -> bool {
    match blanket_type {
        rustdoc_types::Type::Generic(..) => {
            // Blanket implementation over a bare generic type, like `T`.
            // This matches!
            true
        }
        rustdoc_types::Type::BorrowedRef { type_, .. } => {
            // Blanket implementation over a reference, like `&T`.
            // It matches if the underlying type beheath the reference matches.
            blanket_type_might_cover_types_in_downstream_crate(type_)
        }
        rustdoc_types::Type::ResolvedPath { .. } |  // e.g. `Arc<T>`
        rustdoc_types::Type::Tuple { .. } |         // e.g. `(T,)`
        rustdoc_types::Type::Slice { .. } |         // e.g. `[T]`
        rustdoc_types::Type::Array { .. } |         // e.g. `[T; 1]`
        rustdoc_types::Type::RawPointer { .. } => { // e.g. `*const T`
            // All these types are considered "foreign" by trait coherence,
            // so Rust does not allow implementing another crate's trait on them.
            false
        }
        rustdoc_types::Type::DynTrait { .. } |
        rustdoc_types::Type::Primitive { .. } |
        rustdoc_types::Type::FunctionPointer { .. } |
        rustdoc_types::Type::Pat { .. } |
        rustdoc_types::Type::ImplTrait { .. } |
        rustdoc_types::Type::Infer { .. } |
        rustdoc_types::Type::QualifiedPath { .. } => {
            // None of these can cover a type in a downstream crate.
            false
        }
    }
}

fn is_local_pub_in_priv_path<'a>(
    indexed_crate: &IndexedCrate<'a>,
    path: &'a rustdoc_types::Path,
) -> bool {
    let Some(item) = indexed_crate.inner.index.get(&path.id) else {
        // Not an item in this crate.
        return false;
    };
    is_pub_in_priv_item(indexed_crate, &item.id)
}