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 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
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
6 changes: 4 additions & 2 deletions src/librustc_errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use self::Destination::*;

use syntax_pos::{DUMMY_SP, FileMap, Span, MultiSpan, CharPos};
use syntax_pos::{DUMMY_SP, FileMap, Span, MultiSpan, CharPos, CompilerDesugaringKind};

use {Level, CodeSuggestion, DiagnosticBuilder, SubDiagnostic, CodeMapper};
use RenderSpan::*;
Expand Down Expand Up @@ -724,7 +724,9 @@ impl EmitterWriter {

// First, find all the spans in <*macros> and point instead at their use site
for sp in span.primary_spans() {
if *sp == DUMMY_SP {
if *sp == DUMMY_SP ||
sp.is_compiler_desugaring(CompilerDesugaringKind::QuestionMark)
Copy link
Contributor

Choose a reason for hiding this comment

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

Cool =)

{
continue;
}
let call_sp = cm.call_span_if_macro(*sp);
Expand Down
1 change: 0 additions & 1 deletion src/test/ui/suggestions/try-operator-on-main.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ error[E0277]: the trait bound `(): std::ops::Try` is not satisfied
| ---------------------------
| |
| the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`)
| in this macro invocation
|
= help: the trait `std::ops::Try` is not implemented for `()`
= note: required by `std::ops::Try::from_error`
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)?;
}
20 changes: 20 additions & 0 deletions src/test/ui/suggestions/try-unimplemented.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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`
|
= 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 `()`
Copy link
Contributor

Choose a reason for hiding this comment

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

is this...aspirational?

Copy link
Contributor

Choose a reason for hiding this comment

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

i.e., I don't see code that would emit this "cannot use" error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was intended to test Return type does not implement Try. Looking at try-operator-on-main, it has a pretty similiar error message, so maybe it was decided to use that error message instead.

|
= 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