-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build abstract consts without typeck for
QPath::Resolved
s
- Loading branch information
Showing
9 changed files
with
163 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,49 @@ | ||
use rustc_middle::thir::abstract_const::Node as ACNode; | ||
use rustc_middle::ty::{self, DefIdTree, TyCtxt, TypeFoldable}; | ||
use rustc_span::def_id::LocalDefId; | ||
|
||
/// Builds an abstract const, do not use this directly, but use `AbstractConst::new` instead. | ||
pub(super) fn abstract_const_from_fully_qualif_assoc<'tcx>( | ||
tcx: TyCtxt<'tcx>, | ||
def: ty::WithOptConstParam<LocalDefId>, | ||
) -> Option<Option<&'tcx [ACNode<'tcx>]>> { | ||
let anon_ct_hir_id = tcx.hir().local_def_id_to_hir_id(def.did); | ||
tcx.hir() | ||
.get(anon_ct_hir_id) | ||
.is_anon_const() | ||
.and_then(|ct| tcx.hir().is_fully_qualif_assoc_const_proj(ct.body)) | ||
.map(|(this, path)| { | ||
let trait_did = tcx.parent(path.res.def_id()).unwrap(); | ||
debug!("trait_did: {:?}", trait_did); | ||
let item_ctxt: &dyn crate::astconv::AstConv<'_> = | ||
&crate::collect::ItemCtxt::new(tcx, trait_did); | ||
let self_ty = item_ctxt.ast_ty_to_ty(this); | ||
let trait_ref_substs = <dyn crate::astconv::AstConv<'_>>::ast_path_to_mono_trait_ref( | ||
item_ctxt, | ||
path.span, | ||
trait_did, | ||
self_ty, | ||
&path.segments[0], | ||
) | ||
.substs; | ||
debug!("trait_ref_substs: {:?}", trait_ref_substs); | ||
|
||
// there is no such thing as `feature(generic_associated_consts)` yet so we dont need | ||
// to handle substs for the const on the trait i.e. `N` in `<T as Trait<U>>::ASSOC::<N>` | ||
assert!(path.segments[1].args.is_none()); | ||
|
||
trait_ref_substs.definitely_has_param_types_or_consts(tcx).then(|| { | ||
let ct = tcx.mk_const(ty::Const { | ||
val: ty::ConstKind::Unevaluated(ty::Unevaluated::new( | ||
ty::WithOptConstParam { | ||
did: path.res.def_id(), | ||
const_param_did: def.const_param_did, | ||
}, | ||
trait_ref_substs, | ||
)), | ||
ty: tcx.type_of(path.res.def_id()), | ||
}); | ||
&*tcx.arena.alloc_from_iter([ACNode::Leaf(ct)]) | ||
}) | ||
}) | ||
} |
This file contains 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 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 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
41 changes: 41 additions & 0 deletions
41
src/test/ui/const-generics/generic_const_exprs/assoc_const_no_typeck.rs
This file contains 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,41 @@ | ||
// check-pass | ||
#![feature(generic_const_exprs)] | ||
#![allow(incomplete_features)] | ||
|
||
trait Trait<const N: usize> { | ||
const ASSOC: usize; | ||
type Foo; | ||
} | ||
|
||
fn no_cycle<const N: usize>() | ||
where | ||
u8: Trait<N>, | ||
(): Trait<{ <u8 as Trait<N>>::ASSOC }>, | ||
[(); <() as Trait<{ <u8 as Trait<N>>::ASSOC }>>::ASSOC]: , | ||
{ | ||
} | ||
|
||
fn foo<const N: usize>(_: [(); <<() as Trait<N>>::Foo as Trait<N>>::ASSOC]) | ||
where | ||
(): Trait<N>, | ||
<() as Trait<N>>::Foo: Trait<N>, | ||
{ | ||
} | ||
|
||
trait Trait2<T> { | ||
type Foo; | ||
} | ||
|
||
struct Inherent; | ||
impl Inherent { | ||
const ASSOC: usize = 10; | ||
} | ||
|
||
fn bar<T>() | ||
where | ||
(): Trait2<T, Foo = Inherent>, | ||
[(); <() as Trait2<T>>::Foo::ASSOC]: , | ||
{ | ||
} | ||
|
||
fn main() {} |