Skip to content

Commit

Permalink
Rollup merge of rust-lang#58204 - estebank:impl-trait-semi, r=zackmdavis
Browse files Browse the repository at this point in the history
On return type `impl Trait` for block with no expr point at last semi

Partial solution, doesn't actually validate that the last statement in the function body can satisfy the trait bound, but it's a good incremental improvement over the status quo.

```
error[E0277]: the trait bound `(): Bar` is not satisfied
  --> $DIR/impl-trait-return-trailing-semicolon.rs:3:13
   |
LL | fn foo() -> impl Bar {
   |             ^^^^^^^^ the trait `Bar` is not implemented for `()`
LL |     5;
   |      - consider removing this semicolon
   |
   = note: the return type of a function must have a statically known size
```

Partially addresses rust-lang#54771.
  • Loading branch information
Centril committed Feb 13, 2019
2 parents a603e2a + 0afda05 commit 7579ba9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,11 +598,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
}
}

pub fn report_selection_error(&self,
obligation: &PredicateObligation<'tcx>,
error: &SelectionError<'tcx>,
fallback_has_occurred: bool)
{
pub fn report_selection_error(
&self,
obligation: &PredicateObligation<'tcx>,
error: &SelectionError<'tcx>,
fallback_has_occurred: bool,
) {
let span = obligation.cause.span;

let mut err = match *error {
Expand Down Expand Up @@ -647,6 +648,28 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
trait_ref.to_predicate(), post_message)
));

let parent_node = self.tcx.hir().get_parent_node(obligation.cause.body_id);
let node = self.tcx.hir().find(parent_node);
if let Some(hir::Node::Item(hir::Item {
node: hir::ItemKind::Fn(decl, _, _, body_id),
..
})) = node {
let body = self.tcx.hir().body(*body_id);
if let hir::ExprKind::Block(blk, _) = &body.value.node {
if decl.output.span().overlaps(span) && blk.expr.is_none() &&
"()" == &trait_ref.self_ty().to_string()
{
// FIXME(estebank): When encountering a method with a trait
// bound not satisfied in the return type with a body that has
// no return, suggest removal of semicolon on last statement.
// Once that is added, close #54771.
if let Some(ref stmt) = blk.stmts.last() {
let sp = self.tcx.sess.source_map().end_point(stmt.span);
err.span_label(sp, "consider removing this semicolon");
}
}
}
}
let explanation =
if obligation.cause.code == ObligationCauseCode::MainFunctionType {
"consider using `()`, or a `Result`".to_owned()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
trait Bar {}
impl Bar for u8 {}
fn foo() -> impl Bar {
5; //~^ ERROR the trait bound `(): Bar` is not satisfied
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0277]: the trait bound `(): Bar` is not satisfied
--> $DIR/impl-trait-return-trailing-semicolon.rs:3:13
|
LL | fn foo() -> impl Bar {
| ^^^^^^^^ the trait `Bar` is not implemented for `()`
LL | 5; //~^ ERROR the trait bound `(): Bar` is not satisfied
| - consider removing this semicolon
|
= note: the return type of a function must have a statically known size

error: aborting due to previous error

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

0 comments on commit 7579ba9

Please sign in to comment.