Skip to content

Commit f8de2f5

Browse files
authored
Rollup merge of #91847 - BoxyUwU:generic_arg_infer_fixme, r=lcnr
Fix FIXME for `generic_arg_infer` in `create_substs_for_ast_path` Fixes a FIXME, does some general refactoring of this fn, and also fixes a bug where we would use a const params defaults instead of an inference var ([playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=19456f65ea5dc3fcaa9b696f842ab380)) (lot of stuff in one PR but it was all so close together...) r? `@lcnr` Fixes #91614
2 parents dca8dde + 6c79595 commit f8de2f5

File tree

4 files changed

+74
-55
lines changed

4 files changed

+74
-55
lines changed

Diff for: compiler/rustc_typeck/src/astconv/mod.rs

+33-55
Original file line numberDiff line numberDiff line change
@@ -414,34 +414,40 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
414414
arg: &GenericArg<'_>,
415415
) -> subst::GenericArg<'tcx> {
416416
let tcx = self.astconv.tcx();
417+
418+
let mut handle_ty_args = |has_default, ty: &hir::Ty<'_>| {
419+
if has_default {
420+
tcx.check_optional_stability(
421+
param.def_id,
422+
Some(arg.id()),
423+
arg.span(),
424+
None,
425+
|_, _| {
426+
// Default generic parameters may not be marked
427+
// with stability attributes, i.e. when the
428+
// default parameter was defined at the same time
429+
// as the rest of the type. As such, we ignore missing
430+
// stability attributes.
431+
},
432+
)
433+
}
434+
if let (hir::TyKind::Infer, false) = (&ty.kind, self.astconv.allow_ty_infer()) {
435+
self.inferred_params.push(ty.span);
436+
tcx.ty_error().into()
437+
} else {
438+
self.astconv.ast_ty_to_ty(ty).into()
439+
}
440+
};
441+
417442
match (&param.kind, arg) {
418443
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
419444
self.astconv.ast_region_to_region(lt, Some(param)).into()
420445
}
421446
(&GenericParamDefKind::Type { has_default, .. }, GenericArg::Type(ty)) => {
422-
if has_default {
423-
tcx.check_optional_stability(
424-
param.def_id,
425-
Some(arg.id()),
426-
arg.span(),
427-
None,
428-
|_, _| {
429-
// Default generic parameters may not be marked
430-
// with stability attributes, i.e. when the
431-
// default parameter was defined at the same time
432-
// as the rest of the type. As such, we ignore missing
433-
// stability attributes.
434-
},
435-
)
436-
}
437-
if let (hir::TyKind::Infer, false) =
438-
(&ty.kind, self.astconv.allow_ty_infer())
439-
{
440-
self.inferred_params.push(ty.span);
441-
tcx.ty_error().into()
442-
} else {
443-
self.astconv.ast_ty_to_ty(ty).into()
444-
}
447+
handle_ty_args(has_default, ty)
448+
}
449+
(&GenericParamDefKind::Type { has_default, .. }, GenericArg::Infer(inf)) => {
450+
handle_ty_args(has_default, &inf.to_ty())
445451
}
446452
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
447453
ty::Const::from_opt_const_arg_anon_const(
@@ -453,41 +459,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
453459
)
454460
.into()
455461
}
456-
(&GenericParamDefKind::Const { has_default }, hir::GenericArg::Infer(inf)) => {
457-
if has_default {
458-
tcx.const_param_default(param.def_id).into()
459-
} else if self.astconv.allow_ty_infer() {
460-
// FIXME(const_generics): Actually infer parameter here?
461-
todo!()
462-
} else {
463-
self.inferred_params.push(inf.span);
464-
tcx.ty_error().into()
465-
}
466-
}
467-
(
468-
&GenericParamDefKind::Type { has_default, .. },
469-
hir::GenericArg::Infer(inf),
470-
) => {
471-
if has_default {
472-
tcx.check_optional_stability(
473-
param.def_id,
474-
Some(arg.id()),
475-
arg.span(),
476-
None,
477-
|_, _| {
478-
// Default generic parameters may not be marked
479-
// with stability attributes, i.e. when the
480-
// default parameter was defined at the same time
481-
// as the rest of the type. As such, we ignore missing
482-
// stability attributes.
483-
},
484-
);
485-
}
462+
(&GenericParamDefKind::Const { .. }, hir::GenericArg::Infer(inf)) => {
463+
let ty = tcx.at(self.span).type_of(param.def_id);
486464
if self.astconv.allow_ty_infer() {
487-
self.astconv.ast_ty_to_ty(&inf.to_ty()).into()
465+
self.astconv.ct_infer(ty, Some(param), inf.span).into()
488466
} else {
489467
self.inferred_params.push(inf.span);
490-
tcx.ty_error().into()
468+
tcx.const_error(ty).into()
491469
}
492470
}
493471
_ => unreachable!(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// run-pass
2+
#![feature(generic_arg_infer)]
3+
4+
// test that we dont use defaults to aide in type inference
5+
6+
struct Foo<const N: usize = 2>;
7+
impl<const N: usize> Foo<N> {
8+
fn make_arr() -> [(); N] {
9+
[(); N]
10+
}
11+
}
12+
13+
fn main() {
14+
let [(), (), ()] = Foo::<_>::make_arr();
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![feature(portable_simd)]
2+
#![feature(generic_arg_infer)]
3+
use std::simd::Mask;
4+
5+
fn main() {
6+
let y = Mask::<_, _>::splat(false);
7+
//~^ error: type annotations needed for `Mask<_, {_: usize}>`
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0283]: type annotations needed for `Mask<_, {_: usize}>`
2+
--> $DIR/issue-91614.rs:6:13
3+
|
4+
LL | let y = Mask::<_, _>::splat(false);
5+
| - ^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
6+
| |
7+
| consider giving `y` the explicit type `Mask<_, LANES>`, where the type parameter `T` is specified
8+
|
9+
= note: cannot satisfy `_: MaskElement`
10+
note: required by a bound in `Mask::<T, LANES>::splat`
11+
--> $SRC_DIR/core/src/../../portable-simd/crates/core_simd/src/masks.rs:LL:COL
12+
|
13+
LL | T: MaskElement,
14+
| ^^^^^^^^^^^ required by this bound in `Mask::<T, LANES>::splat`
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0283`.

0 commit comments

Comments
 (0)