Rollup merge of #92875 - BoxyUwU:infer_arg_opt_const_param_of, r=lcnr
Make `opt_const_param_of` work in the presence of `GenericArg::Infer` highly recommend viewing the first and second commits on their own rather than looking at file changes 🤣 Because we filtered args down to just const args we would ignore `GenericArg::Infer` which made us get a `arg_index` which was wrong by however many const `GenericArg::Infer` came previously [example](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=46dba6a53aca6333028a10908ef16e0b) of the [bugs](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=a8eebced26eefa4119fc2e7ae0c76de6) fixed. r? ```@lcnr```
This commit is contained in:
commit
6c94f99d83
@ -293,10 +293,6 @@ impl GenericArg<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_const(&self) -> bool {
|
||||
matches!(self, GenericArg::Const(_))
|
||||
}
|
||||
|
||||
pub fn is_synthetic(&self) -> bool {
|
||||
matches!(self, GenericArg::Lifetime(lifetime) if lifetime.name.ident() == Ident::empty())
|
||||
}
|
||||
@ -318,6 +314,13 @@ impl GenericArg<'_> {
|
||||
GenericArg::Infer(_) => ast::ParamKindOrd::Infer,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_ty_or_const(&self) -> bool {
|
||||
match self {
|
||||
GenericArg::Lifetime(_) => false,
|
||||
GenericArg::Type(_) | GenericArg::Const(_) | GenericArg::Infer(_) => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, HashStable_Generic)]
|
||||
|
@ -31,6 +31,13 @@ impl GenericParamDefKind {
|
||||
GenericParamDefKind::Const { .. } => ast::ParamKindOrd::Const,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_ty_or_const(&self) -> bool {
|
||||
match self {
|
||||
GenericParamDefKind::Lifetime => false,
|
||||
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
|
||||
|
@ -18,6 +18,7 @@ use super::{bad_placeholder, is_suggestable_infer_ty};
|
||||
/// Computes the relevant generic parameter for a potential generic const argument.
|
||||
///
|
||||
/// This should be called using the query `tcx.opt_const_param_of`.
|
||||
#[instrument(level = "debug", skip(tcx))]
|
||||
pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<DefId> {
|
||||
// FIXME(generic_arg_infer): allow for returning DefIds of inference of
|
||||
// GenericArg::Infer below. This may require a change where GenericArg::Infer has some flag
|
||||
@ -25,11 +26,15 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
|
||||
use hir::*;
|
||||
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
|
||||
|
||||
if let Node::AnonConst(_) = tcx.hir().get(hir_id) {
|
||||
match tcx.hir().get(hir_id) {
|
||||
Node::AnonConst(_) => (),
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
let parent_node_id = tcx.hir().get_parent_node(hir_id);
|
||||
let parent_node = tcx.hir().get(parent_node_id);
|
||||
|
||||
match parent_node {
|
||||
let (generics, arg_idx) = match parent_node {
|
||||
// This match arm is for when the def_id appears in a GAT whose
|
||||
// path can't be resolved without typechecking e.g.
|
||||
//
|
||||
@ -75,21 +80,15 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
|
||||
.and_then(|args| {
|
||||
args.args
|
||||
.iter()
|
||||
.filter(|arg| arg.is_const())
|
||||
.filter(|arg| arg.is_ty_or_const())
|
||||
.position(|arg| arg.id() == hir_id)
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
bug!("no arg matching AnonConst in segment");
|
||||
});
|
||||
|
||||
return generics
|
||||
.params
|
||||
.iter()
|
||||
.filter(|param| matches!(param.kind, ty::GenericParamDefKind::Const { .. }))
|
||||
.nth(arg_index)
|
||||
.map(|param| param.def_id);
|
||||
}
|
||||
|
||||
(generics, arg_index)
|
||||
} else {
|
||||
// I dont think it's possible to reach this but I'm not 100% sure - BoxyUwU
|
||||
tcx.sess.delay_span_bug(
|
||||
tcx.def_span(def_id),
|
||||
@ -97,6 +96,7 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
|
||||
);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Node::Expr(&Expr {
|
||||
kind:
|
||||
ExprKind::MethodCall(segment, ..) | ExprKind::Path(QPath::TypeRelative(_, segment)),
|
||||
@ -113,19 +113,14 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
|
||||
.and_then(|args| {
|
||||
args.args
|
||||
.iter()
|
||||
.filter(|arg| arg.is_const())
|
||||
.filter(|arg| arg.is_ty_or_const())
|
||||
.position(|arg| arg.id() == hir_id)
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
bug!("no arg matching AnonConst in segment");
|
||||
});
|
||||
|
||||
tcx.generics_of(type_dependent_def)
|
||||
.params
|
||||
.iter()
|
||||
.filter(|param| matches!(param.kind, ty::GenericParamDefKind::Const { .. }))
|
||||
.nth(idx)
|
||||
.map(|param| param.def_id)
|
||||
(tcx.generics_of(type_dependent_def), idx)
|
||||
}
|
||||
|
||||
Node::Ty(&Ty { kind: TyKind::Path(_), .. })
|
||||
@ -141,8 +136,7 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
|
||||
| ExprKind::Struct(&QPath::Resolved(_, path), ..),
|
||||
..
|
||||
}) => {
|
||||
let body_owner =
|
||||
tcx.hir().local_def_id(tcx.hir().enclosing_body_owner(hir_id));
|
||||
let body_owner = tcx.hir().local_def_id(tcx.hir().enclosing_body_owner(hir_id));
|
||||
let _tables = tcx.typeck(body_owner);
|
||||
&*path
|
||||
}
|
||||
@ -152,10 +146,7 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
|
||||
} else {
|
||||
tcx.sess.delay_span_bug(
|
||||
tcx.def_span(def_id),
|
||||
&format!(
|
||||
"unable to find const parent for {} in pat {:?}",
|
||||
hir_id, pat
|
||||
),
|
||||
&format!("unable to find const parent for {} in pat {:?}", hir_id, pat),
|
||||
);
|
||||
return None;
|
||||
}
|
||||
@ -178,16 +169,14 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
|
||||
.filter_map(|seg| seg.args.map(|args| (args.args, seg)))
|
||||
.find_map(|(args, seg)| {
|
||||
args.iter()
|
||||
.filter(|arg| arg.is_const())
|
||||
.filter(|arg| arg.is_ty_or_const())
|
||||
.position(|arg| arg.id() == hir_id)
|
||||
.map(|index| (index, seg))
|
||||
});
|
||||
let (arg_index, segment) = match filtered {
|
||||
None => {
|
||||
tcx.sess.delay_span_bug(
|
||||
tcx.def_span(def_id),
|
||||
"no arg matching AnonConst in path",
|
||||
);
|
||||
tcx.sess
|
||||
.delay_span_bug(tcx.def_span(def_id), "no arg matching AnonConst in path");
|
||||
return None;
|
||||
}
|
||||
Some(inner) => inner,
|
||||
@ -198,9 +187,8 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
|
||||
let res = segment.res.filter(|&r| r != Res::Err).unwrap_or(path.res);
|
||||
use def::CtorOf;
|
||||
let generics = match res {
|
||||
Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => tcx.generics_of(
|
||||
tcx.parent(def_id).and_then(|def_id| tcx.parent(def_id)).unwrap(),
|
||||
),
|
||||
Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => tcx
|
||||
.generics_of(tcx.parent(def_id).and_then(|def_id| tcx.parent(def_id)).unwrap()),
|
||||
Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Struct, _), def_id) => {
|
||||
tcx.generics_of(tcx.parent(def_id).unwrap())
|
||||
}
|
||||
@ -238,18 +226,28 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
|
||||
}
|
||||
};
|
||||
|
||||
(generics, arg_index)
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
debug!(?parent_node);
|
||||
debug!(?generics, ?arg_idx);
|
||||
generics
|
||||
.params
|
||||
.iter()
|
||||
.filter(|param| matches!(param.kind, ty::GenericParamDefKind::Const { .. }))
|
||||
.nth(arg_index)
|
||||
.map(|param| param.def_id)
|
||||
.filter(|param| param.kind.is_ty_or_const())
|
||||
.nth(match generics.has_self && generics.parent.is_none() {
|
||||
true => arg_idx + 1,
|
||||
false => arg_idx,
|
||||
})
|
||||
.and_then(|param| match param.kind {
|
||||
ty::GenericParamDefKind::Const { .. } => {
|
||||
debug!(?param);
|
||||
Some(param.def_id)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn get_path_containing_arg_in_pat<'hir>(
|
||||
|
@ -0,0 +1,12 @@
|
||||
// check-pass
|
||||
#![feature(generic_arg_infer)]
|
||||
|
||||
struct Foo<const N: bool, const M: u8>;
|
||||
struct Bar<const N: u8, const M: u32>;
|
||||
|
||||
fn main() {
|
||||
let _: Foo<true, _> = Foo::<_, 1>;
|
||||
let _: Foo<_, 1> = Foo::<true, _>;
|
||||
let _: Bar<1, _> = Bar::<_, 300>;
|
||||
let _: Bar<_, 300> = Bar::<1, _>;
|
||||
}
|
@ -4,13 +4,6 @@ error[E0770]: the type of const parameters must not depend on other generic para
|
||||
LL | fn foo<const N: usize, const A: [u8; N]>() {}
|
||||
| ^ the type must not depend on the parameter `N`
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/issue-62878.rs:10:15
|
||||
|
|
||||
LL | foo::<_, {[1]}>();
|
||||
| ^^^ expected `usize`, found array `[{integer}; 1]`
|
||||
error: aborting due to previous error
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
Some errors have detailed explanations: E0308, E0770.
|
||||
For more information about an error, try `rustc --explain E0308`.
|
||||
For more information about this error, try `rustc --explain E0770`.
|
||||
|
@ -8,5 +8,4 @@ fn foo<const N: usize, const A: [u8; N]>() {}
|
||||
|
||||
fn main() {
|
||||
foo::<_, { [1] }>();
|
||||
//[full]~^ ERROR mismatched types
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user