Skip to content

Commit 098c659

Browse files
committed
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).
1 parent 8ec22e7 commit 098c659

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/librustc/traits/auto_trait.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
309309
) -> Option<(ty::ParamEnv<'c>, ty::ParamEnv<'c>)> {
310310
let tcx = infcx.tcx;
311311

312-
let mut select = SelectionContext::new(&infcx);
312+
let mut select = SelectionContext::with_negative(&infcx, true);
313313

314314
let mut already_visited = FxHashSet::default();
315315
let mut predicates = VecDeque::new();
@@ -338,6 +338,19 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
338338

339339
match &result {
340340
&Ok(Some(ref vtable)) => {
341+
// If we see an explicit negative impl (e.g. 'impl !Send for MyStruct'),
342+
// we immediately bail out, since it's impossible for us to continue'
343+
match vtable {
344+
Vtable::VtableImpl(VtableImplData { impl_def_id, .. }) => {
345+
if infcx.tcx.impl_polarity(*impl_def_id) == hir::ImplPolarity::Negative {
346+
debug!("evaluate_nested_obligations: Found explicit negative impl\
347+
{:?}, bailing out", impl_def_id);
348+
return None;
349+
}
350+
},
351+
_ => {}
352+
}
353+
341354
let obligations = vtable.clone().nested_obligations().into_iter();
342355

343356
if !self.evaluate_nested_obligations(

src/test/rustdoc/issue-55321.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
12+
#![feature(optin_builtin_traits)]
13+
14+
// @has issue_55321/struct.A.html
15+
// @has - '//*[@id="implementations-list"]/*[@class="impl"]//*/code' "impl !Send for A"
16+
// @has - '//*[@id="implementations-list"]/*[@class="impl"]//*/code' "impl !Sync for A"
17+
pub struct A();
18+
19+
impl !Send for A {}
20+
impl !Sync for A {}
21+
22+
// @has issue_55321/struct.B.html
23+
// @has - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]//*/code' "impl<T> !Send for \
24+
// B<T>"
25+
// @has - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]//*/code' "impl<T> !Sync for \
26+
// B<T>"
27+
pub struct B<T: ?Sized>(A, Box<T>);

0 commit comments

Comments
 (0)