From 0e82aaeb6799041991c54fb9ff37b5c20a5c6146 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 9 Jan 2024 18:13:19 +0000 Subject: [PATCH] Avoid follow up errors --- .../src/collect/type_of/opaque.rs | 15 +++++++++++ tests/ui/type-alias-impl-trait/issue-77179.rs | 2 -- .../type-alias-impl-trait/issue-77179.stderr | 26 ++----------------- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs b/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs index da7279967dac1..1f7ca48234a24 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs @@ -134,6 +134,21 @@ impl TaitConstraintLocator<'_> { debug!("no constraint: no typeck results"); return; } + + if let Some(hir_sig) = self.tcx.hir_node_by_def_id(item_def_id).fn_decl() { + if hir_sig.output.get_infer_ret_ty().is_some() { + let guar = self.tcx.dcx().span_delayed_bug( + hir_sig.output.span(), + "inferring return types and opaque types do not mix well", + ); + self.found = Some(ty::OpaqueHiddenType { + span: DUMMY_SP, + ty: Ty::new_error(self.tcx, guar), + }); + return; + } + } + // Calling `mir_borrowck` can lead to cycle errors through // const-checking, avoid calling it if we don't have to. // ```rust diff --git a/tests/ui/type-alias-impl-trait/issue-77179.rs b/tests/ui/type-alias-impl-trait/issue-77179.rs index 6e2ce76632fdc..093aeb4b27911 100644 --- a/tests/ui/type-alias-impl-trait/issue-77179.rs +++ b/tests/ui/type-alias-impl-trait/issue-77179.rs @@ -6,9 +6,7 @@ type Pointer = impl std::ops::Deref; fn test() -> Pointer<_> { //~^ ERROR: the placeholder `_` is not allowed within types - //~| ERROR: non-defining opaque type use in defining scope Box::new(1) - //~^ ERROR expected generic type parameter, found `i32` } fn main() { diff --git a/tests/ui/type-alias-impl-trait/issue-77179.stderr b/tests/ui/type-alias-impl-trait/issue-77179.stderr index c5b7c4178e473..68dd6570d00c0 100644 --- a/tests/ui/type-alias-impl-trait/issue-77179.stderr +++ b/tests/ui/type-alias-impl-trait/issue-77179.stderr @@ -7,28 +7,6 @@ LL | fn test() -> Pointer<_> { | | not allowed in type signatures | help: replace with the correct return type: `Pointer` -error[E0792]: non-defining opaque type use in defining scope - --> $DIR/issue-77179.rs:7:14 - | -LL | fn test() -> Pointer<_> { - | ^^^^^^^^^^ argument `i32` is not a generic parameter - | -note: for this opaque type - --> $DIR/issue-77179.rs:5:19 - | -LL | type Pointer = impl std::ops::Deref; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error[E0792]: expected generic type parameter, found `i32` - --> $DIR/issue-77179.rs:10:5 - | -LL | type Pointer = impl std::ops::Deref; - | - this generic parameter must be used with a generic type parameter -... -LL | Box::new(1) - | ^^^^^^^^^^^ - -error: aborting due to 3 previous errors +error: aborting due to 1 previous error -Some errors have detailed explanations: E0121, E0792. -For more information about an error, try `rustc --explain E0121`. +For more information about this error, try `rustc --explain E0121`.