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
//! Writer logic for `where` clauses and other bounds.
//!
//! Contains logic for writing the various forms of `Foo: Bar`.
use std::fmt::{Display, Formatter, Result};

use crate::rust_ir::*;
use chalk_ir::{interner::Interner, *};
use itertools::Itertools;

use super::{
    display_trait_with_assoc_ty_value, display_type_with_generics, render_trait::RenderAsRust,
    state::InternalWriterState,
};
use crate::split::Split;

impl<I: Interner> RenderAsRust<I> for InlineBound<I> {
    fn fmt(&self, s: &InternalWriterState<'_, I>, f: &'_ mut Formatter<'_>) -> Result {
        match self {
            // Foo: Vec<T>
            InlineBound::TraitBound(trait_bound) => trait_bound.fmt(s, f),
            // Foo: Iterator<Item=Foo>
            InlineBound::AliasEqBound(eq_bound) => eq_bound.fmt(s, f),
        }
    }
}

impl<I: Interner> RenderAsRust<I> for TraitBound<I> {
    fn fmt(&self, s: &InternalWriterState<'_, I>, f: &'_ mut Formatter<'_>) -> Result {
        display_type_with_generics(s, self.trait_id, &self.args_no_self).fmt(f)
    }
}

impl<I: Interner> RenderAsRust<I> for AliasEqBound<I> {
    fn fmt(&self, s: &InternalWriterState<'_, I>, f: &'_ mut Formatter<'_>) -> Result {
        display_trait_with_assoc_ty_value(
            s,
            s.db().associated_ty_data(self.associated_ty_id),
            &self.trait_bound.args_no_self,
            &self.parameters,
            &self.value,
        )
        .fmt(f)
    }
}

impl<I: Interner> RenderAsRust<I> for QuantifiedWhereClause<I> {
    fn fmt(&self, s: &InternalWriterState<'_, I>, f: &'_ mut Formatter<'_>) -> Result {
        let interner = s.db().interner();
        let s = &s.add_debrujin_index(None);
        if !self.binders.is_empty(interner) {
            write!(
                f,
                "forall<{}> ",
                s.binder_var_display(&self.binders).format(", ")
            )?;
        }
        self.skip_binders().fmt(s, f)
    }
}

impl<I: Interner> RenderAsRust<I> for QuantifiedInlineBound<I> {
    fn fmt(&self, s: &InternalWriterState<'_, I>, f: &'_ mut Formatter<'_>) -> Result {
        let interner = s.db().interner();
        let s = &s.add_debrujin_index(None);
        if !self.binders.is_empty(interner) {
            write!(
                f,
                "forall<{}> ",
                s.binder_var_display(&self.binders).format(", ")
            )?;
        }
        self.skip_binders().fmt(s, f)
    }
}

impl<I: Interner> RenderAsRust<I> for Vec<QuantifiedWhereClause<I>> {
    fn fmt(&self, s: &InternalWriterState<'_, I>, f: &'_ mut Formatter<'_>) -> Result {
        write!(
            f,
            "{}",
            self.iter()
                .map(|where_clause| { format!("{}{}", s.indent(), where_clause.display(s)) })
                .format(",\n")
        )?;
        Ok(())
    }
}

impl<I: Interner> RenderAsRust<I> for WhereClause<I> {
    fn fmt(&self, s: &InternalWriterState<'_, I>, f: &'_ mut Formatter<'_>) -> Result {
        match self {
            WhereClause::Implemented(trait_ref) => trait_ref.fmt(s, f),
            WhereClause::AliasEq(alias_eq) => alias_eq.fmt(s, f),
            WhereClause::LifetimeOutlives(lifetime) => lifetime.display(s).fmt(f),
            WhereClause::TypeOutlives(ty) => ty.display(s).fmt(f),
        }
    }
}

/// This renders `TraitRef` as a clause in a where clause, as opposed to its
/// usage in other places.
impl<I: Interner> RenderAsRust<I> for TraitRef<I> {
    fn fmt(&self, s: &InternalWriterState<'_, I>, f: &'_ mut Formatter<'_>) -> Result {
        let interner = s.db().interner();
        write!(
            f,
            "{}: {}",
            self.self_type_parameter(interner).display(s),
            display_type_with_generics(
                s,
                self.trait_id,
                &self.substitution.as_slice(interner)[1..]
            )
        )
    }
}

/// This renders `AliasEq` as a clause in a where clause, as opposed to its
/// usage in other places.
impl<I: Interner> RenderAsRust<I> for AliasEq<I> {
    fn fmt(&self, s: &InternalWriterState<'_, I>, f: &'_ mut Formatter<'_>) -> Result {
        // we have: X: Y<A1, A2, A3, Z<B1, B2, B3>=D>
        // B1, B2, B3, X, A1, A2, A3 are put into alias_eq.alias.substitution
        // D is alias_eq.ty
        // Z is alias_eq.alias.associated_ty_id
        // Y is also packed into alias_eq.alias.associated_ty_id
        // Now, we split out A*, Y/Z and B*:
        // trait_params is X, A1, A2, A3,
        // assoc_type_params is B1, B2, B3,
        // assoc_ty_datum stores info about Y and Z.
        match &self.alias {
            AliasTy::Projection(projection_ty) => {
                let (assoc_ty_datum, trait_params, assoc_type_params) =
                    s.db().split_projection(projection_ty);
                // An alternate form might be `<{} as {}<{}>>::{}<{}> = {}` (with same
                // parameter ordering). This alternate form would require type equality
                // constraints (https://github.com/rust-lang/rust/issues/20041).
                write!(
                    f,
                    "{}: {}",
                    trait_params[0].display(s),
                    display_trait_with_assoc_ty_value(
                        s,
                        assoc_ty_datum,
                        &trait_params[1..],
                        assoc_type_params,
                        &self.ty
                    ),
                )
            }
            AliasTy::Opaque(opaque) => write!(f, "{}", opaque.display(s)),
        }
    }
}

impl<I: Interner> RenderAsRust<I> for LifetimeOutlives<I> {
    fn fmt(&self, s: &InternalWriterState<'_, I>, f: &mut Formatter<'_>) -> Result {
        // a': 'b
        write!(f, "{}: {}", self.a.display(s), self.b.display(s))
    }
}

impl<I: Interner> RenderAsRust<I> for TypeOutlives<I> {
    fn fmt(&self, s: &InternalWriterState<'_, I>, f: &mut Formatter<'_>) -> Result {
        // T: 'a
        write!(f, "{}: {}", self.ty.display(s), self.lifetime.display(s))
    }
}