Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix ICE when suggesting ::new #146217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
bors merged 1 commit into rust-lang:master from lukas-code:suggest-new-ice
Sep 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions compiler/rustc_hir_typeck/src/expr.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2585,12 +2585,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.filter(|item| item.is_fn() && !item.is_method())
.filter_map(|item| {
// Only assoc fns that return `Self`
let fn_sig = self.tcx.fn_sig(item.def_id).skip_binder();
let ret_ty = fn_sig.output();
let ret_ty = self.tcx.normalize_erasing_late_bound_regions(
self.typing_env(self.param_env),
ret_ty,
);
let fn_sig = self
.tcx
.fn_sig(item.def_id)
.instantiate(self.tcx, self.fresh_args_for_item(span, item.def_id));
let ret_ty = self.tcx.instantiate_bound_regions_with_erased(fn_sig.output());
if !self.can_eq(self.param_env, ret_ty, adt_ty) {
return None;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/privacy/suggest-box-new.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ fn main() {
let _ = std::collections::HashMap {};
//~^ ERROR cannot construct `HashMap<_, _, _>` with struct literal syntax due to private fields
let _ = Box {}; //~ ERROR cannot construct `Box<_, _>` with struct literal syntax due to private fields

// test that we properly instantiate the parameter of `Box::<T>::new` with an inference variable
let _ = Box::<i32> {}; //~ ERROR cannot construct `Box<i32>` with struct literal syntax due to private fields
}
32 changes: 30 additions & 2 deletions tests/ui/privacy/suggest-box-new.stderr
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,41 @@ LL + let _ = Box::new_zeroed();
LL - let _ = Box {};
LL + let _ = Box::new_in(_, _);
|
= and 12 other candidates
= and 13 other candidates
help: consider using the `Default` trait
|
LL - let _ = Box {};
LL + let _ = <Box as std::default::Default>::default();
|

error: aborting due to 4 previous errors
error: cannot construct `Box<i32>` with struct literal syntax due to private fields
--> $DIR/suggest-box-new.rs:21:13
|
LL | let _ = Box::<i32> {};
| ^^^^^^^^^^
|
= note: private fields `0` and `1` that were not provided
help: you might have meant to use an associated function to build this type
|
LL - let _ = Box::<i32> {};
LL + let _ = Box::<i32>::new(_);
|
LL - let _ = Box::<i32> {};
LL + let _ = Box::<i32>::new_in(_, _);
|
LL - let _ = Box::<i32> {};
LL + let _ = Box::<i32>::into_inner(_);
|
LL - let _ = Box::<i32> {};
LL + let _ = Box::<i32>::write(_, _);
|
= and 4 other candidates
help: consider using the `Default` trait
|
LL - let _ = Box::<i32> {};
LL + let _ = <Box::<i32> as std::default::Default>::default();
|

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0423`.
19 changes: 19 additions & 0 deletions tests/ui/privacy/suggest-new-projection-ice.rs
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! Regression test for <https://github.com/rust-lang/rust/issues/146174>.
//! Ensure that we don't ICE when an associated function returns an associated type.

mod m {
pub trait Project {
type Assoc;
}
pub struct Foo {
_priv: (),
}
impl Foo {
fn new<T: Project>() -> T::Assoc {
todo!()
}
}
}
fn main() {
let _ = m::Foo {}; //~ ERROR: cannot construct `Foo`
}
10 changes: 10 additions & 0 deletions tests/ui/privacy/suggest-new-projection-ice.stderr
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: cannot construct `Foo` with struct literal syntax due to private fields
--> $DIR/suggest-new-projection-ice.rs:18:13
|
LL | let _ = m::Foo {};
| ^^^^^^
|
= note: private field `_priv` that was not provided

error: aborting due to 1 previous error

2 changes: 1 addition & 1 deletion tests/ui/suggestions/multi-suggestion.ascii.stderr
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ LL + let _ = Box::new_zeroed();
LL - let _ = Box {};
LL + let _ = Box::new_in(_, _);
|
= and 12 other candidates
= and 13 other candidates
help: consider using the `Default` trait
|
LL - let _ = Box {};
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/suggestions/multi-suggestion.unicode.stderr
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ LL + let _ = Box::new_zeroed();
LL - let _ = Box {};
LL + let _ = Box::new_in(_, _);
╰ and 12 other candidates
╰ and 13 other candidates
help: consider using the `Default` trait
╭╴
LL - let _ = Box {};
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /