Skip to content

Commit 4ca8078

Browse files
author
Lukas Markeffsky
committed
fix ICE when suggesting ::new
1 parent 71289c3 commit 4ca8078

File tree

7 files changed

+69
-10
lines changed

7 files changed

+69
-10
lines changed

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,12 +2585,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25852585
.filter(|item| item.is_fn() && !item.is_method())
25862586
.filter_map(|item| {
25872587
// Only assoc fns that return `Self`
2588-
let fn_sig = self.tcx.fn_sig(item.def_id).skip_binder();
2589-
let ret_ty = fn_sig.output();
2590-
let ret_ty = self.tcx.normalize_erasing_late_bound_regions(
2591-
self.typing_env(self.param_env),
2592-
ret_ty,
2593-
);
2588+
let fn_sig = self
2589+
.tcx
2590+
.fn_sig(item.def_id)
2591+
.instantiate(self.tcx, self.fresh_args_for_item(span, item.def_id));
2592+
let ret_ty = self.tcx.instantiate_bound_regions_with_erased(fn_sig.output());
25942593
if !self.can_eq(self.param_env, ret_ty, adt_ty) {
25952594
return None;
25962595
}

tests/ui/privacy/suggest-box-new.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ fn main() {
1616
let _ = std::collections::HashMap {};
1717
//~^ ERROR cannot construct `HashMap<_, _, _>` with struct literal syntax due to private fields
1818
let _ = Box {}; //~ ERROR cannot construct `Box<_, _>` with struct literal syntax due to private fields
19+
20+
// test that we properly instantiate the parameter of `Box::<T>::new` with an inference variable
21+
let _ = Box::<i32> {}; //~ ERROR cannot construct `Box<i32>` with struct literal syntax due to private fields
1922
}

tests/ui/privacy/suggest-box-new.stderr

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,41 @@ LL + let _ = Box::new_zeroed();
118118
LL - let _ = Box {};
119119
LL + let _ = Box::new_in(_, _);
120120
|
121-
= and 12 other candidates
121+
= and 13 other candidates
122122
help: consider using the `Default` trait
123123
|
124124
LL - let _ = Box {};
125125
LL + let _ = <Box as std::default::Default>::default();
126126
|
127127

128-
error: aborting due to 4 previous errors
128+
error: cannot construct `Box<i32>` with struct literal syntax due to private fields
129+
--> $DIR/suggest-box-new.rs:21:13
130+
|
131+
LL | let _ = Box::<i32> {};
132+
| ^^^^^^^^^^
133+
|
134+
= note: private fields `0` and `1` that were not provided
135+
help: you might have meant to use an associated function to build this type
136+
|
137+
LL - let _ = Box::<i32> {};
138+
LL + let _ = Box::<i32>::new(_);
139+
|
140+
LL - let _ = Box::<i32> {};
141+
LL + let _ = Box::<i32>::new_in(_, _);
142+
|
143+
LL - let _ = Box::<i32> {};
144+
LL + let _ = Box::<i32>::into_inner(_);
145+
|
146+
LL - let _ = Box::<i32> {};
147+
LL + let _ = Box::<i32>::write(_, _);
148+
|
149+
= and 4 other candidates
150+
help: consider using the `Default` trait
151+
|
152+
LL - let _ = Box::<i32> {};
153+
LL + let _ = <Box::<i32> as std::default::Default>::default();
154+
|
155+
156+
error: aborting due to 5 previous errors
129157

130158
For more information about this error, try `rustc --explain E0423`.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//! Regression test for <https://github.com/rust-lang/rust/issues/146174>.
2+
//! Ensure that we don't ICE when an associated function returns an associated type.
3+
4+
mod m {
5+
pub trait Project {
6+
type Assoc;
7+
}
8+
pub struct Foo {
9+
_priv: (),
10+
}
11+
impl Foo {
12+
fn new<T: Project>() -> T::Assoc {
13+
todo!()
14+
}
15+
}
16+
}
17+
fn main() {
18+
let _ = m::Foo {}; //~ ERROR: cannot construct `Foo`
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: cannot construct `Foo` with struct literal syntax due to private fields
2+
--> $DIR/suggest-new-projection-ice.rs:18:13
3+
|
4+
LL | let _ = m::Foo {};
5+
| ^^^^^^
6+
|
7+
= note: private field `_priv` that was not provided
8+
9+
error: aborting due to 1 previous error
10+

tests/ui/suggestions/multi-suggestion.ascii.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ LL + let _ = Box::new_zeroed();
118118
LL - let _ = Box {};
119119
LL + let _ = Box::new_in(_, _);
120120
|
121-
= and 12 other candidates
121+
= and 13 other candidates
122122
help: consider using the `Default` trait
123123
|
124124
LL - let _ = Box {};

tests/ui/suggestions/multi-suggestion.unicode.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ LL + let _ = Box::new_zeroed();
118118
LL - let _ = Box {};
119119
LL + let _ = Box::new_in(_, _);
120120
121-
╰ and 12 other candidates
121+
╰ and 13 other candidates
122122
help: consider using the `Default` trait
123123
╭╴
124124
LL - let _ = Box {};

0 commit comments

Comments
 (0)