Auto merge of #125134 - compiler-errors:negative-traits-are-not-notable, r=fmease

rustdoc: Negative impls are not notable

In #124097, we add `impl !Iterator for [T]` for coherence reasons, and since `Iterator` is a [notable trait](8387315ab3/library/core/src/iter/traits/iterator.rs (L40)), this means that all `-> &[_]` now are tagged with a `!Iterator` impl as a notable trait.

I "fixed" the failing tests in that PR with 6cbbb8b709a43482847243484ed67131e372ba71, where I just blessed the tests, since I didn't want to mix these changes with that PR; however, don't believe negative impls are notable, and this PR aims to prevent these impls from being mentioned.

In the standard library, we use negative impls purely to guide coherence. They're not really a signal of anything useful to the end-user. If there ever is a case that we want negative impls to be mentioned as notable, this really should be an opt-in feature.
This commit is contained in:
bors 2024-05-15 14:52:49 +00:00
commit b21b74b5e6
4 changed files with 32 additions and 0 deletions

View File

@ -1424,6 +1424,10 @@ pub(crate) fn notable_traits_button(ty: &clean::Type, cx: &mut Context<'_>) -> O
if let Some(impls) = cx.cache().impls.get(&did) {
for i in impls {
let impl_ = i.inner_impl();
if impl_.polarity != ty::ImplPolarity::Positive {
continue;
}
if !ty.is_doc_subtype_of(&impl_.for_, cx.cache()) {
// Two different types might have the same did,
// without actually being the same.
@ -1459,6 +1463,10 @@ fn notable_traits_decl(ty: &clean::Type, cx: &Context<'_>) -> (String, String) {
for i in impls {
let impl_ = i.inner_impl();
if impl_.polarity != ty::ImplPolarity::Positive {
continue;
}
if !ty.is_doc_subtype_of(&impl_.for_, cx.cache()) {
// Two different types might have the same did,
// without actually being the same.

View File

@ -0,0 +1 @@
<script type="text/json" id="notable-traits-data">{"Negative":"&lt;/code&gt;&lt;/pre&gt;"}</script>

View File

@ -0,0 +1 @@
<script type="text/json" id="notable-traits-data">{"Positive":"&lt;h3&gt;Notable traits for &lt;code&gt;&lt;a class=\"struct\" href=\"struct.Positive.html\" title=\"struct doc_notable_trait_negative::Positive\"&gt;Positive&lt;/a&gt;&lt;/code&gt;&lt;/h3&gt;&lt;pre&gt;&lt;code&gt;&lt;div class=\"where\"&gt;impl &lt;a class=\"trait\" href=\"trait.SomeTrait.html\" title=\"trait doc_notable_trait_negative::SomeTrait\"&gt;SomeTrait&lt;/a&gt; for &lt;a class=\"struct\" href=\"struct.Positive.html\" title=\"struct doc_notable_trait_negative::Positive\"&gt;Positive&lt;/a&gt;&lt;/div&gt;"}</script>

View File

@ -0,0 +1,22 @@
#![feature(doc_notable_trait, negative_impls)]
#[doc(notable_trait)]
pub trait SomeTrait {}
pub struct Positive;
impl SomeTrait for Positive {}
pub struct Negative;
impl !SomeTrait for Negative {}
// @has doc_notable_trait_negative/fn.positive.html
// @snapshot positive - '//script[@id="notable-traits-data"]'
pub fn positive() -> Positive {
todo!()
}
// @has doc_notable_trait_negative/fn.negative.html
// @count - '//script[@id="notable-traits-data"]' 0
pub fn negative() -> Negative {
&[]
}