Skip to content

regression in method dispatch  #44224

Closed
Closed

Description

@nikomatsakis

According to @SimonSapin, #43880 likely broke Servo's build. Servo itself is fixed in servo/servo#18327, but I'm opening this issue to decide on whether this regression is a bug or expected behavior.

cc @arielb1

Activity

  1. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Aug 31, 2017
  2. SimonSapin commented on Sep 1, 2017

    @SimonSapin
    Contributor

    The code that broke is:

    #[derive(JSTraceable)]
    pub enum Filter {
        None,
        Native(fn (node: &Node) -> u16),
        JS(Rc<NodeFilter>)
    }
    /// A trait to allow tracing (only) DOM objects.
    pub unsafe trait JSTraceable {
        /// Trace `self`.
        unsafe fn trace(&self, trc: *mut JSTracer);
    }
    
    unsafe impl<A, B> JSTraceable for fn(A) -> B {
        #[inline]
        unsafe fn trace(&self, _: *mut JSTracer) {
            // Do nothing
        }
    }
    #[proc_macro_derive(JSTraceable)]
    pub fn expand_token_stream(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    
        // ...
    
        let style = synstructure::BindStyle::Ref.into();
        let match_body = synstructure::each_field(&mut type_, &style, |binding| {
            Some(quote! { #binding.trace(tracer); })
        });
    
        // ...

    I think the generated code would look something like this:

    unsafe impl JSTraceable for Filter {
        unsafe fn trace(&self, tracer: *mut JSTracer) {
            match *self {
                Filter::Native(ref field1) => {
                    field1.trace(tracer);
                }
                // ...
            }
        }
    }

    The error message in rustc 1.21.0-nightly (7eeac1b 2017-08-30) was:

       Compiling script v0.0.1 (file:///home/simon/servo2/components/script)
    error[E0599]: no method named `trace` found for type `&fn(&dom::node::Node) -> u16` in the current scope
       --> /home/simon/servo2/components/script/dom/treewalker.rs:464:10
        |
    464 | #[derive(JSTraceable)]
        |          ^^^^^^^^^^^
        |
        = note: JSTraceable is a function, perhaps you wish to call it
        = help: items from traits can only be used if the trait is implemented and in scope
        = note: the following trait defines an item `trace`, perhaps you need to implement it:
                candidate #1: `dom::bindings::trace::JSTraceable`
    
    error: aborting due to previous error
    
    error: Could not compile `script`.

    The fix was adding a new impl for fn(&A) -> B, in addition to the one for fn(A) -> B. Isn’t the former a subset of the types covered by the latter?

    unsafe impl<'a, A, B> JSTraceable for fn(&A) -> B {
        #[inline]
        unsafe fn trace(&self, _: *mut JSTracer) {
            // Do nothing
        }
    }
  3. added this to the 1.22 milestone on Sep 2, 2017
  4. nikomatsakis commented on Sep 5, 2017

    @nikomatsakis
    ContributorAuthor

    @SimonSapin thanks for the detailed notes.

    I was talking to @arielb1 and I understand better the reason for the change. I am of two minds about it, to be honest. In short, under the older algorithm, we integrate "impl selection" directly into method dispatch. In particular, for each potentially applicable impl, we would instantiate the impl and then check whether the receiver type was a valid subtype of what the impl expected. In this case, the receiver type is for<'a> fn(&'a u8). This is a valid subtype of fn(A) where A = &'x u8, because 'a can be instantiated as 'x. Under the newer algorithm, we use trait selection instead. This creates a problem: an impl like impl<A> Foo for fn(A) doesn't implement Foo for for<'a> fn(&u8), because of the placement of the region binders.

    This handling of region binders is a subtle point in Rust, and one that I actually consider a borderline bug. In particular, I'm in the process of various refactorings that aim to make the internal handling of higher-ranked things more consistent with all other regions, which offers a lot of simplicity across the board. But one side-effect is that we would no longer permit an impl of Foo for both fn(&u8) and fn(A) -- in other words, the workaround that @SimonSapin put in place would no longer work, which seems suboptimal. (In particular, I aim to have region erasure erase region bindings altogether.)

    (It may be that these refactorings I would like to do can't be done the way I wanted to do them, though, since there could be other code relying on the existing behavior; it's hard to be sure without a working branch to test with (which I think is close)).

  5. SimonSapin commented on Sep 5, 2017

    @SimonSapin
    Contributor

    because of the placement of the region binders.

    I don’t understand what that means. Is this a concept of the language that is relevant to its users, or only an internal detail of the compiler’s implementation?

    But one side-effect is that we would no longer permit an impl of Foo for both fn(&u8) and fn(A)

    Right, #43880 was a breaking change so effectively reverting it is another breaking change (unless it happens within the same release cycle).

    The team needs to decide whether this breakage is acceptable per https://github.com/rust-lang/rfcs/blob/master/text/1122-language-semver.md.

  6. SimonSapin commented on Sep 5, 2017

    @SimonSapin
    Contributor

    And the larger policy question is: “it doesn’t affect any code that happens to be tested by crater/cargobomb” sufficient to justify any breaking change, even if it not a soundness fix?

  7. nikomatsakis commented on Sep 5, 2017

    @nikomatsakis
    ContributorAuthor

    @SimonSapin

    I don’t understand what that means. Is this a concept of the language that is relevant to its users, or only an internal detail of the compiler’s implementation?

    Sorry, I geeked out a bit in the response there. The answer is somewhere in the middle. In short, there are corner cases of the language that interact with particulars in the implementation. Sometimes this gives rise to complicated or internally inconsistent definitions. The goal of #43880 was effectively to remove some "conceptual duplication" between how trait matching worked during method dispatch and how it works at other times.

    The changes I was describing around subtyping and higher-ranked regions are another such case. They are independent except that they both interact on this particular example.

    Right, #43880 was a breaking change so effectively reverting it is another breaking change (unless it happens within the same release cycle).

    This does not necessarily -- that is, I don't know that @arielb1's new method dispatch code accepts anything in particular that the old code did not, but I could be wrong.

    The team needs to decide whether this breakage is acceptable per https://github.com/rust-lang/rfcs/blob/master/text/1122-language-semver.md.

    Indeed, we do. In my view, all of the changes under discussion fall roughly under the "Underspecified language semantics" heading. This is not to say we should necessarily make those changes -- just that they are the kinds of changes that arise we seek to clean up the compiler implementation and the definition of the trait system. In such cases, the "real-world effects" (i.e., how much code is affected) is definitely a consideration, as is the magnitude of the cruft.

  8. SimonSapin commented on Sep 5, 2017

    @SimonSapin
    Contributor

    In my view, all of the changes under discussion fall roughly under the "Underspecified language semantics" heading.

    Alright, that's fair enough.

    And as @aturon just said on IRC it's also the role of Nightly to catch issues like this that might have slipped through cargobomb, with time before they reach Stable.

  9. removed
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Sep 14, 2017
  10. nikomatsakis commented on Sep 14, 2017

    @nikomatsakis
    ContributorAuthor

    @rfcbot fcp close

    OK, having had time to think this over, I think the general feeling is that the change here was both permitted and a good change, although the regression is unfortunate. The intention is to clarify and improve method dispatch, and in particular to avoid having two "similar but different in subtle ways" implementations of how to match a trait against impls.

    This does however mean that the behavior with respect to function types, consolidating on the behavior using during trait selection (which hence requires that the type of receiver be equal to the type in the trait impl, rather than being a subtype).

    I'm going to move that we close this issue, therefore.

  11. rfcbot commented on Sep 14, 2017

    @rfcbot

    Team member @nikomatsakis has proposed to close this. The next step is review by the rest of the tagged teams:

    No concerns currently listed.

    Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

    See this document for info about what commands tagged team members can give me.

  12. 11 remaining items

  13. cramertj commented on Sep 18, 2017

    @cramertj
    Member

    @rfcbot reviewed

    (I don't have permission to check boxes in rust-lang/rust)

  14. eddyb commented on Sep 18, 2017

    @eddyb
    Contributor

    @cramertj Pretty sure that permission is literally "editing anyone else's comments".

    • yup
  15. cramertj commented on Sep 18, 2017

    @cramertj
    Member

    @eddyb Yes, but I have that permission in rust-lang/rfcs 😄

  16. aturon commented on Oct 19, 2017

    @aturon
    Contributor

    Checked for @nrc, who is away.

  17. added
    final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
    and removed
    proposed-final-comment-periodProposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.
    on Oct 19, 2017
  18. rfcbot commented on Oct 19, 2017

    @rfcbot

    🔔 This is now entering its final comment period, as per the review above. 🔔

  19. rfcbot commented on Oct 29, 2017

    @rfcbot

    The final comment period is now complete.

  20. added a commit that references this issue on Nov 3, 2017
  21. nikomatsakis commented on Nov 9, 2017

    @nikomatsakis
    ContributorAuthor

    Closing as resolved.

  22. added a commit that references this issue on May 11, 2026
  23. added a commit that references this issue on May 13, 2026
  24. added a commit that references this issue on Oct 13, 2026
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

    C-bugCategory: This is a bug.T-langRelevant to the language teamfinal-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions