Skip to content

Commit 1ec7ae1

Browse files
committed
resolve the rustc_reservation_impl attribute in 1 place
1 parent 9a94ecd commit 1ec7ae1

File tree

11 files changed

+112
-66
lines changed

11 files changed

+112
-66
lines changed

src/librustc/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ rustc_queries! {
286286
query associated_item(_: DefId) -> ty::AssocItem {}
287287

288288
query impl_trait_ref(_: DefId) -> Option<ty::TraitRef<'tcx>> {}
289-
query impl_polarity(_: DefId) -> hir::ImplPolarity {}
289+
query impl_polarity(_: DefId) -> ty::ImplPolarity {}
290290

291291
query issue33140_self_ty(_: DefId) -> Option<ty::Ty<'tcx>> {}
292292
}

src/librustc/traits/auto_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ impl AutoTraitFinder<'tcx> {
321321
match vtable {
322322
Vtable::VtableImpl(VtableImplData { impl_def_id, .. }) => {
323323
// Blame tidy for the weird bracket placement
324-
if infcx.tcx.impl_polarity(*impl_def_id) == hir::ImplPolarity::Negative
324+
if infcx.tcx.impl_polarity(*impl_def_id) == ty::ImplPolarity::Negative
325325
{
326326
debug!("evaluate_nested_obligations: Found explicit negative impl\
327327
{:?}, bailing out", impl_def_id);

src/librustc/traits/select.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ use std::iter;
5050
use std::rc::Rc;
5151
use crate::util::nodemap::{FxHashMap, FxHashSet};
5252

53-
use syntax::symbol::sym;
54-
5553
pub struct SelectionContext<'cx, 'tcx> {
5654
infcx: &'cx InferCtxt<'cx, 'tcx>,
5755

@@ -1334,15 +1332,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
13341332
candidate: SelectionCandidate<'tcx>,
13351333
) -> SelectionResult<'tcx, SelectionCandidate<'tcx>> {
13361334
if let ImplCandidate(def_id) = candidate {
1337-
if !self.allow_negative_impls
1338-
&& self.tcx().impl_polarity(def_id) == hir::ImplPolarity::Negative
1339-
{
1340-
return Err(Unimplemented);
1341-
}
1342-
1343-
if self.tcx().has_attr(def_id, sym::rustc_reservation_impl) {
1344-
return Ok(None);
1345-
}
1335+
match self.tcx().impl_polarity(def_id) {
1336+
ty::ImplPolarity::Negative if !self.allow_negative_impls => {
1337+
return Err(Unimplemented);
1338+
}
1339+
ty::ImplPolarity::Reservation => {
1340+
return Ok(None);
1341+
}
1342+
_ => {}
1343+
};
13461344
}
13471345
Ok(Some(candidate))
13481346
}
@@ -3734,8 +3732,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
37343732
return Err(());
37353733
}
37363734

3737-
if self.intercrate.is_none() &&
3738-
self.tcx().has_attr(impl_def_id, sym::rustc_reservation_impl)
3735+
if self.intercrate.is_none()
3736+
&& self.tcx().impl_polarity(impl_def_id) == ty::ImplPolarity::Reservation
37393737
{
37403738
debug!("match_impl: reservation impls only apply in intercrate mode");
37413739
return Err(());

src/librustc/ty/mod.rs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ pub struct ImplHeader<'tcx> {
167167
pub predicates: Vec<Predicate<'tcx>>,
168168
}
169169

170+
#[derive(Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, HashStable)]
171+
pub enum ImplPolarity {
172+
/// `impl Trait for Type`
173+
Positive,
174+
/// `impl !Trait for Type`
175+
Negative,
176+
/// `#[rustc_reservation_impl] impl Trait for Type`
177+
Reservation,
178+
}
179+
170180
#[derive(Copy, Clone, Debug, PartialEq, HashStable)]
171181
pub struct AssocItem {
172182
pub def_id: DefId,
@@ -2911,11 +2921,24 @@ impl<'tcx> TyCtxt<'tcx> {
29112921
return Some(ImplOverlapKind::Permitted);
29122922
}
29132923

2914-
if self.impl_polarity(def_id1) != self.impl_polarity(def_id2) {
2915-
debug!("impls_are_allowed_to_overlap({:?}, {:?}) - different polarities, None",
2916-
def_id1, def_id2);
2917-
return None;
2918-
}
2924+
match (self.impl_polarity(def_id1), self.impl_polarity(def_id2)) {
2925+
(ImplPolarity::Reservation, _) |
2926+
(_, ImplPolarity::Reservation) => {
2927+
// `#[rustc_reservation_impl]` impls don't overlap with anything
2928+
debug!("impls_are_allowed_to_overlap({:?}, {:?}) = Some(Permitted) (reservations)",
2929+
def_id1, def_id2);
2930+
return Some(ImplOverlapKind::Permitted);
2931+
}
2932+
(ImplPolarity::Positive, ImplPolarity::Negative) |
2933+
(ImplPolarity::Negative, ImplPolarity::Positive) => {
2934+
// FIXME: when can this happen?
2935+
debug!("impls_are_allowed_to_overlap({:?}, {:?}) - None (differing polarities)",
2936+
def_id1, def_id2);
2937+
return None;
2938+
}
2939+
(ImplPolarity::Positive, ImplPolarity::Positive) |
2940+
(ImplPolarity::Negative, ImplPolarity::Negative) => {}
2941+
};
29192942

29202943
let is_marker_overlap = if self.features().overlapping_marker_traits {
29212944
let trait1_is_empty = self.impl_trait_ref(def_id1)
@@ -2935,15 +2958,10 @@ impl<'tcx> TyCtxt<'tcx> {
29352958
is_marker_impl(def_id1) && is_marker_impl(def_id2)
29362959
};
29372960

2938-
// `#[rustc_reservation_impl]` impls don't overlap with anything
2939-
let is_reserve_overlap = {
2940-
self.has_attr(def_id1, sym::rustc_reservation_impl) ||
2941-
self.has_attr(def_id2, sym::rustc_reservation_impl)
2942-
};
29432961

2944-
if is_marker_overlap || is_reserve_overlap {
2945-
debug!("impls_are_allowed_to_overlap({:?}, {:?}) = Some(Permitted) ({:?}/{:?})",
2946-
def_id1, def_id2, is_marker_overlap, is_reserve_overlap);
2962+
if is_marker_overlap {
2963+
debug!("impls_are_allowed_to_overlap({:?}, {:?}) = Some(Permitted) (marker overlap)",
2964+
def_id1, def_id2);
29472965
Some(ImplOverlapKind::Permitted)
29482966
} else {
29492967
if let Some(self_ty1) = self.issue33140_self_ty(def_id1) {
@@ -3325,7 +3343,7 @@ fn issue33140_self_ty(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Ty<'_>> {
33253343
debug!("issue33140_self_ty({:?}), trait-ref={:?}", def_id, trait_ref);
33263344

33273345
let is_marker_like =
3328-
tcx.impl_polarity(def_id) == hir::ImplPolarity::Positive &&
3346+
tcx.impl_polarity(def_id) == ty::ImplPolarity::Positive &&
33293347
tcx.associated_item_def_ids(trait_ref.def_id).is_empty();
33303348

33313349
// Check whether these impls would be ok for a marker trait.

src/librustc_metadata/decoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ impl<'a, 'tcx> CrateMetadata {
722722
self.get_impl_data(id).parent_impl
723723
}
724724

725-
pub fn get_impl_polarity(&self, id: DefIndex) -> hir::ImplPolarity {
725+
pub fn get_impl_polarity(&self, id: DefIndex) -> ty::ImplPolarity {
726726
self.get_impl_data(id).polarity
727727
}
728728

src/librustc_metadata/encoder.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,8 +1172,9 @@ impl EncodeContext<'tcx> {
11721172
ctor_sig: None,
11731173
}), repr_options)
11741174
}
1175-
hir::ItemKind::Impl(_, polarity, defaultness, ..) => {
1175+
hir::ItemKind::Impl(_, _, defaultness, ..) => {
11761176
let trait_ref = tcx.impl_trait_ref(def_id);
1177+
let polarity = tcx.impl_polarity(def_id);
11771178
let parent = if let Some(trait_ref) = trait_ref {
11781179
let trait_def = tcx.trait_def(trait_ref.def_id);
11791180
trait_def.ancestors(tcx, def_id).nth(1).and_then(|node| {

src/librustc_metadata/schema.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ pub struct TraitAliasData<'tcx> {
327327

328328
#[derive(RustcEncodable, RustcDecodable)]
329329
pub struct ImplData<'tcx> {
330-
pub polarity: hir::ImplPolarity,
330+
pub polarity: ty::ImplPolarity,
331331
pub defaultness: hir::Defaultness,
332332
pub parent_impl: Option<DefId>,
333333

src/librustc_traits/lowering/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc::hir::def::DefKind;
44
use rustc::hir::def_id::DefId;
55
use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor};
66
use rustc::hir::map::definitions::DefPathData;
7-
use rustc::hir::{self, ImplPolarity};
7+
use rustc::hir;
88
use rustc::traits::{
99
Clause,
1010
Clauses,
@@ -295,7 +295,8 @@ fn program_clauses_for_trait(tcx: TyCtxt<'_>, def_id: DefId) -> Clauses<'_> {
295295
}
296296

297297
fn program_clauses_for_impl(tcx: TyCtxt<'tcx>, def_id: DefId) -> Clauses<'tcx> {
298-
if let ImplPolarity::Negative = tcx.impl_polarity(def_id) {
298+
// FIXME: implement reservation impls.
299+
if let ty::ImplPolarity::Negative = tcx.impl_polarity(def_id) {
299300
return List::empty();
300301
}
301302

src/librustc_typeck/check/wfcheck.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,27 @@ pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: DefId) {
9494
//
9595
// won't be allowed unless there's an *explicit* implementation of `Send`
9696
// for `T`
97-
hir::ItemKind::Impl(_, polarity, defaultness, _, ref trait_ref, ref self_ty, _) => {
97+
hir::ItemKind::Impl(_, _, defaultness, _, ref trait_ref, ref self_ty, _) => {
9898
let is_auto = tcx.impl_trait_ref(tcx.hir().local_def_id(item.hir_id))
99-
.map_or(false, |trait_ref| tcx.trait_is_auto(trait_ref.def_id));
99+
.map_or(false, |trait_ref| tcx.trait_is_auto(trait_ref.def_id));
100+
let polarity = tcx.impl_polarity(def_id);
100101
if let (hir::Defaultness::Default { .. }, true) = (defaultness, is_auto) {
101102
tcx.sess.span_err(item.span, "impls of auto traits cannot be default");
102103
}
103-
if polarity == hir::ImplPolarity::Positive {
104-
check_impl(tcx, item, self_ty, trait_ref);
105-
} else {
106-
// FIXME(#27579): what amount of WF checking do we need for neg impls?
107-
if trait_ref.is_some() && !is_auto {
108-
span_err!(tcx.sess, item.span, E0192,
109-
"negative impls are only allowed for \
110-
auto traits (e.g., `Send` and `Sync`)")
104+
match polarity {
105+
ty::ImplPolarity::Positive => {
106+
check_impl(tcx, item, self_ty, trait_ref);
107+
}
108+
ty::ImplPolarity::Negative => {
109+
// FIXME(#27579): what amount of WF checking do we need for neg impls?
110+
if trait_ref.is_some() && !is_auto {
111+
span_err!(tcx.sess, item.span, E0192,
112+
"negative impls are only allowed for \
113+
auto traits (e.g., `Send` and `Sync`)")
114+
}
115+
}
116+
ty::ImplPolarity::Reservation => {
117+
// FIXME: what amount of WF checking do we need for reservation impls?
111118
}
112119
}
113120
}
@@ -401,20 +408,18 @@ fn check_impl<'tcx>(
401408
// `#[rustc_reservation_impl]` impls are not real impls and
402409
// therefore don't need to be WF (the trait's `Self: Trait` predicate
403410
// won't hold).
404-
if !fcx.tcx.has_attr(item_def_id, sym::rustc_reservation_impl) {
405-
let trait_ref = fcx.tcx.impl_trait_ref(item_def_id).unwrap();
406-
let trait_ref =
407-
fcx.normalize_associated_types_in(
408-
ast_trait_ref.path.span, &trait_ref);
409-
let obligations =
410-
ty::wf::trait_obligations(fcx,
411-
fcx.param_env,
412-
fcx.body_id,
413-
&trait_ref,
414-
ast_trait_ref.path.span);
415-
for obligation in obligations {
416-
fcx.register_predicate(obligation);
417-
}
411+
let trait_ref = fcx.tcx.impl_trait_ref(item_def_id).unwrap();
412+
let trait_ref =
413+
fcx.normalize_associated_types_in(
414+
ast_trait_ref.path.span, &trait_ref);
415+
let obligations =
416+
ty::wf::trait_obligations(fcx,
417+
fcx.param_env,
418+
fcx.body_id,
419+
&trait_ref,
420+
ast_trait_ref.path.span);
421+
for obligation in obligations {
422+
fcx.register_predicate(obligation);
418423
}
419424
}
420425
None => {

src/librustc_typeck/collect.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,10 +1866,30 @@ fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::TraitRef<'_>> {
18661866
}
18671867
}
18681868

1869-
fn impl_polarity(tcx: TyCtxt<'_>, def_id: DefId) -> hir::ImplPolarity {
1869+
fn impl_polarity(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ImplPolarity {
18701870
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
1871-
match tcx.hir().expect_item(hir_id).node {
1872-
hir::ItemKind::Impl(_, polarity, ..) => polarity,
1871+
let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl);
1872+
let item = tcx.hir().expect_item(hir_id);
1873+
match &item.node {
1874+
hir::ItemKind::Impl(_, hir::ImplPolarity::Negative, ..) => {
1875+
if is_rustc_reservation {
1876+
tcx.sess.span_err(item.span, "reservation impls can't be negative");
1877+
}
1878+
ty::ImplPolarity::Negative
1879+
}
1880+
hir::ItemKind::Impl(_, hir::ImplPolarity::Positive, _, _, None, _, _) => {
1881+
if is_rustc_reservation {
1882+
tcx.sess.span_err(item.span, "reservation impls can't be inherent");
1883+
}
1884+
ty::ImplPolarity::Positive
1885+
}
1886+
hir::ItemKind::Impl(_, hir::ImplPolarity::Positive, _, _, Some(_tr), _, _) => {
1887+
if is_rustc_reservation {
1888+
ty::ImplPolarity::Reservation
1889+
} else {
1890+
ty::ImplPolarity::Positive
1891+
}
1892+
}
18731893
ref item => bug!("impl_polarity: {:?} not an impl", item),
18741894
}
18751895
}

0 commit comments

Comments
 (0)