Skip to content

Commit e29ecb7

Browse files
authored
Rollup merge of #103276 - compiler-errors:default-on-uninit-ice, r=TaKO8Ki
Erase regions before checking for `Default` in uninitialized binding error Fixes #103250
2 parents 1f21023 + c5c9f74 commit e29ecb7

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,17 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
492492
let Some(default_trait) = tcx.get_diagnostic_item(sym::Default) else {
493493
return false;
494494
};
495+
// Regions are already solved, so we must use a fresh InferCtxt,
496+
// but the type has region variables, so erase those.
495497
tcx.infer_ctxt()
496498
.build()
497-
.type_implements_trait(default_trait, ty, ty::List::empty(), param_env)
498-
.may_apply()
499+
.type_implements_trait(
500+
default_trait,
501+
tcx.erase_regions(ty),
502+
ty::List::empty(),
503+
param_env,
504+
)
505+
.must_apply_modulo_regions()
499506
};
500507

501508
let assign_value = match ty.kind() {

src/test/ui/borrowck/issue-103250.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// edition:2021
2+
3+
type TranslateFn = Box<dyn Fn(String, String) -> String>;
4+
5+
pub struct DeviceCluster {
6+
devices: Vec<Device>,
7+
}
8+
9+
impl DeviceCluster {
10+
pub async fn do_something(&mut self) -> Result<String, Box<dyn std::error::Error>> {
11+
let mut last_error: Box<dyn std::error::Error>;
12+
13+
for device in &mut self.devices {
14+
match device.do_something().await {
15+
Ok(info) => {
16+
return Ok(info);
17+
}
18+
Err(e) => {}
19+
}
20+
}
21+
22+
Err(last_error)
23+
//~^ ERROR used binding `last_error` isn't initialized
24+
}
25+
}
26+
27+
pub struct Device {
28+
translate_fn: Option<TranslateFn>,
29+
}
30+
31+
impl Device {
32+
pub async fn do_something(&mut self) -> Result<String, Box<dyn std::error::Error>> {
33+
Ok(String::from(""))
34+
}
35+
}
36+
37+
fn main() {}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0381]: used binding `last_error` isn't initialized
2+
--> $DIR/issue-103250.rs:22:13
3+
|
4+
LL | let mut last_error: Box<dyn std::error::Error>;
5+
| -------------- binding declared here but left uninitialized
6+
...
7+
LL | Err(last_error)
8+
| ^^^^^^^^^^ `last_error` used here but it isn't initialized
9+
|
10+
help: consider assigning a value
11+
|
12+
LL | let mut last_error: Box<dyn std::error::Error> = todo!();
13+
| +++++++++
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0381`.

0 commit comments

Comments
 (0)