Skip to content

Commit

Permalink
Auto merge of rust-lang#133189 - GuillaumeGomez:rollup-rhylr1f, r=Gui…
Browse files Browse the repository at this point in the history
…llaumeGomez

Rollup of 6 pull requests

Successful merges:

 - rust-lang#132577 (Report the `unexpected_cfgs` lint in external macros)
 - rust-lang#132758 (Improve `{BTreeMap,HashMap}::get_key_value` docs.)
 - rust-lang#133180 ([rustdoc] Fix items with generics not having their jump to def link generated)
 - rust-lang#133181 (Update books)
 - rust-lang#133182 (const_panic: inline in bootstrap builds to avoid f16/f128 crashes)
 - rust-lang#133187 (Add reference annotations for diagnostic attributes)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Nov 19, 2024
2 parents b71fb5e + 87d1684 commit e364066
Show file tree
Hide file tree
Showing 44 changed files with 332 additions and 172 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ dependencies = [

[[package]]
name = "anstream"
version = "0.6.17"
version = "0.6.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23a1e53f0f5d86382dafe1cf314783b2044280f406e7e1506368220ad11b1338"
checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b"
dependencies = [
"anstyle",
"anstyle-parse",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3185,6 +3185,7 @@ declare_lint! {
pub UNEXPECTED_CFGS,
Warn,
"detects unexpected names and values in `#[cfg]` conditions",
report_in_external_macro
}

declare_lint! {
Expand Down
46 changes: 42 additions & 4 deletions library/alloc/src/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,20 +677,58 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
}
}

/// Returns the key-value pair corresponding to the supplied key.
/// Returns the key-value pair corresponding to the supplied key. This is
/// potentially useful:
/// - for key types where non-identical keys can be considered equal;
/// - for getting the `&K` stored key value from a borrowed `&Q` lookup key; or
/// - for getting a reference to a key with the same lifetime as the collection.
///
/// The supplied key may be any borrowed form of the map's key type, but the ordering
/// on the borrowed form *must* match the ordering on the key type.
///
/// # Examples
///
/// ```
/// use std::cmp::Ordering;
/// use std::collections::BTreeMap;
///
/// #[derive(Clone, Copy, Debug)]
/// struct S {
/// id: u32,
/// # #[allow(unused)] // prevents a "field `name` is never read" error
/// name: &'static str, // ignored by equality and ordering operations
/// }
///
/// impl PartialEq for S {
/// fn eq(&self, other: &S) -> bool {
/// self.id == other.id
/// }
/// }
///
/// impl Eq for S {}
///
/// impl PartialOrd for S {
/// fn partial_cmp(&self, other: &S) -> Option<Ordering> {
/// self.id.partial_cmp(&other.id)
/// }
/// }
///
/// impl Ord for S {
/// fn cmp(&self, other: &S) -> Ordering {
/// self.id.cmp(&other.id)
/// }
/// }
///
/// let j_a = S { id: 1, name: "Jessica" };
/// let j_b = S { id: 1, name: "Jess" };
/// let p = S { id: 2, name: "Paul" };
/// assert_eq!(j_a, j_b);
///
/// let mut map = BTreeMap::new();
/// map.insert(1, "a");
/// assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
/// assert_eq!(map.get_key_value(&2), None);
/// map.insert(j_a, "Paris");
/// assert_eq!(map.get_key_value(&j_a), Some((&j_a, &"Paris")));
/// assert_eq!(map.get_key_value(&j_b), Some((&j_a, &"Paris"))); // the notable case
/// assert_eq!(map.get_key_value(&p), None);
/// ```
#[stable(feature = "map_get_key_value", since = "1.40.0")]
pub fn get_key_value<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ pub macro const_panic {
#[noinline]
if const #[track_caller] #[inline] { // Inline this, to prevent codegen
$crate::panic!($const_msg)
} else #[track_caller] { // Do not inline this, it makes perf worse
} else #[track_caller] #[cfg_attr(bootstrap, inline)] { // Do not inline this, it makes perf worse
$crate::panic!($runtime_msg)
}
)
Expand Down
40 changes: 36 additions & 4 deletions library/std/src/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,11 @@ where
self.base.get(k)
}

/// Returns the key-value pair corresponding to the supplied key.
/// Returns the key-value pair corresponding to the supplied key. This is
/// potentially useful:
/// - for key types where non-identical keys can be considered equal;
/// - for getting the `&K` stored key value from a borrowed `&Q` lookup key; or
/// - for getting a reference to a key with the same lifetime as the collection.
///
/// The supplied key may be any borrowed form of the map's key type, but
/// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
Expand All @@ -890,11 +894,39 @@ where
///
/// ```
/// use std::collections::HashMap;
/// use std::hash::{Hash, Hasher};
///
/// #[derive(Clone, Copy, Debug)]
/// struct S {
/// id: u32,
/// # #[allow(unused)] // prevents a "field `name` is never read" error
/// name: &'static str, // ignored by equality and hashing operations
/// }
///
/// impl PartialEq for S {
/// fn eq(&self, other: &S) -> bool {
/// self.id == other.id
/// }
/// }
///
/// impl Eq for S {}
///
/// impl Hash for S {
/// fn hash<H: Hasher>(&self, state: &mut H) {
/// self.id.hash(state);
/// }
/// }
///
/// let j_a = S { id: 1, name: "Jessica" };
/// let j_b = S { id: 1, name: "Jess" };
/// let p = S { id: 2, name: "Paul" };
/// assert_eq!(j_a, j_b);
///
/// let mut map = HashMap::new();
/// map.insert(1, "a");
/// assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
/// assert_eq!(map.get_key_value(&2), None);
/// map.insert(j_a, "Paris");
/// assert_eq!(map.get_key_value(&j_a), Some((&j_a, &"Paris")));
/// assert_eq!(map.get_key_value(&j_b), Some((&j_a, &"Paris"))); // the notable case
/// assert_eq!(map.get_key_value(&p), None);
/// ```
#[inline]
#[stable(feature = "map_get_key_value", since = "1.40.0")]
Expand Down
2 changes: 1 addition & 1 deletion src/doc/nomicon
23 changes: 19 additions & 4 deletions src/librustdoc/html/render/span_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub(crate) enum LinkFromSrc {
/// It returns the `krate`, the source code files and the `span` correspondence map.
///
/// Note about the `span` correspondence map: the keys are actually `(lo, hi)` of `span`s. We don't
/// need the `span` context later on, only their position, so instead of keep a whole `Span`, we
/// need the `span` context later on, only their position, so instead of keeping a whole `Span`, we
/// only keep the `lo` and `hi`.
pub(crate) fn collect_spans_and_sources(
tcx: TyCtxt<'_>,
Expand All @@ -45,9 +45,9 @@ pub(crate) fn collect_spans_and_sources(
include_sources: bool,
generate_link_to_definition: bool,
) -> (FxIndexMap<PathBuf, String>, FxHashMap<Span, LinkFromSrc>) {
let mut visitor = SpanMapVisitor { tcx, matches: FxHashMap::default() };

if include_sources {
let mut visitor = SpanMapVisitor { tcx, matches: FxHashMap::default() };

if generate_link_to_definition {
tcx.hir().walk_toplevel_module(&mut visitor);
}
Expand Down Expand Up @@ -76,7 +76,22 @@ impl<'tcx> SpanMapVisitor<'tcx> {
} else {
LinkFromSrc::External(def_id)
};
self.matches.insert(path.span, link);
// In case the path ends with generics, we remove them from the span.
let span = path
.segments
.last()
.map(|last| {
// In `use` statements, the included item is not in the path segments.
// However, it doesn't matter because you can't have generics on `use`
// statements.
if path.span.contains(last.ident.span) {
path.span.with_hi(last.ident.span.hi())
} else {
path.span
}
})
.unwrap_or(path.span);
self.matches.insert(span, link);
}
Res::Local(_) => {
if let Some(span) = self.tcx.hir().res_span(path.res) {
Expand Down
10 changes: 5 additions & 5 deletions src/tools/miri/miri-script/Cargo.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
version = 4

[[package]]
name = "anyhow"
Expand Down Expand Up @@ -521,15 +521,15 @@ checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904"

[[package]]
name = "xshell"
version = "0.2.6"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db0ab86eae739efd1b054a8d3d16041914030ac4e01cd1dca0cf252fd8b6437"
checksum = "9e7290c623014758632efe00737145b6867b66292c42167f2ec381eb566a373d"
dependencies = [
"xshell-macros",
]

[[package]]
name = "xshell-macros"
version = "0.2.6"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d422e8e38ec76e2f06ee439ccc765e9c6a9638b9e7c9f2e8255e4d41e8bd852"
checksum = "32ac00cd3f8ec9c1d33fb3e7958a82df6989c42d747bd326c822b1d625283547"
14 changes: 14 additions & 0 deletions tests/rustdoc/link-on-path-with-generics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// This test ensures that paths with generics still get their link to their definition
// correctly generated.

//@ compile-flags: -Zunstable-options --generate-link-to-definition
#![crate_name = "foo"]

//@ has 'src/foo/link-on-path-with-generics.rs.html'

pub struct Soyo<T>(T);
pub struct Saya;

//@ has - '//pre[@class="rust"]//a[@href="#9"]' 'Soyo'
//@ has - '//pre[@class="rust"]//a[@href="#10"]' 'Saya'
pub fn bar<T>(s: Soyo<T>, x: Saya) {}
11 changes: 11 additions & 0 deletions tests/ui/check-cfg/auxiliary/cfg_macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Inspired by https://github.com/rust-lang/cargo/issues/14775

pub fn my_lib_func() {}

#[macro_export]
macro_rules! my_lib_macro {
() => {
#[cfg(my_lib_cfg)]
$crate::my_lib_func()
};
}
12 changes: 12 additions & 0 deletions tests/ui/check-cfg/report-in-external-macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This test checks that we emit the `unexpected_cfgs` lint even in code
// coming from an external macro.

//@ check-pass
//@ no-auto-check-cfg
//@ aux-crate: cfg_macro=cfg_macro.rs
//@ compile-flags: --check-cfg=cfg()

fn main() {
cfg_macro::my_lib_macro!();
//~^ WARNING unexpected `cfg` condition name
}
14 changes: 14 additions & 0 deletions tests/ui/check-cfg/report-in-external-macros.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
warning: unexpected `cfg` condition name: `my_lib_cfg`
--> $DIR/report-in-external-macros.rs:10:5
|
LL | cfg_macro::my_lib_macro!();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: expected names are: `clippy`, `debug_assertions`, `doc`, `doctest`, `fmt_debug`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `ub_checks`, `unix`, and `windows`
= help: to expect this configuration use `--check-cfg=cfg(my_lib_cfg)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
= note: `#[warn(unexpected_cfgs)]` on by default
= note: this warning originates in the macro `cfg_macro::my_lib_macro` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: 1 warning emitted

2 changes: 2 additions & 0 deletions tests/ui/diagnostic_namespace/deny_malformed_attribute.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//@ reference: attributes.diagnostic.namespace.unknown-invalid-syntax

#![deny(unknown_or_malformed_diagnostic_attributes)]

#[diagnostic::unknown_attribute]
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/diagnostic_namespace/deny_malformed_attribute.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error: unknown diagnostic attribute
--> $DIR/deny_malformed_attribute.rs:3:15
--> $DIR/deny_malformed_attribute.rs:5:15
|
LL | #[diagnostic::unknown_attribute]
| ^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/deny_malformed_attribute.rs:1:9
--> $DIR/deny_malformed_attribute.rs:3:9
|
LL | #![deny(unknown_or_malformed_diagnostic_attributes)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//@ edition:2021
//@ aux-build:bad_on_unimplemented.rs
//@ reference: attributes.diagnostic.on_unimplemented.syntax

// Do not ICE when encountering a malformed `#[diagnostic::on_unimplemented]` annotation in a
// dependency when incorrectly used (#124651).
Expand Down
Loading

0 comments on commit e364066

Please sign in to comment.