Skip to content
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
5 changes: 1 addition & 4 deletions compiler/rustc_hir_analysis/src/astconv/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::GenericArg;
use rustc_infer::infer::TyCtxtInferExt;
use rustc_middle::ty::{
self, subst, subst::SubstsRef, GenericParamDef, GenericParamDefKind, IsSuggestable, Ty, TyCtxt,
};
Expand Down Expand Up @@ -83,9 +82,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
Res::Def(DefKind::TyParam, src_def_id) => {
if let Some(param_local_id) = param.def_id.as_local() {
let param_name = tcx.hir().ty_param_name(param_local_id);
let infcx = tcx.infer_ctxt().build();
let param_type =
infcx.resolve_numeric_literals_with_default(tcx.type_of(param.def_id));
let param_type = tcx.type_of(param.def_id);
if param_type.is_suggestable(tcx, false) {
err.span_suggestion(
tcx.def_span(src_def_id),
Expand Down
16 changes: 5 additions & 11 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_hir::{
Expr, ExprKind, GenericBound, Node, Path, QPath, Stmt, StmtKind, TyKind, WherePredicate,
};
use rustc_hir_analysis::astconv::AstConv;
use rustc_infer::infer::{self, TyCtxtInferExt};
use rustc_infer::infer;
use rustc_infer::traits::{self, StatementAsExpression};
use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::{self, Binder, DefIdTree, IsSuggestable, ToPredicate, Ty};
Expand Down Expand Up @@ -921,19 +921,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(self, ty);
let bound_vars = self.tcx.late_bound_vars(fn_id);
let ty = self.tcx.erase_late_bound_regions(Binder::bind_with_vars(ty, bound_vars));
let ty = self.normalize(expr.span, ty);
let ty = match self.tcx.asyncness(fn_id.owner) {
hir::IsAsync::Async => {
let infcx = self.tcx.infer_ctxt().build();
infcx.get_impl_future_output_ty(ty).unwrap_or_else(|| {
span_bug!(
fn_decl.output.span(),
"failed to get output type of async function"
)
})
}
hir::IsAsync::Async => self.get_impl_future_output_ty(ty).unwrap_or_else(|| {
span_bug!(fn_decl.output.span(), "failed to get output type of async function")
}),
hir::IsAsync::NotAsync => ty,
};
let ty = self.normalize(expr.span, ty);
if self.can_coerce(found, ty) {
err.multipart_suggestion(
"you might have meant to return this value",
Expand Down
16 changes: 6 additions & 10 deletions compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{
};
use crate::infer::error_reporting::{TyCategory, TypeAnnotationNeeded as ErrorCode};
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use crate::infer::{self, InferCtxt, TyCtxtInferExt};
use crate::infer::{self, InferCtxt};
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
use crate::traits::query::normalize::QueryNormalizeExt as _;
use crate::traits::specialize::to_pretty_impl_header;
Expand Down Expand Up @@ -1934,14 +1934,6 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
return report(normalized_impl_candidates, err);
}

let normalize = |candidate| {
let infcx = self.tcx.infer_ctxt().build();
infcx
.at(&ObligationCause::dummy(), ty::ParamEnv::empty())
.query_normalize(candidate)
.map_or(candidate, |normalized| normalized.value)
};

// Sort impl candidates so that ordering is consistent for UI tests.
// because the ordering of `impl_candidates` may not be deterministic:
// https://github.com/rust-lang/rust/pull/57475#issuecomment-455519507
Expand All @@ -1951,7 +1943,11 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
let mut normalized_impl_candidates_and_similarities = impl_candidates
.into_iter()
.map(|ImplCandidate { trait_ref, similarity }| {
let normalized = normalize(trait_ref);
// FIXME(compiler-errors): This should be using `NormalizeExt::normalize`
let normalized = self
.at(&ObligationCause::dummy(), ty::ParamEnv::empty())
.query_normalize(trait_ref)
.map_or(trait_ref, |normalized| normalized.value);
(similarity, normalized)
})
.collect::<Vec<_>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// type/region parameters.
let self_ty = obligation.self_ty().skip_binder();
match self_ty.kind() {
ty::Generator(..) => {
// async constructs get lowered to a special kind of generator that
// should *not* `impl Generator`.
ty::Generator(did, ..) if !self.tcx().generator_is_async(*did) => {
debug!(?self_ty, ?obligation, "assemble_generator_candidates",);

candidates.vec.push(GeneratorCandidate);
Expand All @@ -223,6 +225,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
) {
let self_ty = obligation.self_ty().skip_binder();
if let ty::Generator(did, ..) = self_ty.kind() {
// async constructs get lowered to a special kind of generator that
// should directly `impl Future`.
if self.tcx().generator_is_async(*did) {
debug!(?self_ty, ?obligation, "assemble_future_candidates",);

Expand Down
45 changes: 45 additions & 0 deletions src/test/ui/async-await/generator-not-future.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// edition:2018
#![feature(generators, generator_trait)]

use std::future::Future;
use std::ops::Generator;

async fn async_fn() {}
fn returns_async_block() -> impl Future<Output = ()> {
async {}
}
fn returns_generator() -> impl Generator<(), Yield = (), Return = ()> {
|| {
let _: () = yield ();
}
}

fn takes_future(_f: impl Future<Output = ()>) {}
fn takes_generator<ResumeTy>(_g: impl Generator<ResumeTy, Yield = (), Return = ()>) {}

fn main() {
// okay:
takes_future(async_fn());
takes_future(returns_async_block());
takes_future(async {});
takes_generator(returns_generator());
takes_generator(|| {
let _: () = yield ();
});

// async futures are not generators:
takes_generator(async_fn());
//~^ ERROR the trait bound
takes_generator(returns_async_block());
//~^ ERROR the trait bound
takes_generator(async {});
//~^ ERROR the trait bound

// generators are not futures:
takes_future(returns_generator());
//~^ ERROR is not a future
takes_future(|ctx| {
//~^ ERROR is not a future
ctx = yield ();
});
}
81 changes: 81 additions & 0 deletions src/test/ui/async-await/generator-not-future.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
error[E0277]: the trait bound `impl Future<Output = ()>: Generator<_>` is not satisfied
--> $DIR/generator-not-future.rs:31:21
|
LL | takes_generator(async_fn());
| --------------- ^^^^^^^^^^ the trait `Generator<_>` is not implemented for `impl Future<Output = ()>`
| |
| required by a bound introduced by this call
|
note: required by a bound in `takes_generator`
--> $DIR/generator-not-future.rs:18:39
|
LL | fn takes_generator<ResumeTy>(_g: impl Generator<ResumeTy, Yield = (), Return = ()>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `takes_generator`

error[E0277]: the trait bound `impl Future<Output = ()>: Generator<_>` is not satisfied
--> $DIR/generator-not-future.rs:33:21
|
LL | takes_generator(returns_async_block());
| --------------- ^^^^^^^^^^^^^^^^^^^^^ the trait `Generator<_>` is not implemented for `impl Future<Output = ()>`
| |
| required by a bound introduced by this call
|
note: required by a bound in `takes_generator`
--> $DIR/generator-not-future.rs:18:39
|
LL | fn takes_generator<ResumeTy>(_g: impl Generator<ResumeTy, Yield = (), Return = ()>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `takes_generator`

error[E0277]: the trait bound `[async block@$DIR/generator-not-future.rs:35:21: 35:29]: Generator<_>` is not satisfied
--> $DIR/generator-not-future.rs:35:21
|
LL | takes_generator(async {});
| --------------- ^^^^^^^^ the trait `Generator<_>` is not implemented for `[async block@$DIR/generator-not-future.rs:35:21: 35:29]`
| |
| required by a bound introduced by this call
|
note: required by a bound in `takes_generator`
--> $DIR/generator-not-future.rs:18:39
|
LL | fn takes_generator<ResumeTy>(_g: impl Generator<ResumeTy, Yield = (), Return = ()>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `takes_generator`

error[E0277]: `impl Generator<Yield = (), Return = ()>` is not a future
--> $DIR/generator-not-future.rs:39:18
|
LL | takes_future(returns_generator());
| ------------ ^^^^^^^^^^^^^^^^^^^ `impl Generator<Yield = (), Return = ()>` is not a future
| |
| required by a bound introduced by this call
|
= help: the trait `Future` is not implemented for `impl Generator<Yield = (), Return = ()>`
= note: impl Generator<Yield = (), Return = ()> must be a future or must implement `IntoFuture` to be awaited
note: required by a bound in `takes_future`
--> $DIR/generator-not-future.rs:17:26
|
LL | fn takes_future(_f: impl Future<Output = ()>) {}
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `takes_future`

error[E0277]: `[generator@$DIR/generator-not-future.rs:41:18: 41:23]` is not a future
--> $DIR/generator-not-future.rs:41:18
|
LL | takes_future(|ctx| {
| _____------------_^
| | |
| | required by a bound introduced by this call
LL | |
LL | | ctx = yield ();
LL | | });
| |_____^ `[generator@$DIR/generator-not-future.rs:41:18: 41:23]` is not a future
|
= help: the trait `Future` is not implemented for `[generator@$DIR/generator-not-future.rs:41:18: 41:23]`
= note: [generator@$DIR/generator-not-future.rs:41:18: 41:23] must be a future or must implement `IntoFuture` to be awaited
note: required by a bound in `takes_future`
--> $DIR/generator-not-future.rs:17:26
|
LL | fn takes_future(_f: impl Future<Output = ()>) {}
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `takes_future`

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0277`.
17 changes: 16 additions & 1 deletion src/test/ui/return/tail-expr-as-potential-return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// edition:2018

fn main() {
let _ = foo(true);
}

fn foo(x: bool) -> Result<f64, i32> {
Expand All @@ -30,3 +29,19 @@ async fn bar(x: bool) -> Result<f64, i32> {
}
Ok(42.0)
}

trait Identity {
type Out;
}

impl<T> Identity for T {
type Out = T;
}

async fn foo2() -> i32 {
if true {
1i32 //~ ERROR mismatched types
//| HELP you might have meant to return this value
}
0
}
21 changes: 18 additions & 3 deletions src/test/ui/return/tail-expr-as-potential-return.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0308]: mismatched types
--> $DIR/tail-expr-as-potential-return.rs:28:9
--> $DIR/tail-expr-as-potential-return.rs:27:9
|
LL | / if x {
LL | | Err(42)
Expand All @@ -16,7 +16,22 @@ LL | return Err(42);
| ++++++ +

error[E0308]: mismatched types
--> $DIR/tail-expr-as-potential-return.rs:20:9
--> $DIR/tail-expr-as-potential-return.rs:43:9
|
LL | / if true {
LL | | 1i32
| | ^^^^ expected `()`, found `i32`
LL | | //| HELP you might have meant to return this value
LL | | }
| |_____- expected this to be `()`
|
help: you might have meant to return this value
|
LL | return 1i32;
| ++++++ +

error[E0308]: mismatched types
--> $DIR/tail-expr-as-potential-return.rs:19:9
|
LL | / if x {
LL | | Err(42)
Expand All @@ -32,6 +47,6 @@ help: you might have meant to return this value
LL | return Err(42);
| ++++++ +

error: aborting due to 2 previous errors
error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0308`.