Unnamed repository; edit this file 'description' to name the repository.
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
use hir_def::DefWithBodyId;
use test_fixture::WithFixture;

use crate::{InferBodyId, db::HirDatabase, setup_tracing, test_db::TestDB};

fn lower_mir(#[rust_analyzer::rust_fixture] ra_fixture: &str) {
    let _tracing = setup_tracing();
    let (db, file_ids) = TestDB::with_many_files(ra_fixture);
    crate::attach_db(db.as_dyn(), || {
        let file_id = *file_ids.last().unwrap();
        let module_id = db.module_for_file(file_id.file_id(&db));
        let def_map = module_id.def_map(&db);
        let scope = &def_map[module_id].scope;
        let funcs = scope.declarations().filter_map(|x| match x {
            hir_def::ModuleDefId::FunctionId(it) => Some(it),
            _ => None,
        });
        for func in funcs {
            _ = db.mir_body(func.into());
        }
    })
}

#[test]
fn dyn_projection_with_auto_traits_regression_next_solver() {
    lower_mir(
        r#"
//- minicore: sized, send
pub trait Deserializer {}

pub trait Strictest {
    type Object: ?Sized;
}

impl Strictest for dyn CustomValue {
    type Object = dyn CustomValue + Send;
}

pub trait CustomValue: Send {}

impl CustomValue for () {}

struct Box<T: ?Sized>;

type DeserializeFn<T> = fn(&mut dyn Deserializer) -> Box<T>;

fn foo() {
    (|deserializer| Box::new(())) as DeserializeFn<<dyn CustomValue as Strictest>::Object>;
}
    "#,
    );
}

fn check_borrowck(#[rust_analyzer::rust_fixture] ra_fixture: &str) {
    let _tracing = setup_tracing();
    let (db, file_ids) = TestDB::with_many_files(ra_fixture);
    crate::attach_db(db.as_dyn(), || {
        let file_id = *file_ids.last().unwrap();
        let module_id = db.module_for_file(file_id.file_id(&db));
        let def_map = module_id.def_map(&db);
        let scope = &def_map[module_id].scope;

        let mut bodies: Vec<DefWithBodyId> = Vec::new();

        for decl in scope.declarations() {
            if let hir_def::ModuleDefId::FunctionId(f) = decl {
                bodies.push(f.into());
            }
        }

        for impl_id in scope.impls() {
            let impl_items = impl_id.impl_items(&db);
            for (_, item) in impl_items.items.iter() {
                if let hir_def::AssocItemId::FunctionId(f) = item {
                    bodies.push((*f).into());
                }
            }
        }

        for body in bodies {
            let _ = InferBodyId::from(body).borrowck(db.as_dyn());
        }
    })
}

#[test]
fn regression_21173_const_generic_impl_with_assoc_type() {
    check_borrowck(
        r#"
pub trait Tr {
    type Assoc;
    fn f(&self, handle: Self::Assoc) -> i32;
}

pub struct ConstGeneric<const N: usize>;

impl<const N: usize> Tr for &ConstGeneric<N> {
    type Assoc = AssocTy;

    fn f(&self, a: Self::Assoc) -> i32 {
        a.x
    }
}

pub struct AssocTy {
    x: i32,
}
    "#,
    );
}

#[test]
fn borrowck_tuple_field_projection_recovery_does_not_panic() {
    check_borrowck(
        r#"
//- minicore: sized
fn tuple_field() {
    let t = (1,);
    let x = t.1;
}
    "#,
    );
}

#[test]
fn borrowck_alias_projection_recovery_does_not_panic() {
    check_borrowck(
        r#"
//- minicore: sized
trait Tr { type A; }
fn alias<T: Tr>(x: T::A) {
    let (a, b) = x;
}
    "#,
    );
}

#[test]
fn borrowck_opaque_downcast_recovery_does_not_panic() {
    check_borrowck(
        r#"
//- minicore: option, sized
struct PathBuf;
fn opaque<T>(path: T) -> impl Sized {
    Some(path)
}
fn caller(path: &PathBuf) {
    let Some(value) = opaque(path) else { return };
}
    "#,
    );
}

#[test]
fn borrowck_hrtb_closure_argument_does_not_panic() {
    check_borrowck(
        r#"
//- minicore: fn, copy
enum Res<T, E> {
    Ok(T),
    Err(E),
}

struct S;

impl S {
    fn set<F>(&mut self, _: F)
    where
        F: for<'a> Fn(&mut (), &'a [u8]) -> Res<(), ()>,
    {
    }
}

fn main() {
    let mut s = S;
    s.set(|_, _| Res::Err(()));
}
    "#,
    );
}