Skip to content

Commit

Permalink
Implement soundness check for min_specialization
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper committed Mar 15, 2020
1 parent 32d330d commit 0bbbe71
Show file tree
Hide file tree
Showing 24 changed files with 796 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/librustc_typeck/constrained_generic_params.rs
Expand Up @@ -79,10 +79,18 @@ impl<'tcx> TypeVisitor<'tcx> for ParameterCollector {
}

fn visit_const(&mut self, c: &'tcx ty::Const<'tcx>) -> bool {
if let ty::ConstKind::Param(data) = c.val {
self.parameters.push(Parameter::from(data));
match c.val {
ty::ConstKind::Unevaluated(..) if !self.include_nonconstraining => {
// Constant expressions are not injective
return c.ty.visit_with(self);
}
ty::ConstKind::Param(data) => {
self.parameters.push(Parameter::from(data));
}
_ => {}
}
false

c.super_visit_with(self)
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/librustc_typeck/impl_wf_check.rs
Expand Up @@ -9,16 +9,20 @@
//! fixed, but for the moment it's easier to do these checks early.

use crate::constrained_generic_params as cgp;
use min_specialization::check_min_specialization;

use rustc::ty::query::Providers;
use rustc::ty::{self, TyCtxt, TypeFoldable};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_span::Span;

use std::collections::hash_map::Entry::{Occupied, Vacant};

use rustc_span::Span;
mod min_specialization;

/// Checks that all the type/lifetime parameters on an impl also
/// appear in the trait ref or self type (or are constrained by a
Expand Down Expand Up @@ -60,7 +64,9 @@ pub fn impl_wf_check(tcx: TyCtxt<'_>) {
}

fn check_mod_impl_wf(tcx: TyCtxt<'_>, module_def_id: DefId) {
tcx.hir().visit_item_likes_in_module(module_def_id, &mut ImplWfCheck { tcx });
let min_specialization = tcx.features().min_specialization;
tcx.hir()
.visit_item_likes_in_module(module_def_id, &mut ImplWfCheck { tcx, min_specialization });
}

pub fn provide(providers: &mut Providers<'_>) {
Expand All @@ -69,6 +75,7 @@ pub fn provide(providers: &mut Providers<'_>) {

struct ImplWfCheck<'tcx> {
tcx: TyCtxt<'tcx>,
min_specialization: bool,
}

impl ItemLikeVisitor<'tcx> for ImplWfCheck<'tcx> {
Expand All @@ -77,6 +84,9 @@ impl ItemLikeVisitor<'tcx> for ImplWfCheck<'tcx> {
let impl_def_id = self.tcx.hir().local_def_id(item.hir_id);
enforce_impl_params_are_constrained(self.tcx, impl_def_id, items);
enforce_impl_items_are_distinct(self.tcx, items);
if self.min_specialization {
check_min_specialization(self.tcx, impl_def_id, item.span);
}
}
}

Expand Down

0 comments on commit 0bbbe71

Please sign in to comment.