Skip to content

Commit

Permalink
Rollup merge of #100389 - compiler-errors:return-type-suggestion-cycl…
Browse files Browse the repository at this point in the history
…e, r=cjgillot

Do not report cycle error when inferring return type for suggestion

The UI test is a good example of a case where this happens. The cycle is due to needing the value of the return type `-> _` to compute the variances of items in the crate, but then needing the variances of the items in the crate to do typechecking to infer what `-> _`'s real type is.

Since we're already gonna emit an error in astconv, just delay the cycle bug as an error.
  • Loading branch information
matthiaskrgr authored Aug 17, 2022
2 parents 989e4ff + 5309375 commit 1948288
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4359,6 +4359,7 @@ dependencies = [
"rustc_serialize",
"rustc_session",
"rustc_span",
"rustc_target",
"tracing",
]

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_query_impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
20 changes: 20 additions & 0 deletions compiler/rustc_query_impl/src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::PolyFnSig<'tcx>, ty::Binder<'_, ty::FnSig<'_>>>(fn_sig) }
}
}
14 changes: 14 additions & 0 deletions src/test/ui/suggestions/return-cycle-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::marker::PhantomData;

struct Token<T>(PhantomData<T>);

impl<T> Token<T> {
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() {}
12 changes: 12 additions & 0 deletions src/test/ui/suggestions/return-cycle-2.stderr
Original file line number Diff line number Diff line change
@@ -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`.
14 changes: 14 additions & 0 deletions src/test/ui/suggestions/return-cycle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::marker::PhantomData;

struct Token<T>(PhantomData<T>);

impl<T> Token<T> {
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() {}
12 changes: 12 additions & 0 deletions src/test/ui/suggestions/return-cycle.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 1948288

Please sign in to comment.