- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
          Uplift Canonical to rustc_type_ir
          #117008
        
          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
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 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
    
  
  
    
              
  
    
      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 | 
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| use std::fmt; | ||
| use std::hash; | ||
| use std::ops::ControlFlow; | ||
|  | ||
| use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; | ||
| use rustc_serialize::{Decodable, Encodable}; | ||
|  | ||
| use crate::fold::{FallibleTypeFolder, TypeFoldable}; | ||
| use crate::visit::{TypeVisitable, TypeVisitor}; | ||
| use crate::TyDecoder; | ||
| use crate::{HashStableContext, Interner, TyEncoder, UniverseIndex}; | ||
|  | ||
| /// A "canonicalized" type `V` is one where all free inference | ||
| /// variables have been rewritten to "canonical vars". These are | ||
| /// numbered starting from 0 in order of first appearance. | ||
| pub struct Canonical<I: Interner, V> { | ||
| pub value: V, | ||
| pub max_universe: UniverseIndex, | ||
| pub variables: I::CanonicalVars, | ||
| } | ||
|  | ||
| impl<I: Interner, V> Canonical<I, V> { | ||
| /// Allows you to map the `value` of a canonical while keeping the | ||
| /// same set of bound variables. | ||
| /// | ||
| /// **WARNING:** This function is very easy to mis-use, hence the | ||
| /// name! In particular, the new value `W` must use all **the | ||
| /// same type/region variables** in **precisely the same order** | ||
| /// as the original! (The ordering is defined by the | ||
| /// `TypeFoldable` implementation of the type in question.) | ||
| /// | ||
| /// An example of a **correct** use of this: | ||
| /// | ||
| /// ```rust,ignore (not real code) | ||
| /// let a: Canonical<I, T> = ...; | ||
| /// let b: Canonical<I, (T,)> = a.unchecked_map(|v| (v, )); | ||
| /// ``` | ||
| /// | ||
| /// An example of an **incorrect** use of this: | ||
| /// | ||
| /// ```rust,ignore (not real code) | ||
| /// let a: Canonical<I, T> = ...; | ||
| /// let ty: Ty<I> = ...; | ||
| /// let b: Canonical<I, (T, Ty<I>)> = a.unchecked_map(|v| (v, ty)); | ||
| /// ``` | ||
| pub fn unchecked_map<W>(self, map_op: impl FnOnce(V) -> W) -> Canonical<I, W> { | ||
| let Canonical { max_universe, variables, value } = self; | ||
| Canonical { max_universe, variables, value: map_op(value) } | ||
| } | ||
|  | ||
| /// Allows you to map the `value` of a canonical while keeping the same set of | ||
| /// bound variables. | ||
| /// | ||
| /// **WARNING:** This function is very easy to mis-use, hence the name! See | ||
| /// the comment of [Canonical::unchecked_map] for more details. | ||
| pub fn unchecked_rebind<W>(self, value: W) -> Canonical<I, W> { | ||
| let Canonical { max_universe, variables, value: _ } = self; | ||
| Canonical { max_universe, variables, value } | ||
| } | ||
| } | ||
|  | ||
| impl<I: Interner, V: hash::Hash> hash::Hash for Canonical<I, V> { | ||
| fn hash<H: hash::Hasher>(&self, state: &mut H) { | ||
| self.value.hash(state); | ||
| self.max_universe.hash(state); | ||
| self.variables.hash(state); | ||
| } | ||
| } | ||
|  | ||
| impl<CTX: HashStableContext, I: Interner, V: HashStable<CTX>> HashStable<CTX> for Canonical<I, V> | ||
| where | ||
| I::CanonicalVars: HashStable<CTX>, | ||
| { | ||
| fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { | ||
| self.value.hash_stable(hcx, hasher); | ||
| self.max_universe.hash_stable(hcx, hasher); | ||
| self.variables.hash_stable(hcx, hasher); | ||
| } | ||
| } | ||
|  | ||
| impl<I: Interner, V: Eq> Eq for Canonical<I, V> {} | ||
|  | ||
| impl<I: Interner, V: PartialEq> PartialEq for Canonical<I, V> { | ||
| fn eq(&self, other: &Self) -> bool { | ||
| self.value == other.value | ||
| && self.max_universe == other.max_universe | ||
| && self.variables == other.variables | ||
| } | ||
| } | ||
|  | ||
| impl<I: Interner, V: fmt::Display> fmt::Display for Canonical<I, V> { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| write!( | ||
| f, | ||
| "Canonical {{ value: {}, max_universe: {:?}, variables: {:?} }}", | ||
| self.value, self.max_universe, self.variables | ||
| ) | ||
| } | ||
| } | ||
|  | ||
| impl<I: Interner, V: fmt::Debug> fmt::Debug for Canonical<I, V> { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| f.debug_struct("Canonical") | ||
| .field("value", &self.value) | ||
| .field("max_universe", &self.max_universe) | ||
| .field("variables", &self.variables) | ||
| .finish() | ||
| } | ||
| } | ||
|  | ||
| impl<I: Interner, V: Clone> Clone for Canonical<I, V> { | ||
| fn clone(&self) -> Self { | ||
| Canonical { | ||
| value: self.value.clone(), | ||
| max_universe: self.max_universe.clone(), | ||
| variables: self.variables.clone(), | ||
| } | ||
| } | ||
| } | ||
|  | ||
| impl<I: Interner, V: Copy> Copy for Canonical<I, V> where I::CanonicalVars: Copy {} | ||
|  | ||
| impl<I: Interner, V: TypeFoldable<I>> TypeFoldable<I> for Canonical<I, V> | ||
| where | ||
| I::CanonicalVars: TypeFoldable<I>, | ||
| { | ||
| fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> { | ||
| Ok(Canonical { | ||
| value: self.value.try_fold_with(folder)?, | ||
| max_universe: self.max_universe.try_fold_with(folder)?, | ||
| variables: self.variables.try_fold_with(folder)?, | ||
| }) | ||
| } | ||
| } | ||
|  | ||
| impl<I: Interner, V: TypeVisitable<I>> TypeVisitable<I> for Canonical<I, V> | ||
| where | ||
| I::CanonicalVars: TypeVisitable<I>, | ||
| { | ||
| fn visit_with<F: TypeVisitor<I>>(&self, folder: &mut F) -> ControlFlow<F::BreakTy> { | ||
| self.value.visit_with(folder)?; | ||
| self.max_universe.visit_with(folder)?; | ||
| self.variables.visit_with(folder) | ||
| } | ||
| } | ||
|  | ||
| impl<I: Interner, E: TyEncoder<I = I>, V: Encodable<E>> Encodable<E> for Canonical<I, V> | ||
| where | ||
| I::CanonicalVars: Encodable<E>, | ||
| { | ||
| fn encode(&self, s: &mut E) { | ||
| self.value.encode(s); | ||
| self.max_universe.encode(s); | ||
| self.variables.encode(s); | ||
| } | ||
| } | ||
|  | ||
| impl<I: Interner, D: TyDecoder<I = I>, V: Decodable<D>> Decodable<D> for Canonical<I, V> | ||
| where | ||
| I::CanonicalVars: Decodable<D>, | ||
| { | ||
| fn decode(d: &mut D) -> Self { | ||
| Canonical { | ||
| value: Decodable::decode(d), | ||
| max_universe: Decodable::decode(d), | ||
| variables: Decodable::decode(d), | ||
| } | ||
| } | ||
| } | 
  
    
      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
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  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.
I would prefer using an extension trait I think,
UserType::is_identityseems wrong and easy to misuseThere 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.
okie