From 098c659276d60fff7e0f225e1d6bc7d05591319e Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Thu, 25 Oct 2018 15:14:32 -0400 Subject: [PATCH 1/3] Check for negative impls when finding auto traits Fixes #55321 When AutoTraitFinder begins examining a type, it checks for an explicit negative impl. However, it wasn't checking for negative impls found when calling 'select' on predicates found from nested obligations. This commit makes AutoTraitFinder check for negative impls whenever it makes a call to 'select'. If a negative impl is found, it immediately bails out. Normal users of SelectioContext don't need to worry about this, since they stop as soon as an Unimplemented error is encountered. However, we add predicates to our ParamEnv when we encounter this error, so we need to handle negative impls specially (so that we don't try adding them to our ParamEnv). --- src/librustc/traits/auto_trait.rs | 15 ++++++++++++++- src/test/rustdoc/issue-55321.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/test/rustdoc/issue-55321.rs diff --git a/src/librustc/traits/auto_trait.rs b/src/librustc/traits/auto_trait.rs index 50ca6ca78ab3a..1600634a199e4 100644 --- a/src/librustc/traits/auto_trait.rs +++ b/src/librustc/traits/auto_trait.rs @@ -309,7 +309,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> { ) -> Option<(ty::ParamEnv<'c>, ty::ParamEnv<'c>)> { let tcx = infcx.tcx; - let mut select = SelectionContext::new(&infcx); + let mut select = SelectionContext::with_negative(&infcx, true); let mut already_visited = FxHashSet::default(); let mut predicates = VecDeque::new(); @@ -338,6 +338,19 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> { match &result { &Ok(Some(ref vtable)) => { + // If we see an explicit negative impl (e.g. 'impl !Send for MyStruct'), + // we immediately bail out, since it's impossible for us to continue' + match vtable { + Vtable::VtableImpl(VtableImplData { impl_def_id, .. }) => { + if infcx.tcx.impl_polarity(*impl_def_id) == hir::ImplPolarity::Negative { + debug!("evaluate_nested_obligations: Found explicit negative impl\ + {:?}, bailing out", impl_def_id); + return None; + } + }, + _ => {} + } + let obligations = vtable.clone().nested_obligations().into_iter(); if !self.evaluate_nested_obligations( diff --git a/src/test/rustdoc/issue-55321.rs b/src/test/rustdoc/issue-55321.rs new file mode 100644 index 0000000000000..f6ba7c7be177e --- /dev/null +++ b/src/test/rustdoc/issue-55321.rs @@ -0,0 +1,27 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + + +#![feature(optin_builtin_traits)] + +// @has issue_55321/struct.A.html +// @has - '//*[@id="implementations-list"]/*[@class="impl"]//*/code' "impl !Send for A" +// @has - '//*[@id="implementations-list"]/*[@class="impl"]//*/code' "impl !Sync for A" +pub struct A(); + +impl !Send for A {} +impl !Sync for A {} + +// @has issue_55321/struct.B.html +// @has - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]//*/code' "impl !Send for \ +// B" +// @has - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]//*/code' "impl !Sync for \ +// B" +pub struct B(A, Box); From fa444be81d9acf9d99adcbc930da144e89480515 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Thu, 25 Oct 2018 15:32:59 -0400 Subject: [PATCH 2/3] Fix tidy error --- src/librustc/traits/auto_trait.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/librustc/traits/auto_trait.rs b/src/librustc/traits/auto_trait.rs index 1600634a199e4..e5d7b5d5e94ee 100644 --- a/src/librustc/traits/auto_trait.rs +++ b/src/librustc/traits/auto_trait.rs @@ -342,7 +342,9 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> { // we immediately bail out, since it's impossible for us to continue' match vtable { Vtable::VtableImpl(VtableImplData { impl_def_id, .. }) => { - if infcx.tcx.impl_polarity(*impl_def_id) == hir::ImplPolarity::Negative { + // Blame tidy for the weird bracket placement + if infcx.tcx.impl_polarity(*impl_def_id) == hir::ImplPolarity::Negative + { debug!("evaluate_nested_obligations: Found explicit negative impl\ {:?}, bailing out", impl_def_id); return None; From 56acb2a00180c1e041e03e48c1c1818c305e13c6 Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 8 Nov 2018 12:15:26 -0500 Subject: [PATCH 3/3] Fix typo in comment Co-Authored-By: Aaron1011 --- src/librustc/traits/auto_trait.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc/traits/auto_trait.rs b/src/librustc/traits/auto_trait.rs index e5d7b5d5e94ee..2087ced25319e 100644 --- a/src/librustc/traits/auto_trait.rs +++ b/src/librustc/traits/auto_trait.rs @@ -339,7 +339,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> { match &result { &Ok(Some(ref vtable)) => { // If we see an explicit negative impl (e.g. 'impl !Send for MyStruct'), - // we immediately bail out, since it's impossible for us to continue' + // we immediately bail out, since it's impossible for us to continue. match vtable { Vtable::VtableImpl(VtableImplData { impl_def_id, .. }) => { // Blame tidy for the weird bracket placement