Skip to content

Commit 63c748e

Browse files
committed
Auto merge of rust-lang#104481 - matthiaskrgr:rollup-hf8rev0, r=matthiaskrgr
Rollup of 10 pull requests Successful merges: - rust-lang#103484 (Add `rust` to `let_underscore_lock` example) - rust-lang#103489 (Make `pointer::byte_offset_from` more generic) - rust-lang#104193 (Shift no characters when using raw string literals) - rust-lang#104348 (Respect visibility & stability of inherent associated types) - rust-lang#104401 (avoid memory leak in mpsc test) - rust-lang#104419 (Fix test/ui/issues/issue-30490.rs) - rust-lang#104424 (rustdoc: remove no-op CSS `.popover { font-size: 1rem }`) - rust-lang#104425 (rustdoc: remove no-op CSS `.main-header { justify-content }`) - rust-lang#104450 (Fuchsia test suite script fix) - rust-lang#104471 (Update PROBLEMATIC_CONSTS in style.rs) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents e702534 + 8bbecb9 commit 63c748e

File tree

25 files changed

+279
-143
lines changed

25 files changed

+279
-143
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

+46-37
Original file line numberDiff line numberDiff line change
@@ -1917,17 +1917,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
19171917
}
19181918

19191919
// see if we can satisfy using an inherent associated type
1920-
for impl_ in tcx.inherent_impls(adt_def.did()) {
1921-
let assoc_ty = tcx.associated_items(impl_).find_by_name_and_kind(
1922-
tcx,
1923-
assoc_ident,
1924-
ty::AssocKind::Type,
1925-
*impl_,
1926-
);
1927-
if let Some(assoc_ty) = assoc_ty {
1928-
let ty = tcx.type_of(assoc_ty.def_id);
1929-
return Ok((ty, DefKind::AssocTy, assoc_ty.def_id));
1930-
}
1920+
for &impl_ in tcx.inherent_impls(adt_def.did()) {
1921+
let Some(assoc_ty_did) = self.lookup_assoc_ty(assoc_ident, hir_ref_id, span, impl_) else {
1922+
continue;
1923+
};
1924+
// FIXME(inherent_associated_types): This does not substitute parameters.
1925+
let ty = tcx.type_of(assoc_ty_did);
1926+
return Ok((ty, DefKind::AssocTy, assoc_ty_did));
19311927
}
19321928
}
19331929

@@ -2014,37 +2010,17 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
20142010
};
20152011

20162012
let trait_did = bound.def_id();
2017-
let (assoc_ident, def_scope) =
2018-
tcx.adjust_ident_and_get_scope(assoc_ident, trait_did, hir_ref_id);
2019-
2020-
// We have already adjusted the item name above, so compare with `ident.normalize_to_macros_2_0()` instead
2021-
// of calling `filter_by_name_and_kind`.
2022-
let item = tcx.associated_items(trait_did).in_definition_order().find(|i| {
2023-
i.kind.namespace() == Namespace::TypeNS
2024-
&& i.ident(tcx).normalize_to_macros_2_0() == assoc_ident
2025-
});
2026-
// Assume that if it's not matched, there must be a const defined with the same name
2027-
// but it was used in a type position.
2028-
let Some(item) = item else {
2013+
let Some(assoc_ty_did) = self.lookup_assoc_ty(assoc_ident, hir_ref_id, span, trait_did) else {
2014+
// Assume that if it's not matched, there must be a const defined with the same name
2015+
// but it was used in a type position.
20292016
let msg = format!("found associated const `{assoc_ident}` when type was expected");
20302017
let guar = tcx.sess.struct_span_err(span, &msg).emit();
20312018
return Err(guar);
20322019
};
20332020

2034-
let ty = self.projected_ty_from_poly_trait_ref(span, item.def_id, assoc_segment, bound);
2021+
let ty = self.projected_ty_from_poly_trait_ref(span, assoc_ty_did, assoc_segment, bound);
20352022
let ty = self.normalize_ty(span, ty);
20362023

2037-
let kind = DefKind::AssocTy;
2038-
if !item.visibility(tcx).is_accessible_from(def_scope, tcx) {
2039-
let kind = kind.descr(item.def_id);
2040-
let msg = format!("{} `{}` is private", kind, assoc_ident);
2041-
tcx.sess
2042-
.struct_span_err(span, &msg)
2043-
.span_label(span, &format!("private {}", kind))
2044-
.emit();
2045-
}
2046-
tcx.check_stability(item.def_id, Some(hir_ref_id), span, None);
2047-
20482024
if let Some(variant_def_id) = variant_resolution {
20492025
tcx.struct_span_lint_hir(
20502026
AMBIGUOUS_ASSOCIATED_ITEMS,
@@ -2063,7 +2039,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
20632039
};
20642040

20652041
could_refer_to(DefKind::Variant, variant_def_id, "");
2066-
could_refer_to(kind, item.def_id, " also");
2042+
could_refer_to(DefKind::AssocTy, assoc_ty_did, " also");
20672043

20682044
lint.span_suggestion(
20692045
span,
@@ -2076,7 +2052,40 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
20762052
},
20772053
);
20782054
}
2079-
Ok((ty, kind, item.def_id))
2055+
Ok((ty, DefKind::AssocTy, assoc_ty_did))
2056+
}
2057+
2058+
fn lookup_assoc_ty(
2059+
&self,
2060+
ident: Ident,
2061+
block: hir::HirId,
2062+
span: Span,
2063+
scope: DefId,
2064+
) -> Option<DefId> {
2065+
let tcx = self.tcx();
2066+
let (ident, def_scope) = tcx.adjust_ident_and_get_scope(ident, scope, block);
2067+
2068+
// We have already adjusted the item name above, so compare with `ident.normalize_to_macros_2_0()` instead
2069+
// of calling `find_by_name_and_kind`.
2070+
let item = tcx.associated_items(scope).in_definition_order().find(|i| {
2071+
i.kind.namespace() == Namespace::TypeNS
2072+
&& i.ident(tcx).normalize_to_macros_2_0() == ident
2073+
})?;
2074+
2075+
let kind = DefKind::AssocTy;
2076+
if !item.visibility(tcx).is_accessible_from(def_scope, tcx) {
2077+
let kind = kind.descr(item.def_id);
2078+
let msg = format!("{kind} `{ident}` is private");
2079+
let def_span = self.tcx().def_span(item.def_id);
2080+
tcx.sess
2081+
.struct_span_err_with_code(span, &msg, rustc_errors::error_code!(E0624))
2082+
.span_label(span, &format!("private {kind}"))
2083+
.span_label(def_span, &format!("{kind} defined here"))
2084+
.emit();
2085+
}
2086+
tcx.check_stability(item.def_id, Some(block), span, None);
2087+
2088+
Some(item.def_id)
20802089
}
20812090

20822091
fn qpath_to_ty(

compiler/rustc_lint/src/let_underscore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ declare_lint! {
5757
/// of at end of scope, which is typically incorrect.
5858
///
5959
/// ### Example
60-
/// ```compile_fail
60+
/// ```rust,compile_fail
6161
/// use std::sync::{Arc, Mutex};
6262
/// use std::thread;
6363
/// let data = Arc::new(Mutex::new(0));

compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,23 @@ declare_lint! {
2626
///
2727
/// ### Example
2828
///
29-
/// ```
29+
/// ```rust
30+
/// trait Duh {}
31+
///
32+
/// impl Duh for i32 {}
33+
///
3034
/// trait Trait {
31-
/// type Assoc: Send;
35+
/// type Assoc: Duh;
3236
/// }
3337
///
3438
/// struct Struct;
3539
///
36-
/// impl Trait for Struct {
37-
/// type Assoc = i32;
40+
/// impl<F: Duh> Trait for F {
41+
/// type Assoc = F;
3842
/// }
3943
///
4044
/// fn test() -> impl Trait<Assoc = impl Sized> {
41-
/// Struct
45+
/// 42
4246
/// }
4347
/// ```
4448
///

compiler/rustc_lint_defs/src/builtin.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ declare_lint! {
605605
///
606606
/// ### Example
607607
///
608-
/// ```
608+
/// ```rust
609609
/// #[warn(unused_tuple_struct_fields)]
610610
/// struct S(i32, i32, i32);
611611
/// let s = S(1, 2, 3);
@@ -1154,7 +1154,7 @@ declare_lint! {
11541154
///
11551155
/// ### Example
11561156
///
1157-
/// ```compile_fail
1157+
/// ```rust,compile_fail
11581158
/// #[repr(packed)]
11591159
/// pub struct Foo {
11601160
/// field1: u64,
@@ -2548,7 +2548,7 @@ declare_lint! {
25482548
///
25492549
/// ### Example
25502550
///
2551-
/// ```compile_fail
2551+
/// ```rust,compile_fail
25522552
/// # #![allow(unused)]
25532553
/// enum E {
25542554
/// A,
@@ -3918,7 +3918,7 @@ declare_lint! {
39183918
///
39193919
/// ### Example
39203920
///
3921-
/// ```
3921+
/// ```rust
39223922
/// #![allow(test_unstable_lint)]
39233923
/// ```
39243924
///

compiler/rustc_parse_format/src/lib.rs

+74-76
Original file line numberDiff line numberDiff line change
@@ -818,96 +818,94 @@ fn find_skips_from_snippet(
818818
_ => return (vec![], false),
819819
};
820820

821-
fn find_skips(snippet: &str, is_raw: bool) -> Vec<usize> {
822-
let mut s = snippet.char_indices();
823-
let mut skips = vec![];
824-
while let Some((pos, c)) = s.next() {
825-
match (c, s.clone().next()) {
826-
// skip whitespace and empty lines ending in '\\'
827-
('\\', Some((next_pos, '\n'))) if !is_raw => {
828-
skips.push(pos);
829-
skips.push(next_pos);
830-
let _ = s.next();
821+
if str_style.is_some() {
822+
return (vec![], true);
823+
}
831824

832-
while let Some((pos, c)) = s.clone().next() {
833-
if matches!(c, ' ' | '\n' | '\t') {
834-
skips.push(pos);
835-
let _ = s.next();
836-
} else {
837-
break;
838-
}
839-
}
840-
}
841-
('\\', Some((next_pos, 'n' | 't' | 'r' | '0' | '\\' | '\'' | '\"'))) => {
842-
skips.push(next_pos);
843-
let _ = s.next();
844-
}
845-
('\\', Some((_, 'x'))) if !is_raw => {
846-
for _ in 0..3 {
847-
// consume `\xAB` literal
848-
if let Some((pos, _)) = s.next() {
849-
skips.push(pos);
850-
} else {
851-
break;
852-
}
825+
let snippet = &snippet[1..snippet.len() - 1];
826+
827+
let mut s = snippet.char_indices();
828+
let mut skips = vec![];
829+
while let Some((pos, c)) = s.next() {
830+
match (c, s.clone().next()) {
831+
// skip whitespace and empty lines ending in '\\'
832+
('\\', Some((next_pos, '\n'))) => {
833+
skips.push(pos);
834+
skips.push(next_pos);
835+
let _ = s.next();
836+
837+
while let Some((pos, c)) = s.clone().next() {
838+
if matches!(c, ' ' | '\n' | '\t') {
839+
skips.push(pos);
840+
let _ = s.next();
841+
} else {
842+
break;
853843
}
854844
}
855-
('\\', Some((_, 'u'))) if !is_raw => {
845+
}
846+
('\\', Some((next_pos, 'n' | 't' | 'r' | '0' | '\\' | '\'' | '\"'))) => {
847+
skips.push(next_pos);
848+
let _ = s.next();
849+
}
850+
('\\', Some((_, 'x'))) => {
851+
for _ in 0..3 {
852+
// consume `\xAB` literal
856853
if let Some((pos, _)) = s.next() {
857854
skips.push(pos);
855+
} else {
856+
break;
858857
}
859-
if let Some((next_pos, next_c)) = s.next() {
860-
if next_c == '{' {
861-
// consume up to 6 hexanumeric chars
862-
let digits_len =
863-
s.clone().take(6).take_while(|(_, c)| c.is_digit(16)).count();
864-
865-
let len_utf8 = s
866-
.as_str()
867-
.get(..digits_len)
868-
.and_then(|digits| u32::from_str_radix(digits, 16).ok())
869-
.and_then(char::from_u32)
870-
.map_or(1, char::len_utf8);
871-
872-
// Skip the digits, for chars that encode to more than 1 utf-8 byte
873-
// exclude as many digits as it is greater than 1 byte
874-
//
875-
// So for a 3 byte character, exclude 2 digits
876-
let required_skips =
877-
digits_len.saturating_sub(len_utf8.saturating_sub(1));
878-
879-
// skip '{' and '}' also
880-
for pos in (next_pos..).take(required_skips + 2) {
881-
skips.push(pos)
882-
}
858+
}
859+
}
860+
('\\', Some((_, 'u'))) => {
861+
if let Some((pos, _)) = s.next() {
862+
skips.push(pos);
863+
}
864+
if let Some((next_pos, next_c)) = s.next() {
865+
if next_c == '{' {
866+
// consume up to 6 hexanumeric chars
867+
let digits_len =
868+
s.clone().take(6).take_while(|(_, c)| c.is_digit(16)).count();
869+
870+
let len_utf8 = s
871+
.as_str()
872+
.get(..digits_len)
873+
.and_then(|digits| u32::from_str_radix(digits, 16).ok())
874+
.and_then(char::from_u32)
875+
.map_or(1, char::len_utf8);
876+
877+
// Skip the digits, for chars that encode to more than 1 utf-8 byte
878+
// exclude as many digits as it is greater than 1 byte
879+
//
880+
// So for a 3 byte character, exclude 2 digits
881+
let required_skips = digits_len.saturating_sub(len_utf8.saturating_sub(1));
882+
883+
// skip '{' and '}' also
884+
for pos in (next_pos..).take(required_skips + 2) {
885+
skips.push(pos)
886+
}
883887

884-
s.nth(digits_len);
885-
} else if next_c.is_digit(16) {
886-
skips.push(next_pos);
887-
// We suggest adding `{` and `}` when appropriate, accept it here as if
888-
// it were correct
889-
let mut i = 0; // consume up to 6 hexanumeric chars
890-
while let (Some((next_pos, c)), _) = (s.next(), i < 6) {
891-
if c.is_digit(16) {
892-
skips.push(next_pos);
893-
} else {
894-
break;
895-
}
896-
i += 1;
888+
s.nth(digits_len);
889+
} else if next_c.is_digit(16) {
890+
skips.push(next_pos);
891+
// We suggest adding `{` and `}` when appropriate, accept it here as if
892+
// it were correct
893+
let mut i = 0; // consume up to 6 hexanumeric chars
894+
while let (Some((next_pos, c)), _) = (s.next(), i < 6) {
895+
if c.is_digit(16) {
896+
skips.push(next_pos);
897+
} else {
898+
break;
897899
}
900+
i += 1;
898901
}
899902
}
900903
}
901-
_ => {}
902904
}
905+
_ => {}
903906
}
904-
skips
905907
}
906-
907-
let r_start = str_style.map_or(0, |r| r + 1);
908-
let r_end = str_style.unwrap_or(0);
909-
let s = &snippet[r_start + 1..snippet.len() - r_end - 1];
910-
(find_skips(s, str_style.is_some()), true)
908+
(skips, true)
911909
}
912910

913911
#[cfg(test)]

library/core/src/ptr/const_ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ impl<T: ?Sized> *const T {
709709
#[unstable(feature = "pointer_byte_offsets", issue = "96283")]
710710
#[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")]
711711
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
712-
pub const unsafe fn byte_offset_from(self, origin: *const T) -> isize {
712+
pub const unsafe fn byte_offset_from<U: ?Sized>(self, origin: *const U) -> isize {
713713
// SAFETY: the caller must uphold the safety contract for `offset_from`.
714714
unsafe { self.cast::<u8>().offset_from(origin.cast::<u8>()) }
715715
}

library/core/src/ptr/mut_ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ impl<T: ?Sized> *mut T {
889889
#[unstable(feature = "pointer_byte_offsets", issue = "96283")]
890890
#[rustc_const_unstable(feature = "const_pointer_byte_offsets", issue = "96283")]
891891
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
892-
pub const unsafe fn byte_offset_from(self, origin: *const T) -> isize {
892+
pub const unsafe fn byte_offset_from<U: ?Sized>(self, origin: *const U) -> isize {
893893
// SAFETY: the caller must uphold the safety contract for `offset_from`.
894894
unsafe { self.cast::<u8>().offset_from(origin.cast::<u8>()) }
895895
}

library/std/src/sync/mpsc/tests.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -713,10 +713,11 @@ fn issue_39364() {
713713
let t = thread::spawn(move || {
714714
thread::sleep(Duration::from_millis(300));
715715
let _ = tx.clone();
716-
crate::mem::forget(tx);
716+
// Don't drop; hand back to caller.
717+
tx
717718
});
718719

719720
let _ = rx.recv_timeout(Duration::from_millis(500));
720-
t.join().unwrap();
721+
let _tx = t.join().unwrap(); // delay dropping until end of test
721722
let _ = rx.recv_timeout(Duration::from_millis(500));
722723
}

0 commit comments

Comments
 (0)