Skip to content

Commit 0dccb7c

Browse files
committed
Fix assorted issues
1 parent 0fd7221 commit 0dccb7c

File tree

17 files changed

+100
-89
lines changed

17 files changed

+100
-89
lines changed

src/librustc/hir/lowering.rs

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,7 +2267,8 @@ impl<'a> LoweringContext<'a> {
22672267
-> hir::GenericParam {
22682268
let mut bounds = self.lower_param_bounds(&param.bounds, itctx.reborrow());
22692269
// TODO(const_generics): only create `hir::GenericParam` once.
2270-
match param.kind {
2270+
2271+
let (name, kind) = match param.kind {
22712272
GenericParamKind::Lifetime => {
22722273
let was_collecting_in_band = self.is_collecting_in_band_lifetimes;
22732274
self.is_collecting_in_band_lifetimes = false;
@@ -2277,19 +2278,12 @@ impl<'a> LoweringContext<'a> {
22772278
hir::LifetimeName::Param(param_name) => param_name,
22782279
_ => hir::ParamName::Plain(lt.name.ident()),
22792280
};
2280-
let param = hir::GenericParam {
2281-
id: lt.id,
2282-
name: param_name,
2283-
span: lt.span,
2284-
pure_wrt_drop: attr::contains_name(&param.attrs, "may_dangle"),
2285-
attrs: self.lower_attrs(&param.attrs),
2286-
bounds,
2287-
kind: hir::GenericParamKind::Lifetime { in_band: false }
2288-
};
2281+
2282+
let kind = hir::GenericParamKind::Lifetime { in_band: false };
22892283

22902284
self.is_collecting_in_band_lifetimes = was_collecting_in_band;
22912285

2292-
param
2286+
(param_name, kind)
22932287
}
22942288
GenericParamKind::Type { ref default, .. } => {
22952289
// Don't expose `Self` (recovered "keyword used as ident" parse error).
@@ -2309,37 +2303,33 @@ impl<'a> LoweringContext<'a> {
23092303
.collect();
23102304
}
23112305

2312-
hir::GenericParam {
2313-
id: self.lower_node_id(param.id).node_id,
2314-
name: hir::ParamName::Plain(ident),
2315-
pure_wrt_drop: attr::contains_name(&param.attrs, "may_dangle"),
2316-
attrs: self.lower_attrs(&param.attrs),
2317-
bounds,
2318-
span: ident.span,
2319-
kind: hir::GenericParamKind::Type {
2320-
default: default.as_ref().map(|x| {
2321-
self.lower_ty(x, ImplTraitContext::Disallowed)
2322-
}),
2323-
synthetic: param.attrs.iter()
2324-
.filter(|attr| attr.check_name("rustc_synthetic"))
2325-
.map(|_| hir::SyntheticTyParamKind::ImplTrait)
2326-
.next(),
2327-
}
2328-
}
2306+
let kind = hir::GenericParamKind::Type {
2307+
default: default.as_ref().map(|x| {
2308+
self.lower_ty(x, ImplTraitContext::Disallowed)
2309+
}),
2310+
synthetic: param.attrs.iter()
2311+
.filter(|attr| attr.check_name("rustc_synthetic"))
2312+
.map(|_| hir::SyntheticTyParamKind::ImplTrait)
2313+
.next(),
2314+
};
2315+
2316+
(hir::ParamName::Plain(ident), kind)
23292317
}
23302318
GenericParamKind::Const { ref ty } => {
2331-
hir::GenericParam {
2332-
id: self.lower_node_id(param.id).node_id,
2333-
name: hir::ParamName::Plain(param.ident),
2334-
span: param.ident.span,
2335-
pure_wrt_drop: attr::contains_name(&param.attrs, "may_dangle"),
2336-
attrs: self.lower_attrs(&param.attrs),
2337-
bounds,
2338-
kind: hir::GenericParamKind::Const {
2339-
ty: self.lower_ty(&ty, ImplTraitContext::Disallowed),
2340-
}
2341-
}
2319+
(hir::ParamName::Plain(param.ident), hir::GenericParamKind::Const {
2320+
ty: self.lower_ty(&ty, ImplTraitContext::Disallowed),
2321+
})
23422322
}
2323+
};
2324+
2325+
hir::GenericParam {
2326+
id: self.lower_node_id(param.id).node_id,
2327+
name,
2328+
span: param.ident.span,
2329+
pure_wrt_drop: attr::contains_name(&param.attrs, "may_dangle"),
2330+
attrs: self.lower_attrs(&param.attrs),
2331+
bounds,
2332+
kind,
23432333
}
23442334
}
23452335

src/librustc/middle/resolve_lifetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,7 @@ fn object_lifetime_defaults_for_item(
13721372
})
13731373
}
13741374
GenericParamKind::Const { .. } => {
1375-
// TODO(const_generics):
1375+
// Generic consts don't impose any constraints.
13761376
None
13771377
}
13781378
})

src/librustc/ty/error.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ pub enum TypeError<'tcx> {
6161
// Data structure used in const unification
6262
#[derive(Clone, Debug)]
6363
pub enum ConstError<'tcx> {
64-
Types(ExpectedFound<&'tcx ty::Const<'tcx>>),
6564
Mismatch(ExpectedFound<&'tcx ty::Const<'tcx>>),
6665
}
6766

src/librustc/ty/relate.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -591,26 +591,23 @@ pub fn super_relate_consts<'a, 'gcx, 'tcx, R>(relation: &mut R,
591591
-> RelateResult<'tcx, &'tcx Const<'tcx>>
592592
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
593593
{
594-
if a.ty != b.ty {
595-
// TODO(const_generics): replace with an assert
596-
return Err(TypeError::ConstError(
597-
ConstError::Types(expected_found(relation, &a, &b))
598-
));
599-
}
594+
assert!(a.ty != b.ty);
600595

601596
let tcx = relation.tcx();
602597
// Currently, the values that can be unified are those that
603598
// implement both `PartialEq` and `Eq`, corresponding to
604599
// `structural_match` types.
605600
// FIXME(const_generics): check for `structural_match` synthetic attribute.
606-
// TODO(const_generics): a == b is only valid if a, b are Scalar, ScalarPair, ByRef
607601
// TODO(const_generics): possibly need indirection for ByRef?
608602
// TODO(const_generics): Param should be a bug here
609603
match (a.val, b.val) {
610604
(ConstValue::Infer(_), _) | (_, ConstValue::Infer(_)) => {
611605
// The caller should handle these cases!
612606
bug!("var types encountered in super_relate_consts")
613607
}
608+
(ConstValue::Param(_), _) | (_, ConstValue::Param(_)) => {
609+
bug!("param types encountered in super_relate_consts")
610+
}
614611
(ConstValue::Scalar(_), _) |
615612
(ConstValue::ScalarPair(..), _) |
616613
(ConstValue::ByRef(..), _)
@@ -622,20 +619,16 @@ pub fn super_relate_consts<'a, 'gcx, 'tcx, R>(relation: &mut R,
622619
(ConstValue::Unevaluated(a_def_id, a_substs), ConstValue::Unevaluated(b_def_id, b_substs))
623620
if a_def_id == b_def_id =>
624621
{
625-
// TODO(const_generics): should just using an invariant (covariance) method
626622
let substs =
627623
relation.relate_with_variance(ty::Variance::Invariant, &a_substs, &b_substs)?;
628624
Ok(tcx.mk_const(ty::Const {
629625
val: ConstValue::Unevaluated(a_def_id, substs),
630626
ty: a.ty,
631627
}))
632-
}
633-
(ConstValue::Param(ref a_p), ConstValue::Param(ref b_p)) if a_p.index == b_p.index => {
634-
Ok(a)
635628
}
636629
_ => {
637630
Err(TypeError::ConstError(
638-
ConstError::Types(expected_found(relation, &a, &b))
631+
ConstError::Mismatch(expected_found(relation, &a, &b))
639632
))
640633
}
641634
}

src/librustc/ty/structural_impls.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,6 @@ impl<'a, 'tcx> Lift<'tcx> for ty::error::ConstError<'a> {
466466
use ty::error::ConstError::*;
467467

468468
match *self {
469-
Types(ref x) => return tcx.lift(x).map(Types),
470469
Mismatch(ref x) => return tcx.lift(x).map(Mismatch),
471470
}
472471
}
@@ -1150,7 +1149,6 @@ EnumTypeFoldableImpl! {
11501149

11511150
EnumTypeFoldableImpl! {
11521151
impl<'tcx> TypeFoldable<'tcx> for ty::error::ConstError<'tcx> {
1153-
(ty::error::ConstError::Types)(x),
11541152
(ty::error::ConstError::Mismatch)(x),
11551153
}
11561154
}

src/librustc/util/ppaux.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ impl PrintContext {
344344
GenericParamDefKind::Type { has_default, .. } => {
345345
Some((param.def_id, has_default))
346346
}
347-
GenericParamDefKind::Const { .. } => unimplemented!(), // TODO(const_generics):
347+
GenericParamDefKind::Const { .. } => None, // FIXME(const_generics:defaults)
348348
}).peekable();
349349
let has_default = {
350350
let has_default = type_params.peek().map(|(_, has_default)| has_default);

src/librustc_codegen_llvm/mir/operand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ impl OperandRef<'ll, 'tcx> {
9090

9191
let val = match val.val {
9292
ConstValue::Unevaluated(..) => bug!(),
93-
ConstValue::Param(_) => unimplemented!(), // TODO(const_generics)
94-
ConstValue::Infer(_) => unimplemented!(), // TODO(const_generics)
93+
ConstValue::Param(_) => bug!(),
94+
ConstValue::Infer(_) => bug!(),
9595
ConstValue::Scalar(x) => {
9696
let scalar = match layout.abi {
9797
layout::Abi::Scalar(ref x) => x,

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,10 +1336,9 @@ fn slice_pat_covered_by_constructor<'tcx>(
13361336
ConstantValue(const_val) => {
13371337
let val = match const_val.val {
13381338
ConstValue::Unevaluated(..) |
1339+
ConstValue::Param(_) | ConstValue::Infer(_) |
13391340
ConstValue::ByRef(..) => bug!("unexpected ConstValue: {:?}", const_val),
13401341
ConstValue::Scalar(val) | ConstValue::ScalarPair(val, _) => val,
1341-
ConstValue::Param(_) | // TODO(const_generics)
1342-
ConstValue::Infer(_) => unimplemented!(), // TODO(const_generics)
13431342
};
13441343
if let Ok(ptr) = val.to_ptr() {
13451344
let is_array_ptr = const_val.ty

src/librustc_typeck/check/dropck.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,9 @@ pub fn check_safety_of_destructor_if_necessary<'a, 'gcx, 'tcx>(
306306
match kind.unpack() {
307307
UnpackedKind::Lifetime(r) => rcx.sub_regions(origin(), parent_scope, r),
308308
UnpackedKind::Type(ty) => rcx.type_must_outlive(origin(), ty, parent_scope),
309-
UnpackedKind::Const(_ct) => unimplemented!(), // TODO(const_generics):
309+
UnpackedKind::Const(_) => {
310+
// Generic consts don't add constraints.
311+
}
310312
}
311313
}
312314
Ok(())

src/librustc_typeck/check/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,15 +1330,22 @@ pub fn check_item_type<'a,'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, it: &'tcx hir::Item
13301330
} else {
13311331
for item in &m.items {
13321332
let generics = tcx.generics_of(tcx.hir.local_def_id(item.id));
1333-
if generics.params.len() - generics.own_counts().lifetimes != 0 {
1333+
let own_counts = generics.own_counts();
1334+
if generics.params.len() - own_counts.lifetimes != 0 {
13341335
let mut err = struct_span_err!(tcx.sess, item.span, E0044,
13351336
"foreign items may not have type or const parameters");
13361337
err.span_label(item.span, "can't have type or const parameters");
13371338
// FIXME: once we start storing spans for type arguments, turn this into a
13381339
// suggestion.
1339-
// TODO(const_generics): help message
1340-
err.help("use specialization instead of type parameters by replacing them \
1341-
with concrete types like `u32`");
1340+
if own_counts.types > 0 {
1341+
err.help("use specialization instead of type parameters by replacing \
1342+
them with concrete types like `u32`");
1343+
}
1344+
if own_counts.consts > 0 {
1345+
err.help("use specialization instead of const parameters by replacing \
1346+
them with concrete consts like `0`");
1347+
}
1348+
13421349
err.emit();
13431350
}
13441351

@@ -1348,7 +1355,7 @@ pub fn check_item_type<'a,'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, it: &'tcx hir::Item
13481355
}
13491356
}
13501357
}
1351-
_ => {/* nothing to do */ }
1358+
_ => { /* nothing to do */ }
13521359
}
13531360
}
13541361

0 commit comments

Comments
 (0)