Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Try error messages #43984

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions src/libcore/ops/try.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/// in terms of a success/failure dichotomy. This trait allows both
/// extracting those success or failure values from an existing instance and
/// creating a new instance from a success or failure value.
#[cfg_attr(not(stage0), lang = "try")]
#[unstable(feature = "try_trait", issue = "42327")]
#[rustc_on_unimplemented = "the `?` operator can only be used in a function that returns `Result` \
(or another type that implements `{Try}`)"]
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/middle/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ language_item_table! {

StrEqFnLangItem, "str_eq", str_eq_fn;

TryTraitLangItem, "try", try_trait;

// A number of panic-related lang items. The `panic` item corresponds to
// divide-by-zero and various panic cases with `match`. The
// `panic_bounds_check` item is for indexing arrays.
Expand Down
27 changes: 17 additions & 10 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use infer::type_variable::TypeVariableOrigin;
use rustc::lint::builtin::EXTRA_REQUIREMENT_IN_IMPL;
use std::fmt;
use syntax::ast;
use syntax::codemap::CompilerDesugaringKind;
use ty::{self, AdtKind, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
use ty::error::{ExpectedFound, TypeError};
use ty::fast_reject;
Expand Down Expand Up @@ -585,21 +586,27 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
trait_ref.to_predicate(),
post_message);

let is_desugaring = span
.is_compiler_desugaring(CompilerDesugaringKind::QuestionMark);
let def_id = trait_predicate.skip_binder().def_id();
let unimplemented_note = self.on_unimplemented_note(trait_ref, obligation);
if let Some(ref s) = unimplemented_note {
let help_msg = format!("{}the trait `{}` is not implemented for `{}`",
pre_message,
trait_ref,
trait_ref.self_ty());

if is_desugaring && Some(def_id) == self.tcx.lang_items.try_trait() {
err.span_label(span,
format!("`?` cannot be applied to a value of type `{}`",
trait_ref.self_ty()));
err.help(&help_msg);
} else if let Some(ref s) = unimplemented_note {
// If it has a custom "#[rustc_on_unimplemented]"
// error message, let's display it as the label!
err.span_label(span, s.as_str());
err.help(&format!("{}the trait `{}` is not implemented for `{}`",
pre_message,
trait_ref,
trait_ref.self_ty()));
err.help(&help_msg);
} else {
err.span_label(span,
&*format!("{}the trait `{}` is not implemented for `{}`",
pre_message,
trait_ref,
trait_ref.self_ty()));
err.span_label(span, help_msg);
}

// Try to report a help message
Expand Down
22 changes: 22 additions & 0 deletions src/test/ui/suggestions/try-unimplemented.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
source();
ret();
}

fn source() -> u32 {
3u32?
}

fn ret() {
Ok(3u32)?;
}
26 changes: 26 additions & 0 deletions src/test/ui/suggestions/try-unimplemented.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error[E0277]: the trait bound `u32: std::ops::Try` is not satisfied
--> $DIR/try-unimplemented.rs:17:5
|
17 | 3u32?
| -----
| |
| `?` cannot be applied to a value of type `u32`
| in this macro invocation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, we have to get rid of this "in this macro invocation"....

|
= help: the trait `std::ops::Try` is not implemented for `u32`
= note: required by `std::ops::Try::into_result`

error[E0277]: the trait bound `(): std::ops::Try` is not satisfied
--> $DIR/try-unimplemented.rs:21:5
|
21 | Ok(3u32)?;
| ---------
| |
| cannot use the `?` operator in a function that returns `()`
| in this macro invocation
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`

error: aborting due to 2 previous errors