Skip to content

Commit e364066

Browse files
committed
Auto merge of rust-lang#133189 - GuillaumeGomez:rollup-rhylr1f, r=GuillaumeGomez
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
2 parents b71fb5e + 87d1684 commit e364066

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+332
-172
lines changed

Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ dependencies = [
111111

112112
[[package]]
113113
name = "anstream"
114-
version = "0.6.17"
114+
version = "0.6.18"
115115
source = "registry+https://github.com/rust-lang/crates.io-index"
116-
checksum = "23a1e53f0f5d86382dafe1cf314783b2044280f406e7e1506368220ad11b1338"
116+
checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b"
117117
dependencies = [
118118
"anstyle",
119119
"anstyle-parse",

compiler/rustc_lint_defs/src/builtin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3185,6 +3185,7 @@ declare_lint! {
31853185
pub UNEXPECTED_CFGS,
31863186
Warn,
31873187
"detects unexpected names and values in `#[cfg]` conditions",
3188+
report_in_external_macro
31883189
}
31893190

31903191
declare_lint! {

library/alloc/src/collections/btree/map.rs

+42-4
Original file line numberDiff line numberDiff line change
@@ -677,20 +677,58 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
677677
}
678678
}
679679

680-
/// Returns the key-value pair corresponding to the supplied key.
680+
/// Returns the key-value pair corresponding to the supplied key. This is
681+
/// potentially useful:
682+
/// - for key types where non-identical keys can be considered equal;
683+
/// - for getting the `&K` stored key value from a borrowed `&Q` lookup key; or
684+
/// - for getting a reference to a key with the same lifetime as the collection.
681685
///
682686
/// The supplied key may be any borrowed form of the map's key type, but the ordering
683687
/// on the borrowed form *must* match the ordering on the key type.
684688
///
685689
/// # Examples
686690
///
687691
/// ```
692+
/// use std::cmp::Ordering;
688693
/// use std::collections::BTreeMap;
689694
///
695+
/// #[derive(Clone, Copy, Debug)]
696+
/// struct S {
697+
/// id: u32,
698+
/// # #[allow(unused)] // prevents a "field `name` is never read" error
699+
/// name: &'static str, // ignored by equality and ordering operations
700+
/// }
701+
///
702+
/// impl PartialEq for S {
703+
/// fn eq(&self, other: &S) -> bool {
704+
/// self.id == other.id
705+
/// }
706+
/// }
707+
///
708+
/// impl Eq for S {}
709+
///
710+
/// impl PartialOrd for S {
711+
/// fn partial_cmp(&self, other: &S) -> Option<Ordering> {
712+
/// self.id.partial_cmp(&other.id)
713+
/// }
714+
/// }
715+
///
716+
/// impl Ord for S {
717+
/// fn cmp(&self, other: &S) -> Ordering {
718+
/// self.id.cmp(&other.id)
719+
/// }
720+
/// }
721+
///
722+
/// let j_a = S { id: 1, name: "Jessica" };
723+
/// let j_b = S { id: 1, name: "Jess" };
724+
/// let p = S { id: 2, name: "Paul" };
725+
/// assert_eq!(j_a, j_b);
726+
///
690727
/// let mut map = BTreeMap::new();
691-
/// map.insert(1, "a");
692-
/// assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
693-
/// assert_eq!(map.get_key_value(&2), None);
728+
/// map.insert(j_a, "Paris");
729+
/// assert_eq!(map.get_key_value(&j_a), Some((&j_a, &"Paris")));
730+
/// assert_eq!(map.get_key_value(&j_b), Some((&j_a, &"Paris"))); // the notable case
731+
/// assert_eq!(map.get_key_value(&p), None);
694732
/// ```
695733
#[stable(feature = "map_get_key_value", since = "1.40.0")]
696734
pub fn get_key_value<Q: ?Sized>(&self, k: &Q) -> Option<(&K, &V)>

library/core/src/panic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ pub macro const_panic {
215215
#[noinline]
216216
if const #[track_caller] #[inline] { // Inline this, to prevent codegen
217217
$crate::panic!($const_msg)
218-
} else #[track_caller] { // Do not inline this, it makes perf worse
218+
} else #[track_caller] #[cfg_attr(bootstrap, inline)] { // Do not inline this, it makes perf worse
219219
$crate::panic!($runtime_msg)
220220
}
221221
)

library/std/src/collections/hash/map.rs

+36-4
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,11 @@ where
880880
self.base.get(k)
881881
}
882882

883-
/// Returns the key-value pair corresponding to the supplied key.
883+
/// Returns the key-value pair corresponding to the supplied key. This is
884+
/// potentially useful:
885+
/// - for key types where non-identical keys can be considered equal;
886+
/// - for getting the `&K` stored key value from a borrowed `&Q` lookup key; or
887+
/// - for getting a reference to a key with the same lifetime as the collection.
884888
///
885889
/// The supplied key may be any borrowed form of the map's key type, but
886890
/// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
@@ -890,11 +894,39 @@ where
890894
///
891895
/// ```
892896
/// use std::collections::HashMap;
897+
/// use std::hash::{Hash, Hasher};
898+
///
899+
/// #[derive(Clone, Copy, Debug)]
900+
/// struct S {
901+
/// id: u32,
902+
/// # #[allow(unused)] // prevents a "field `name` is never read" error
903+
/// name: &'static str, // ignored by equality and hashing operations
904+
/// }
905+
///
906+
/// impl PartialEq for S {
907+
/// fn eq(&self, other: &S) -> bool {
908+
/// self.id == other.id
909+
/// }
910+
/// }
911+
///
912+
/// impl Eq for S {}
913+
///
914+
/// impl Hash for S {
915+
/// fn hash<H: Hasher>(&self, state: &mut H) {
916+
/// self.id.hash(state);
917+
/// }
918+
/// }
919+
///
920+
/// let j_a = S { id: 1, name: "Jessica" };
921+
/// let j_b = S { id: 1, name: "Jess" };
922+
/// let p = S { id: 2, name: "Paul" };
923+
/// assert_eq!(j_a, j_b);
893924
///
894925
/// let mut map = HashMap::new();
895-
/// map.insert(1, "a");
896-
/// assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
897-
/// assert_eq!(map.get_key_value(&2), None);
926+
/// map.insert(j_a, "Paris");
927+
/// assert_eq!(map.get_key_value(&j_a), Some((&j_a, &"Paris")));
928+
/// assert_eq!(map.get_key_value(&j_b), Some((&j_a, &"Paris"))); // the notable case
929+
/// assert_eq!(map.get_key_value(&p), None);
898930
/// ```
899931
#[inline]
900932
#[stable(feature = "map_get_key_value", since = "1.40.0")]

src/doc/nomicon

src/librustdoc/html/render/span_map.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub(crate) enum LinkFromSrc {
3636
/// It returns the `krate`, the source code files and the `span` correspondence map.
3737
///
3838
/// Note about the `span` correspondence map: the keys are actually `(lo, hi)` of `span`s. We don't
39-
/// need the `span` context later on, only their position, so instead of keep a whole `Span`, we
39+
/// need the `span` context later on, only their position, so instead of keeping a whole `Span`, we
4040
/// only keep the `lo` and `hi`.
4141
pub(crate) fn collect_spans_and_sources(
4242
tcx: TyCtxt<'_>,
@@ -45,9 +45,9 @@ pub(crate) fn collect_spans_and_sources(
4545
include_sources: bool,
4646
generate_link_to_definition: bool,
4747
) -> (FxIndexMap<PathBuf, String>, FxHashMap<Span, LinkFromSrc>) {
48-
let mut visitor = SpanMapVisitor { tcx, matches: FxHashMap::default() };
49-
5048
if include_sources {
49+
let mut visitor = SpanMapVisitor { tcx, matches: FxHashMap::default() };
50+
5151
if generate_link_to_definition {
5252
tcx.hir().walk_toplevel_module(&mut visitor);
5353
}
@@ -76,7 +76,22 @@ impl<'tcx> SpanMapVisitor<'tcx> {
7676
} else {
7777
LinkFromSrc::External(def_id)
7878
};
79-
self.matches.insert(path.span, link);
79+
// In case the path ends with generics, we remove them from the span.
80+
let span = path
81+
.segments
82+
.last()
83+
.map(|last| {
84+
// In `use` statements, the included item is not in the path segments.
85+
// However, it doesn't matter because you can't have generics on `use`
86+
// statements.
87+
if path.span.contains(last.ident.span) {
88+
path.span.with_hi(last.ident.span.hi())
89+
} else {
90+
path.span
91+
}
92+
})
93+
.unwrap_or(path.span);
94+
self.matches.insert(span, link);
8095
}
8196
Res::Local(_) => {
8297
if let Some(span) = self.tcx.hir().res_span(path.res) {

src/tools/miri/miri-script/Cargo.lock

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file is automatically @generated by Cargo.
22
# It is not intended for manual editing.
3-
version = 3
3+
version = 4
44

55
[[package]]
66
name = "anyhow"
@@ -521,15 +521,15 @@ checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904"
521521

522522
[[package]]
523523
name = "xshell"
524-
version = "0.2.6"
524+
version = "0.2.7"
525525
source = "registry+https://github.com/rust-lang/crates.io-index"
526-
checksum = "6db0ab86eae739efd1b054a8d3d16041914030ac4e01cd1dca0cf252fd8b6437"
526+
checksum = "9e7290c623014758632efe00737145b6867b66292c42167f2ec381eb566a373d"
527527
dependencies = [
528528
"xshell-macros",
529529
]
530530

531531
[[package]]
532532
name = "xshell-macros"
533-
version = "0.2.6"
533+
version = "0.2.7"
534534
source = "registry+https://github.com/rust-lang/crates.io-index"
535-
checksum = "9d422e8e38ec76e2f06ee439ccc765e9c6a9638b9e7c9f2e8255e4d41e8bd852"
535+
checksum = "32ac00cd3f8ec9c1d33fb3e7958a82df6989c42d747bd326c822b1d625283547"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This test ensures that paths with generics still get their link to their definition
2+
// correctly generated.
3+
4+
//@ compile-flags: -Zunstable-options --generate-link-to-definition
5+
#![crate_name = "foo"]
6+
7+
//@ has 'src/foo/link-on-path-with-generics.rs.html'
8+
9+
pub struct Soyo<T>(T);
10+
pub struct Saya;
11+
12+
//@ has - '//pre[@class="rust"]//a[@href="#9"]' 'Soyo'
13+
//@ has - '//pre[@class="rust"]//a[@href="#10"]' 'Saya'
14+
pub fn bar<T>(s: Soyo<T>, x: Saya) {}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Inspired by https://github.com/rust-lang/cargo/issues/14775
2+
3+
pub fn my_lib_func() {}
4+
5+
#[macro_export]
6+
macro_rules! my_lib_macro {
7+
() => {
8+
#[cfg(my_lib_cfg)]
9+
$crate::my_lib_func()
10+
};
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This test checks that we emit the `unexpected_cfgs` lint even in code
2+
// coming from an external macro.
3+
4+
//@ check-pass
5+
//@ no-auto-check-cfg
6+
//@ aux-crate: cfg_macro=cfg_macro.rs
7+
//@ compile-flags: --check-cfg=cfg()
8+
9+
fn main() {
10+
cfg_macro::my_lib_macro!();
11+
//~^ WARNING unexpected `cfg` condition name
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
warning: unexpected `cfg` condition name: `my_lib_cfg`
2+
--> $DIR/report-in-external-macros.rs:10:5
3+
|
4+
LL | cfg_macro::my_lib_macro!();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= 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`
8+
= help: to expect this configuration use `--check-cfg=cfg(my_lib_cfg)`
9+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
10+
= note: `#[warn(unexpected_cfgs)]` on by default
11+
= note: this warning originates in the macro `cfg_macro::my_lib_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
12+
13+
warning: 1 warning emitted
14+

tests/ui/diagnostic_namespace/deny_malformed_attribute.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@ reference: attributes.diagnostic.namespace.unknown-invalid-syntax
2+
13
#![deny(unknown_or_malformed_diagnostic_attributes)]
24

35
#[diagnostic::unknown_attribute]

tests/ui/diagnostic_namespace/deny_malformed_attribute.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: unknown diagnostic attribute
2-
--> $DIR/deny_malformed_attribute.rs:3:15
2+
--> $DIR/deny_malformed_attribute.rs:5:15
33
|
44
LL | #[diagnostic::unknown_attribute]
55
| ^^^^^^^^^^^^^^^^^
66
|
77
note: the lint level is defined here
8-
--> $DIR/deny_malformed_attribute.rs:1:9
8+
--> $DIR/deny_malformed_attribute.rs:3:9
99
|
1010
LL | #![deny(unknown_or_malformed_diagnostic_attributes)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

tests/ui/diagnostic_namespace/malformed_foreign_on_unimplemented.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//@ edition:2021
22
//@ aux-build:bad_on_unimplemented.rs
3+
//@ reference: attributes.diagnostic.on_unimplemented.syntax
34

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

0 commit comments

Comments
 (0)