Skip to content

E0277 explanation when using optional chaining can be improved #49694

Description

@learnopengles

For this error:

 error[E0277]: the trait bound `std::option::Option<&Symbol>: std::ops::FnOnce<()>` is not satisfied
    --> JackCompiler.rs:302:19
     |
 302 |         let b = a.or_else(self.subroutine_symbols.get(name));
     |                   ^^^^^^^ the trait `std::ops::FnOnce<()>` is not implemented for `std::option::Option<&Symbol>`
 
 error: aborting due to previous error

The compiler can suggest adding a closure here. I'm honestly not sure what the issue is myself and I find optional and result chaining very difficult to use in Rust as compared to a language like Swift. However, changing it to a closure by adding "|| " fixed the error for me.

Activity

  1. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jun 29, 2018
  2. estebank commented on Jul 26, 2018

    @estebank
    Contributor

    The traits Fn:

    pub trait Fn<Args> : FnMut<Args> {

    FnOnce:

    pub trait FnOnce<Args> {

    and FnMut:

    pub trait FnMut<Args> : FnOnce<Args> {

    need to have a #[rustc_on_unimplemented] attribute added, with a note suggesting the use of a closure and a short explanation of why the attempted code doesn't work.

  3. added
    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
    E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
    on Jul 26, 2018
  4. lorian7392 commented on Aug 3, 2018

    @lorian7392

    i'm a noob, but I would like to take this on if nobody else is working on that.

  5. lorian7392 commented on Aug 11, 2018

    @lorian7392

    Hello @estebank , I am sorry that I have not worked before, because this time is working overtime.Now I built the compiler and tried adding the #[rustc_on_unimplemented] property, but I have some questions.
    How does (crate_local, label='xxxx', note='xxx') in #[rustc_on_unimplemented] work? Is there any relevant information? Because I use the method in the example, but it doesn't work. Thank you very much for your help.

  6. estebank commented on Aug 11, 2018

    @estebank
    Contributor

    crate_local filtering was added here. The machinery that parses rustc_on_unmplemented now lives here.

    [crate_local is a flag added in error_reporting.rs only if the current evaluated scope is part of the local crate](

    if let Some(true) = self_ty.ty_adt_def().map(|def| def.did.is_local()) {
    flags.push(("crate_local".to_string(), None));
    }
    being compiled (in other words, your code). This means that if that flag is present, and crate_local rule was in rustc_on_unimplemented, then the following message/label/note will be shown. This is done so we can suggest code changes that are only valid if you have access to the source and we don't suggest, for example, changing code in Diesel, Serde or another dependency. These flags, IIRC, only work with on, as in #[rustc_on_unimlpemented(on(crate_local, note="the note"))].

    Try first annotating with #[rustc_on_unimplemented(note="test")] to see if anything actually comes up, as I am now worried that there might be some special handling of closure traits.

    Regardless, you can filter on any of the type arguments (including Self) as well, in the following way: #[rustc_on_unimlpemented(note="regular note", on(Args="()", note="the note if Fn<()>"))].

    The documentation for the feature is at https://doc.rust-lang.org/unstable-book/language-features/on-unimplemented.html, but it is a bit out of date.

  7. estebank commented on Aug 11, 2018

    @estebank
    Contributor

    Extended the documentation a bit in #53279.

  8. lorian7392 commented on Aug 12, 2018

    @lorian7392

    hi @estebank . i try first annotating with #[rustc_on_unimplemented(note="test")] , but it not work. and
    #[rustc_on_unimplemented = "test"] can work

  9. estebank commented on Aug 12, 2018

    @estebank
    Contributor

    @LuGuoHuas I took a look to see what was needed and arrived at this #53296

  10. added a commit that references this issue on Aug 20, 2018
    ffde96c
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 lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-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

      No branches or pull requests

      Issue actions