Skip to content
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

Fix ICE when return impl ?Sized #97413

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 20 additions & 0 deletions compiler/rustc_typeck/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use super::compare_method::check_type_bounds;
use super::compare_method::{compare_const_impl, compare_impl_method, compare_ty_impl};
use super::*;

use hir::{GenericBound, PolyTraitRef, TraitBoundModifier};
use rustc_attr as attr;
use rustc_errors::{Applicability, ErrorGuaranteed, MultiSpan};
use rustc_hir as hir;
Expand Down Expand Up @@ -96,6 +97,25 @@ pub(super) fn check_fn<'a, 'tcx>(

let declared_ret_ty = fn_sig.output();

if let ty::Opaque(def_id, _) = *declared_ret_ty.kind() {
let bounds = match &tcx.hir().expect_item(def_id.expect_local()).kind {
ItemKind::OpaqueTy(ty) => ty.bounds,
_ => bug!("unexpected opaque type"),
};

let sized_def_id = tcx.require_lang_item(LangItem::Sized, None);
if bounds.iter().any(|bound| match bound {
GenericBound::Trait(PolyTraitRef { trait_ref, .. }, TraitBoundModifier::Maybe)
if trait_ref.path.res.def_id() == sized_def_id =>
{
true
}
_ => false,
}) {
sess.span_err(decl.output.span(), "return type should be sized");
}
}

let ret_ty =
fcx.register_infer_ok_obligations(fcx.infcx.replace_opaque_types_with_inference_vars(
declared_ret_ty,
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/return/return-impl-maybe-sized.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Regression test for issue #97226

fn test_fn() -> impl ?Sized {} //~ ERROR return type should be sized

fn main() {}
8 changes: 8 additions & 0 deletions src/test/ui/return/return-impl-maybe-sized.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: return type should be sized
--> $DIR/return-impl-maybe-sized.rs:3:17
|
LL | fn test_fn() -> impl ?Sized {}
| ^^^^^^^^^^^

error: aborting due to previous error