From 5309375d2c8d51dafedb5404f0fb49f402350394 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 10 Aug 2022 20:53:06 +0000 Subject: [PATCH] Do not report cycle error when inferring return type for suggestion --- Cargo.lock | 1 + compiler/rustc_middle/src/query/mod.rs | 1 + compiler/rustc_query_impl/Cargo.toml | 1 + compiler/rustc_query_impl/src/values.rs | 20 +++++++++++++++++++ src/test/ui/suggestions/return-cycle-2.rs | 14 +++++++++++++ src/test/ui/suggestions/return-cycle-2.stderr | 12 +++++++++++ src/test/ui/suggestions/return-cycle.rs | 14 +++++++++++++ src/test/ui/suggestions/return-cycle.stderr | 12 +++++++++++ 8 files changed, 75 insertions(+) create mode 100644 src/test/ui/suggestions/return-cycle-2.rs create mode 100644 src/test/ui/suggestions/return-cycle-2.stderr create mode 100644 src/test/ui/suggestions/return-cycle.rs create mode 100644 src/test/ui/suggestions/return-cycle.stderr diff --git a/Cargo.lock b/Cargo.lock index 395f5a127bd3d..ba81fc6214155 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4359,6 +4359,7 @@ dependencies = [ "rustc_serialize", "rustc_session", "rustc_span", + "rustc_target", "tracing", ] diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index cfc75f673c8f5..8a727d9a1825a 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -770,6 +770,7 @@ rustc_queries! { desc { |tcx| "computing function signature of `{}`", tcx.def_path_str(key) } cache_on_disk_if { key.is_local() } separate_provide_extern + cycle_delay_bug } /// Performs lint checking for the module. diff --git a/compiler/rustc_query_impl/Cargo.toml b/compiler/rustc_query_impl/Cargo.toml index 5673bb83b15f4..c37ae4f32536b 100644 --- a/compiler/rustc_query_impl/Cargo.toml +++ b/compiler/rustc_query_impl/Cargo.toml @@ -20,6 +20,7 @@ rustc_query_system = { path = "../rustc_query_system" } rustc_serialize = { path = "../rustc_serialize" } rustc_session = { path = "../rustc_session" } rustc_span = { path = "../rustc_span" } +rustc_target = { path = "../rustc_target" } tracing = "0.1" [features] diff --git a/compiler/rustc_query_impl/src/values.rs b/compiler/rustc_query_impl/src/values.rs index 718a2971c403d..0ed48f8d4a051 100644 --- a/compiler/rustc_query_impl/src/values.rs +++ b/compiler/rustc_query_impl/src/values.rs @@ -43,3 +43,23 @@ impl<'tcx> Value<'tcx> for AdtSizedConstraint<'_> { } } } + +impl<'tcx> Value<'tcx> for ty::Binder<'_, ty::FnSig<'_>> { + fn from_cycle_error(tcx: QueryCtxt<'tcx>) -> Self { + let err = tcx.ty_error(); + // FIXME(compiler-errors): It would be nice if we could get the + // query key, so we could at least generate a fn signature that + // has the right arity. + let fn_sig = ty::Binder::dummy(tcx.mk_fn_sig( + [].into_iter(), + err, + false, + rustc_hir::Unsafety::Normal, + rustc_target::spec::abi::Abi::Rust, + )); + + // SAFETY: This is never called when `Self` is not `ty::Binder<'tcx, ty::FnSig<'tcx>>`. + // FIXME: Represent the above fact in the trait system somehow. + unsafe { std::mem::transmute::, ty::Binder<'_, ty::FnSig<'_>>>(fn_sig) } + } +} diff --git a/src/test/ui/suggestions/return-cycle-2.rs b/src/test/ui/suggestions/return-cycle-2.rs new file mode 100644 index 0000000000000..d6d24be1b8dc4 --- /dev/null +++ b/src/test/ui/suggestions/return-cycle-2.rs @@ -0,0 +1,14 @@ +use std::marker::PhantomData; + +struct Token(PhantomData); + +impl Token { + fn as_ref(_: i32, _: i32) -> _ { + //~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types + //~| NOTE not allowed in type signatures + //~| HELP replace with the correct return type + Token(PhantomData::<&T>) + } +} + +fn main() {} diff --git a/src/test/ui/suggestions/return-cycle-2.stderr b/src/test/ui/suggestions/return-cycle-2.stderr new file mode 100644 index 0000000000000..3a1a0f7f4f543 --- /dev/null +++ b/src/test/ui/suggestions/return-cycle-2.stderr @@ -0,0 +1,12 @@ +error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types + --> $DIR/return-cycle-2.rs:6:34 + | +LL | fn as_ref(_: i32, _: i32) -> _ { + | ^ + | | + | not allowed in type signatures + | help: replace with the correct return type: `Token<&'static T>` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0121`. diff --git a/src/test/ui/suggestions/return-cycle.rs b/src/test/ui/suggestions/return-cycle.rs new file mode 100644 index 0000000000000..60b80e35a20b8 --- /dev/null +++ b/src/test/ui/suggestions/return-cycle.rs @@ -0,0 +1,14 @@ +use std::marker::PhantomData; + +struct Token(PhantomData); + +impl Token { + fn new() -> _ { + //~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types + //~| NOTE not allowed in type signatures + //~| HELP replace with the correct return type + Token(PhantomData::<()>) + } +} + +fn main() {} diff --git a/src/test/ui/suggestions/return-cycle.stderr b/src/test/ui/suggestions/return-cycle.stderr new file mode 100644 index 0000000000000..63fa9e040874d --- /dev/null +++ b/src/test/ui/suggestions/return-cycle.stderr @@ -0,0 +1,12 @@ +error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types + --> $DIR/return-cycle.rs:6:17 + | +LL | fn new() -> _ { + | ^ + | | + | not allowed in type signatures + | help: replace with the correct return type: `Token<()>` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0121`.