Skip to content

Commit 6136a86

Browse files
committed
Auto merge of #32817 - jseyfried:warn_impl_param_defaults, r=nikomatsakis
Warn for type parameter defaults on impl blocks Warn for type parameter defaults on impl blocks (fixes #31543). r? @nikomatsakis
2 parents 7d4d3cb + 7c73b9e commit 6136a86

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

src/librustc_typeck/collect.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ fn convert_item(ccx: &CrateCtxt, it: &hir::Item) {
715715
// Create generics from the generics specified in the impl head.
716716
debug!("convert: ast_generics={:?}", generics);
717717
let def_id = ccx.tcx.map.local_def_id(it.id);
718-
let ty_generics = ty_generics_for_type_or_impl(ccx, generics);
718+
let ty_generics = ty_generics_for_impl(ccx, generics);
719719
let mut ty_predicates = ty_generic_predicates_for_type_or_impl(ccx, generics);
720720

721721
debug!("convert: impl_bounds={:?}", ty_predicates);
@@ -1455,19 +1455,19 @@ fn compute_type_scheme_of_item<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
14551455
ty::TypeScheme { ty: ty, generics: ty_generics }
14561456
}
14571457
hir::ItemTy(ref t, ref generics) => {
1458-
let ty_generics = ty_generics_for_type_or_impl(ccx, generics);
1458+
let ty_generics = ty_generics_for_type(ccx, generics);
14591459
let ty = ccx.icx(generics).to_ty(&ExplicitRscope, &t);
14601460
ty::TypeScheme { ty: ty, generics: ty_generics }
14611461
}
14621462
hir::ItemEnum(ref ei, ref generics) => {
1463-
let ty_generics = ty_generics_for_type_or_impl(ccx, generics);
1463+
let ty_generics = ty_generics_for_type(ccx, generics);
14641464
let substs = mk_item_substs(ccx, &ty_generics);
14651465
let def = convert_enum_def(tcx, it, ei);
14661466
let t = tcx.mk_enum(def, tcx.mk_substs(substs));
14671467
ty::TypeScheme { ty: t, generics: ty_generics }
14681468
}
14691469
hir::ItemStruct(ref si, ref generics) => {
1470-
let ty_generics = ty_generics_for_type_or_impl(ccx, generics);
1470+
let ty_generics = ty_generics_for_type(ccx, generics);
14711471
let substs = mk_item_substs(ccx, &ty_generics);
14721472
let def = convert_struct_def(tcx, it, si);
14731473
let t = tcx.mk_struct(def, tcx.mk_substs(substs));
@@ -1611,10 +1611,14 @@ fn convert_foreign_item<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
16111611
assert!(prev_predicates.is_none());
16121612
}
16131613

1614-
fn ty_generics_for_type_or_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
1615-
generics: &hir::Generics)
1616-
-> ty::Generics<'tcx> {
1617-
ty_generics(ccx, TypeSpace, generics, &ty::Generics::empty())
1614+
fn ty_generics_for_type<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, generics: &hir::Generics)
1615+
-> ty::Generics<'tcx> {
1616+
ty_generics(ccx, TypeSpace, generics, &ty::Generics::empty(), true)
1617+
}
1618+
1619+
fn ty_generics_for_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, generics: &hir::Generics)
1620+
-> ty::Generics<'tcx> {
1621+
ty_generics(ccx, TypeSpace, generics, &ty::Generics::empty(), false)
16181622
}
16191623

16201624
fn ty_generic_predicates_for_type_or_impl<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
@@ -1633,7 +1637,7 @@ fn ty_generics_for_trait<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
16331637
debug!("ty_generics_for_trait(trait_id={:?}, substs={:?})",
16341638
ccx.tcx.map.local_def_id(trait_id), substs);
16351639

1636-
let mut generics = ty_generics_for_type_or_impl(ccx, ast_generics);
1640+
let mut generics = ty_generics_for_type(ccx, ast_generics);
16371641

16381642
// Add in the self type parameter.
16391643
//
@@ -1665,7 +1669,7 @@ fn ty_generics_for_fn<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
16651669
base_generics: &ty::Generics<'tcx>)
16661670
-> ty::Generics<'tcx>
16671671
{
1668-
ty_generics(ccx, FnSpace, generics, base_generics)
1672+
ty_generics(ccx, FnSpace, generics, base_generics, false)
16691673
}
16701674

16711675
fn ty_generic_predicates_for_fn<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
@@ -1840,7 +1844,8 @@ fn ty_generic_predicates<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
18401844
fn ty_generics<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
18411845
space: ParamSpace,
18421846
ast_generics: &hir::Generics,
1843-
base_generics: &ty::Generics<'tcx>)
1847+
base_generics: &ty::Generics<'tcx>,
1848+
allow_defaults: bool)
18441849
-> ty::Generics<'tcx>
18451850
{
18461851
let tcx = ccx.tcx;
@@ -1863,7 +1868,8 @@ fn ty_generics<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
18631868

18641869
// Now create the real type parameters.
18651870
for i in 0..ast_generics.ty_params.len() {
1866-
let def = get_or_create_type_parameter_def(ccx, ast_generics, space, i as u32);
1871+
let def =
1872+
get_or_create_type_parameter_def(ccx, ast_generics, space, i as u32, allow_defaults);
18671873
debug!("ty_generics: def for type param: {:?}, {:?}", def, space);
18681874
result.types.push(space, def);
18691875
}
@@ -1897,7 +1903,8 @@ fn convert_default_type_parameter<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
18971903
fn get_or_create_type_parameter_def<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
18981904
ast_generics: &hir::Generics,
18991905
space: ParamSpace,
1900-
index: u32)
1906+
index: u32,
1907+
allow_defaults: bool)
19011908
-> ty::TypeParameterDef<'tcx>
19021909
{
19031910
let param = &ast_generics.ty_params[index as usize];
@@ -1918,7 +1925,7 @@ fn get_or_create_type_parameter_def<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
19181925

19191926
let parent = tcx.map.get_parent(param.id);
19201927

1921-
if space != TypeSpace && default.is_some() {
1928+
if !allow_defaults && default.is_some() {
19221929
if !tcx.sess.features.borrow().default_type_parameter_fallback {
19231930
tcx.sess.add_lint(
19241931
lint::builtin::INVALID_TYPE_PARAM_DEFAULT,

src/test/compile-fail/type-parameter-invalid-lint.rs

+6
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@
1414
fn avg<T=i32>(_: T) {}
1515
//~^ ERROR defaults for type parameters are only allowed
1616
//~| WARNING hard error
17+
18+
struct S<T>(T);
19+
impl<T=i32> S<T> {}
20+
//~^ ERROR defaults for type parameters are only allowed
21+
//~| WARNING hard error
22+
1723
fn main() {}

0 commit comments

Comments
 (0)