|
1 | 1 | // ignore-tidy-filelength
|
2 |
| - |
3 | 2 | pub use self::fold::{TypeFoldable, TypeVisitor};
|
4 | 3 | pub use self::AssocItemContainer::*;
|
5 | 4 | pub use self::BorrowKind::*;
|
@@ -1571,11 +1570,45 @@ pub type PlaceholderType = Placeholder<BoundVar>;
|
1571 | 1570 |
|
1572 | 1571 | pub type PlaceholderConst = Placeholder<BoundVar>;
|
1573 | 1572 |
|
| 1573 | +/// A `DefId` which is potentially bundled with its corresponding generic parameter |
| 1574 | +/// in case `did` is a const argument. |
| 1575 | +/// |
| 1576 | +/// This is used to prevent cycle errors during typeck |
| 1577 | +/// as `type_of(const_arg)` depends on `typeck_tables_of(owning_body)` |
| 1578 | +/// which once again requires the type of its generic arguments. |
| 1579 | +/// |
| 1580 | +/// Luckily we only need to deal with const arguments once we |
| 1581 | +/// know their corresponding parameters. We (ab)use this by |
| 1582 | +/// calling `type_of(param_did)` for these arguments. |
| 1583 | +/// |
| 1584 | +/// ```rust |
| 1585 | +/// #![feature(const_generics)] |
| 1586 | +/// |
| 1587 | +/// struct A; |
| 1588 | +/// impl A { |
| 1589 | +/// fn foo<const N: usize>(&self) -> usize { N } |
| 1590 | +/// } |
| 1591 | +/// struct B; |
| 1592 | +/// impl B { |
| 1593 | +/// fn foo<const N: u8>(&self) -> usize { 42 } |
| 1594 | +/// } |
| 1595 | +/// |
| 1596 | +/// fn main() { |
| 1597 | +/// let a = A; |
| 1598 | +/// a.foo::<7>(); |
| 1599 | +/// } |
| 1600 | +/// ``` |
1574 | 1601 | #[derive(Copy, Clone, Debug, TypeFoldable, Lift, RustcEncodable, RustcDecodable)]
|
1575 | 1602 | #[derive(PartialEq, Eq, PartialOrd, Ord)]
|
1576 | 1603 | #[derive(Hash, HashStable)]
|
1577 | 1604 | pub struct WithOptParam<T> {
|
1578 | 1605 | pub did: T,
|
| 1606 | + /// The `DefId` of the corresponding generic paramter in case `did` is |
| 1607 | + /// a const argument. |
| 1608 | + /// |
| 1609 | + /// Note that even if `did` is a const argument, this may still be `None`. |
| 1610 | + /// All queries taking `WithOptParam` start by calling `tcx.opt_const_param_of(def.did)` |
| 1611 | + /// to potentially update `param_did` in case it `None`. |
1579 | 1612 | pub param_did: Option<DefId>,
|
1580 | 1613 | }
|
1581 | 1614 |
|
@@ -2896,7 +2929,6 @@ impl<'tcx> TyCtxt<'tcx> {
|
2896 | 2929 | match instance {
|
2897 | 2930 | ty::InstanceDef::Item(def) => {
|
2898 | 2931 | if let Some((did, param_did)) = def.as_const_arg() {
|
2899 |
| - // The `param_did` is only `Some` for local `DefId`s. |
2900 | 2932 | self.optimized_mir_of_const_arg((did, param_did))
|
2901 | 2933 | } else {
|
2902 | 2934 | self.optimized_mir(def.did)
|
|
0 commit comments