Skip to content

Extend rustc_on_unimplemented to allow pointing at enclosing function/closure #61709

Closed
@estebank

Description

@estebank
Contributor

We should extend rustc_on_unimplemented to be able to point at the enclosing scope. With that capability, we could turn the following:

error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
  --> src/lib.rs:28:31
   |
28 |                     let end = parse_range_u32(end)?;
   |                               ^^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `std::ops::RangeInclusive<u32>`
   |
   = help: the trait `std::ops::Try` is not implemented for `std::ops::RangeInclusive<u32>`
   = note: required by `std::ops::Try::from_error`

into something along the lines of

error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
  --> src/lib.rs:28:31
   |
24 |          .map(|range_spec| {
   |               -
   | ______________|
... |
28 ||                     let end = parse_range_u32(end)?;
   ||                               ^^^^^^^^^^^^^^^^^^^^^ cannot use the `?` operator in a function that returns `std::ops::RangeInclusive<u32>`
... |
48 ||         })
   ||         - this function should return `Result` or `Option` to accept `?`
   ||_________|
   |
   = help: the trait `std::ops::Try` is not implemented for `std::ops::RangeInclusive<u32>`
   = note: required by `std::ops::Try::from_error`

This has caused confusion in the wild.

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
P-lowLow priority
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.
on Jun 10, 2019
added
F-on_unimplementedError messages that can be tackled with `#[rustc_on_unimplemented]`
on Aug 4, 2019
sam09

sam09 commented on Oct 2, 2019

@sam09
Contributor

@estebank Can I pick this up? I don't have much of an idea about this, but would like to get involved

estebank

estebank commented on Oct 2, 2019

@estebank
ContributorAuthor

@sam09 of course! I can take a look at #54946 and #47613 for some changes made to this code, the currently up to date documentation for the feature. You will probably have to take a look at fn report_selection_error and find a way to get the enclosing scope to the current item. This part will likely be hard given that this function only deals with obligations, doesn't have access to the originating item. The most likely successful course of action would be to extend the obligation itself to keep some information (most likely just a span) of the enclosing scope. The you could extend rustc_on_unimplemented to be something like

#[rustc_on_unimplemented(
   on(all(
       any(from_method="from_error", from_method="from_ok"),
       from_desugaring="QuestionMark"),
      message="the `?` operator can only be used in a \
               function that returns `Result` or `Option` \
               (or another type that implements `{Try}`)",
      label="cannot use the `?` operator in a function that returns `{Self}`",
      enclosing_scope="this should return `Result` or `Option`"), // <--- This is new
   on(all(from_method="into_result", from_desugaring="QuestionMark"),
      message="the `?` operator can only be applied to values \
               that implement `{Try}`",
      label="the `?` operator cannot be applied to type `{Self}`")
)]
#[doc(alias = "?")]
pub trait Try 
basil-cow

basil-cow commented on Nov 20, 2019

@basil-cow
Contributor

@sam09 would you mind me picking this up?

sam09

sam09 commented on Nov 21, 2019

@sam09
Contributor

@Areredify @estebank
Apologies for not following up. I have been super busy with some things at work.
@Areredify Please go ahead. I haven't found any time to work on this 😄

basil-cow

basil-cow commented on Nov 22, 2019

@basil-cow
Contributor

@estebank I implemented it, but I don't know how to compile it, since I changed both libstd(added an annotation to the Try trait) and the compiler.

added 2 commits that reference this issue on Dec 3, 2019

Rollup merge of rust-lang#66651 - Areredify:on-unimplemented-scope, r…

4b6ceb1

Rollup merge of rust-lang#66651 - Areredify:on-unimplemented-scope, r…

8dacfc2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsE-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.F-on_unimplementedError messages that can be tackled with `#[rustc_on_unimplemented]`P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @estebank@sam09@basil-cow

      Issue actions

        Extend `rustc_on_unimplemented` to allow pointing at enclosing function/closure · Issue #61709 · rust-lang/rust