- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Make object bound candidates sound in the new trait solver #108333
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
          
     Merged
      
      
            bors
  merged 3 commits into
  rust-lang:master
from
compiler-errors:new-solver-object-sound
  
      
      
   
  Feb 26, 2023 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            3 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,7 @@ | ||||||||||
| use rustc_hir::{Movability, Mutability}; | ||||||||||
| use rustc_data_structures::fx::FxHashMap; | ||||||||||
| use rustc_hir::{def_id::DefId, Movability, Mutability}; | ||||||||||
| use rustc_infer::traits::query::NoSolution; | ||||||||||
| use rustc_middle::ty::{self, Ty, TyCtxt}; | ||||||||||
| use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable}; | ||||||||||
|  | ||||||||||
| use crate::solve::EvalCtxt; | ||||||||||
|  | ||||||||||
|  | @@ -233,3 +234,112 @@ pub(crate) fn extract_tupled_inputs_and_output_from_callable<'tcx>( | |||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|  | ||||||||||
| /// Assemble a list of predicates that would be present on a theoretical | ||||||||||
| /// user impl for an object type. These predicates must be checked any time | ||||||||||
| /// we assemble a built-in object candidate for an object type, since they | ||||||||||
| /// are not implied by the well-formedness of the type. | ||||||||||
| /// | ||||||||||
| /// For example, given the following traits: | ||||||||||
| /// | ||||||||||
| /// ```rust,ignore (theoretical code) | ||||||||||
| /// trait Foo: Baz { | ||||||||||
| /// type Bar: Copy; | ||||||||||
| /// } | ||||||||||
| /// | ||||||||||
| /// trait Baz {} | ||||||||||
| /// ``` | ||||||||||
| /// | ||||||||||
| /// For the dyn type `dyn Foo<Item = Ty>`, we can imagine there being a | ||||||||||
| /// pair of theoretical impls: | ||||||||||
| /// | ||||||||||
| /// ```rust,ignore (theoretical code) | ||||||||||
| /// impl Foo for dyn Foo<Item = Ty> | ||||||||||
| /// where | ||||||||||
| /// Self: Baz, | ||||||||||
| /// <Self as Foo>::Bar: Copy, | ||||||||||
| /// { | ||||||||||
| /// type Bar = Ty; | ||||||||||
| /// } | ||||||||||
| /// | ||||||||||
| /// impl Baz for dyn Foo<Item = Ty> {} | ||||||||||
| /// ``` | ||||||||||
| /// | ||||||||||
| /// However, in order to make such impls well-formed, we need to do an | ||||||||||
| /// additional step of eagerly folding the associated types in the where | ||||||||||
| /// clauses of the impl. In this example, that means replacing | ||||||||||
| /// `<Self as Foo>::Bar` with `Ty` in the first impl. | ||||||||||
| pub(crate) fn predicates_for_object_candidate<'tcx>( | ||||||||||
| ecx: &EvalCtxt<'_, 'tcx>, | ||||||||||
| param_env: ty::ParamEnv<'tcx>, | ||||||||||
| trait_ref: ty::TraitRef<'tcx>, | ||||||||||
| object_bound: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>, | ||||||||||
| ) -> Vec<ty::Predicate<'tcx>> { | ||||||||||
| let tcx = ecx.tcx(); | ||||||||||
| let mut requirements = vec![]; | ||||||||||
| requirements.extend( | ||||||||||
| tcx.super_predicates_of(trait_ref.def_id).instantiate(tcx, trait_ref.substs).predicates, | ||||||||||
| ); | ||||||||||
| for item in tcx.associated_items(trait_ref.def_id).in_definition_order() { | ||||||||||
| // FIXME(associated_const_equality): Also add associated consts to | ||||||||||
| // the requirements here. | ||||||||||
| if item.kind == ty::AssocKind::Type { | ||||||||||
|          | ||||||||||
| if item.kind == ty::AssocKind::Type { | |
| // FIXME(associated_const_equality): Also add associated consts to | |
| // the requirements here. | |
| if item.kind == ty::AssocKind::Type { | 
I think that will be necessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can assoc consts have WC?
        
          
              
                  compiler-errors marked this conversation as resolved.
              
              
                Outdated
          
            Show resolved
            Hide resolved
        
      
                  compiler-errors marked this conversation as resolved.
              
              
                Outdated
          
            Show resolved
            Hide resolved
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // compile-flags: -Ztrait-solver=next | ||
| // check-pass | ||
|  | ||
| trait Trait<'a> { | ||
| type Item: for<'b> Trait2<'b>; | ||
| } | ||
|  | ||
| trait Trait2<'a> {} | ||
| impl Trait2<'_> for () {} | ||
|  | ||
| fn needs_trait(_: Box<impl for<'a> Trait<'a> + ?Sized>) {} | ||
|  | ||
| fn foo(x: Box<dyn for<'a> Trait<'a, Item = ()>>) { | ||
| needs_trait(x); | ||
| } | ||
|  | ||
| fn main() {} | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // compile-flags: -Ztrait-solver=next | ||
| // From #80800 | ||
|  | ||
| trait SuperTrait { | ||
| type A; | ||
| type B; | ||
| } | ||
|  | ||
| trait Trait: SuperTrait<A = <Self as SuperTrait>::B> {} | ||
|  | ||
| fn transmute<A, B>(x: A) -> B { | ||
| foo::<A, B, dyn Trait<A = A, B = B>>(x) | ||
| //~^ ERROR type annotations needed: cannot satisfy `dyn Trait<A = A, B = B>: Trait` | ||
| } | ||
|  | ||
| fn foo<A, B, T: ?Sized>(x: T::A) -> B | ||
| where | ||
| T: Trait<B = B>, | ||
| { | ||
| x | ||
| } | ||
|  | ||
| static X: u8 = 0; | ||
| fn main() { | ||
| let x = transmute::<&u8, &[u8; 1_000_000]>(&X); | ||
| println!("{:?}", x[100_000]); | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| error[E0283]: type annotations needed: cannot satisfy `dyn Trait<A = A, B = B>: Trait` | ||
| --> $DIR/more-object-bound.rs:12:5 | ||
| | | ||
| LL | foo::<A, B, dyn Trait<A = A, B = B>>(x) | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: cannot satisfy `dyn Trait<A = A, B = B>: Trait` | ||
| note: required by a bound in `foo` | ||
| --> $DIR/more-object-bound.rs:18:8 | ||
| | | ||
| LL | fn foo<A, B, T: ?Sized>(x: T::A) -> B | ||
| | --- required by a bound in this function | ||
| LL | where | ||
| LL | T: Trait<B = B>, | ||
| | ^^^^^^^^^^^^ required by this bound in `foo` | ||
|  | ||
| error: aborting due to previous error | ||
|  | ||
| For more information about this error, try `rustc --explain E0283`. | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // compile-flags: -Ztrait-solver=next | ||
|  | ||
| trait Setup { | ||
| type From: Copy; | ||
| } | ||
|  | ||
| fn copy<U: Setup + ?Sized>(from: &U::From) -> U::From { | ||
| *from | ||
| } | ||
|  | ||
| pub fn copy_any<T>(t: &T) -> T { | ||
| copy::<dyn Setup<From=T>>(t) | ||
| //~^ ERROR the trait bound `dyn Setup<From = T>: Setup` is not satisfied | ||
| } | ||
|  | ||
| fn main() { | ||
| let x = String::from("Hello, world"); | ||
| let y = copy_any(&x); | ||
| println!("{y}"); | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| error[E0277]: the trait bound `dyn Setup<From = T>: Setup` is not satisfied | ||
| --> $DIR/object-unsafety.rs:12:12 | ||
| | | ||
| LL | copy::<dyn Setup<From=T>>(t) | ||
| | ^^^^^^^^^^^^^^^^^ the trait `Setup` is not implemented for `dyn Setup<From = T>` | ||
| | | ||
| note: required by a bound in `copy` | ||
| --> $DIR/object-unsafety.rs:7:12 | ||
| | | ||
| LL | fn copy<U: Setup + ?Sized>(from: &U::From) -> U::From { | ||
| | ^^^^^ required by this bound in `copy` | ||
| help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement | ||
| | | ||
| LL | pub fn copy_any<T>(t: &T) -> T where dyn Setup<From = T>: Setup { | ||
| | ++++++++++++++++++++++++++++++++ | ||
|  | ||
| error: aborting due to previous error | ||
|  | ||
| For more information about this error, try `rustc --explain E0277`. | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the main concerning step here 🤔 because I don't have a strong understanding of why the impl with unnormalized projections is not well-formed.
It doesn't compile with either the new solver or the old one, but that doesn't feel like a good enough explanation :< can look into this a bit myself (and should have a rustc-dev-guide section about this)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this actually isn't (technically) failing during wfcheck. On both the "classic" solver and "next" solver, this overflows in
normalize_param_env_or_errorin theparam_envquery for the impl. 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lcnr/solver-woes#10 to track this