Skip to content

Commit 38a76f3

Browse files
committed
Auto merge of rust-lang#106984 - Dylan-DPC:rollup-xce8263, r=Dylan-DPC
Rollup of 5 pull requests Successful merges: - rust-lang#101698 (Constify `TypeId` ordering impls) - rust-lang#106148 (Fix unused_parens issue for higher ranked function pointers) - rust-lang#106922 (Avoid unsafe code in `to_ascii_[lower/upper]case()`) - rust-lang#106951 (Remove ineffective run of SimplifyConstCondition) - rust-lang#106962 (Fix use suggestion span) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents f34cc65 + e6e7c39 commit 38a76f3

37 files changed

+310
-106
lines changed

compiler/rustc_lint/src/early.rs

+2
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
248248
}
249249

250250
fn visit_where_predicate(&mut self, p: &'a ast::WherePredicate) {
251+
lint_callback!(self, enter_where_predicate, p);
251252
ast_visit::walk_where_predicate(self, p);
253+
lint_callback!(self, exit_where_predicate, p);
252254
}
253255

254256
fn visit_poly_trait_ref(&mut self, t: &'a ast::PolyTraitRef) {

compiler/rustc_lint/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ early_lint_methods!(
145145
[
146146
pub BuiltinCombinedEarlyLintPass,
147147
[
148-
UnusedParens: UnusedParens,
148+
UnusedParens: UnusedParens::new(),
149149
UnusedBraces: UnusedBraces,
150150
UnusedImportBraces: UnusedImportBraces,
151151
UnsafeCode: UnsafeCode,

compiler/rustc_lint/src/passes.rs

+3
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ macro_rules! early_lint_methods {
171171

172172
/// Counterpart to `enter_lint_attrs`.
173173
fn exit_lint_attrs(a: &[ast::Attribute]);
174+
175+
fn enter_where_predicate(a: &ast::WherePredicate);
176+
fn exit_where_predicate(a: &ast::WherePredicate);
174177
]);
175178
)
176179
}

compiler/rustc_lint/src/unused.rs

+55-23
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,17 @@ declare_lint! {
824824
"`if`, `match`, `while` and `return` do not need parentheses"
825825
}
826826

827-
declare_lint_pass!(UnusedParens => [UNUSED_PARENS]);
827+
pub struct UnusedParens {
828+
with_self_ty_parens: bool,
829+
}
830+
831+
impl UnusedParens {
832+
pub fn new() -> Self {
833+
Self { with_self_ty_parens: false }
834+
}
835+
}
836+
837+
impl_lint_pass!(UnusedParens => [UNUSED_PARENS]);
828838

829839
impl UnusedDelimLint for UnusedParens {
830840
const DELIM_STR: &'static str = "parentheses";
@@ -999,36 +1009,58 @@ impl EarlyLintPass for UnusedParens {
9991009
}
10001010

10011011
fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
1002-
if let ast::TyKind::Paren(r) = &ty.kind {
1003-
match &r.kind {
1004-
ast::TyKind::TraitObject(..) => {}
1005-
ast::TyKind::BareFn(b) if b.generic_params.len() > 0 => {}
1006-
ast::TyKind::ImplTrait(_, bounds) if bounds.len() > 1 => {}
1007-
ast::TyKind::Array(_, len) => {
1008-
self.check_unused_delims_expr(
1009-
cx,
1010-
&len.value,
1011-
UnusedDelimsCtx::ArrayLenExpr,
1012-
false,
1013-
None,
1014-
None,
1015-
);
1016-
}
1017-
_ => {
1018-
let spans = if let Some(r) = r.span.find_ancestor_inside(ty.span) {
1019-
Some((ty.span.with_hi(r.lo()), ty.span.with_lo(r.hi())))
1020-
} else {
1021-
None
1022-
};
1023-
self.emit_unused_delims(cx, ty.span, spans, "type", (false, false));
1012+
match &ty.kind {
1013+
ast::TyKind::Array(_, len) => {
1014+
self.check_unused_delims_expr(
1015+
cx,
1016+
&len.value,
1017+
UnusedDelimsCtx::ArrayLenExpr,
1018+
false,
1019+
None,
1020+
None,
1021+
);
1022+
}
1023+
ast::TyKind::Paren(r) => {
1024+
match &r.kind {
1025+
ast::TyKind::TraitObject(..) => {}
1026+
ast::TyKind::BareFn(b)
1027+
if self.with_self_ty_parens && b.generic_params.len() > 0 => {}
1028+
ast::TyKind::ImplTrait(_, bounds) if bounds.len() > 1 => {}
1029+
_ => {
1030+
let spans = if let Some(r) = r.span.find_ancestor_inside(ty.span) {
1031+
Some((ty.span.with_hi(r.lo()), ty.span.with_lo(r.hi())))
1032+
} else {
1033+
None
1034+
};
1035+
self.emit_unused_delims(cx, ty.span, spans, "type", (false, false));
1036+
}
10241037
}
1038+
self.with_self_ty_parens = false;
10251039
}
1040+
_ => {}
10261041
}
10271042
}
10281043

10291044
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
10301045
<Self as UnusedDelimLint>::check_item(self, cx, item)
10311046
}
1047+
1048+
fn enter_where_predicate(&mut self, _: &EarlyContext<'_>, pred: &ast::WherePredicate) {
1049+
use rustc_ast::{WhereBoundPredicate, WherePredicate};
1050+
if let WherePredicate::BoundPredicate(WhereBoundPredicate {
1051+
bounded_ty,
1052+
bound_generic_params,
1053+
..
1054+
}) = pred &&
1055+
let ast::TyKind::Paren(_) = &bounded_ty.kind &&
1056+
bound_generic_params.is_empty() {
1057+
self.with_self_ty_parens = true;
1058+
}
1059+
}
1060+
1061+
fn exit_where_predicate(&mut self, _: &EarlyContext<'_>, _: &ast::WherePredicate) {
1062+
assert!(!self.with_self_ty_parens);
1063+
}
10321064
}
10331065

10341066
declare_lint! {

compiler/rustc_mir_transform/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,6 @@ fn run_analysis_to_runtime_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>
487487
fn run_analysis_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
488488
let passes: &[&dyn MirPass<'tcx>] = &[
489489
&cleanup_post_borrowck::CleanupPostBorrowck,
490-
&simplify_branches::SimplifyConstCondition::new("initial"),
491490
&remove_noop_landing_pads::RemoveNoopLandingPads,
492491
&simplify::SimplifyCfg::new("early-opt"),
493492
&deref_separator::Derefer,

compiler/rustc_resolve/src/diagnostics.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use rustc_ast::visit::{self, Visitor};
55
use rustc_ast::{self as ast, Crate, ItemKind, ModKind, NodeId, Path, CRATE_NODE_ID};
66
use rustc_ast_pretty::pprust;
77
use rustc_data_structures::fx::FxHashSet;
8-
use rustc_errors::struct_span_err;
98
use rustc_errors::{
109
pluralize, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan,
1110
};
11+
use rustc_errors::{struct_span_err, SuggestionStyle};
1212
use rustc_feature::BUILTIN_ATTRIBUTES;
1313
use rustc_hir::def::Namespace::{self, *};
1414
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind, PerNS};
@@ -2418,7 +2418,7 @@ fn show_candidates(
24182418
}
24192419

24202420
if let Some(span) = use_placement_span {
2421-
let add_use = match mode {
2421+
let (add_use, trailing) = match mode {
24222422
DiagnosticMode::Pattern => {
24232423
err.span_suggestions(
24242424
span,
@@ -2428,21 +2428,23 @@ fn show_candidates(
24282428
);
24292429
return;
24302430
}
2431-
DiagnosticMode::Import => "",
2432-
DiagnosticMode::Normal => "use ",
2431+
DiagnosticMode::Import => ("", ""),
2432+
DiagnosticMode::Normal => ("use ", ";\n"),
24332433
};
24342434
for candidate in &mut accessible_path_strings {
24352435
// produce an additional newline to separate the new use statement
24362436
// from the directly following item.
2437-
let additional_newline = if let FoundUse::Yes = found_use { "" } else { "\n" };
2438-
candidate.0 = format!("{add_use}{}{append};\n{additional_newline}", &candidate.0);
2437+
let additional_newline = if let FoundUse::No = found_use && let DiagnosticMode::Normal = mode { "\n" } else { "" };
2438+
candidate.0 =
2439+
format!("{add_use}{}{append}{trailing}{additional_newline}", &candidate.0);
24392440
}
24402441

2441-
err.span_suggestions(
2442+
err.span_suggestions_with_style(
24422443
span,
24432444
&msg,
24442445
accessible_path_strings.into_iter().map(|a| a.0),
24452446
Applicability::MaybeIncorrect,
2447+
SuggestionStyle::ShowAlways,
24462448
);
24472449
if let [first, .., last] = &path[..] {
24482450
let sp = first.ident.span.until(last.ident.span);
@@ -2463,7 +2465,7 @@ fn show_candidates(
24632465
msg.push_str(&candidate.0);
24642466
}
24652467

2466-
err.note(&msg);
2468+
err.help(&msg);
24672469
}
24682470
} else if !matches!(mode, DiagnosticMode::Import) {
24692471
assert!(!inaccessible_path_strings.is_empty());

library/alloc/src/str.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,9 @@ impl str {
559559
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
560560
#[inline]
561561
pub fn to_ascii_uppercase(&self) -> String {
562-
let mut bytes = self.as_bytes().to_vec();
563-
bytes.make_ascii_uppercase();
564-
// make_ascii_uppercase() preserves the UTF-8 invariant.
565-
unsafe { String::from_utf8_unchecked(bytes) }
562+
let mut s = self.to_owned();
563+
s.make_ascii_uppercase();
564+
s
566565
}
567566

568567
/// Returns a copy of this string where each character is mapped to its
@@ -592,10 +591,9 @@ impl str {
592591
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
593592
#[inline]
594593
pub fn to_ascii_lowercase(&self) -> String {
595-
let mut bytes = self.as_bytes().to_vec();
596-
bytes.make_ascii_lowercase();
597-
// make_ascii_lowercase() preserves the UTF-8 invariant.
598-
unsafe { String::from_utf8_unchecked(bytes) }
594+
let mut s = self.to_owned();
595+
s.make_ascii_lowercase();
596+
s
599597
}
600598
}
601599

library/core/src/any.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,8 @@ impl dyn Any + Send + Sync {
662662
/// While `TypeId` implements `Hash`, `PartialOrd`, and `Ord`, it is worth
663663
/// noting that the hashes and ordering will vary between Rust releases. Beware
664664
/// of relying on them inside of your code!
665-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
665+
#[derive(Clone, Copy, Debug, Hash, Eq)]
666+
#[derive_const(PartialEq, PartialOrd, Ord)]
666667
#[stable(feature = "rust1", since = "1.0.0")]
667668
pub struct TypeId {
668669
t: u64,

library/std/src/io/error/repr_bitpacked.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,10 @@ static_assert!((TAG_MASK + 1).is_power_of_two());
374374
static_assert!(align_of::<SimpleMessage>() >= TAG_MASK + 1);
375375
static_assert!(align_of::<Custom>() >= TAG_MASK + 1);
376376

377-
static_assert!(@usize_eq: (TAG_MASK & TAG_SIMPLE_MESSAGE), TAG_SIMPLE_MESSAGE);
378-
static_assert!(@usize_eq: (TAG_MASK & TAG_CUSTOM), TAG_CUSTOM);
379-
static_assert!(@usize_eq: (TAG_MASK & TAG_OS), TAG_OS);
380-
static_assert!(@usize_eq: (TAG_MASK & TAG_SIMPLE), TAG_SIMPLE);
377+
static_assert!(@usize_eq: TAG_MASK & TAG_SIMPLE_MESSAGE, TAG_SIMPLE_MESSAGE);
378+
static_assert!(@usize_eq: TAG_MASK & TAG_CUSTOM, TAG_CUSTOM);
379+
static_assert!(@usize_eq: TAG_MASK & TAG_OS, TAG_OS);
380+
static_assert!(@usize_eq: TAG_MASK & TAG_SIMPLE, TAG_SIMPLE);
381381

382382
// This is obviously true (`TAG_CUSTOM` is `0b01`), but in `Repr::new_custom` we
383383
// offset a pointer by this value, and expect it to both be within the same

tests/ui/const-generics/issues/issue-90318.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ impl True for If<true> {}
1212
fn consume<T: 'static>(_val: T)
1313
where
1414
If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
15-
//~^ ERROR: can't compare
15+
//~^ overly complex generic constant
1616
{
1717
}
1818

1919
fn test<T: 'static>()
2020
where
2121
If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
22-
//~^ ERROR: can't compare
22+
//~^ overly complex generic constant
2323
{
2424
}
2525

Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1-
error[E0277]: can't compare `TypeId` with `_` in const contexts
2-
--> $DIR/issue-90318.rs:14:28
1+
error: overly complex generic constant
2+
--> $DIR/issue-90318.rs:14:8
33
|
44
LL | If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
5-
| ^^ no implementation for `TypeId == _`
5+
| ^^-----------------^^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| borrowing is not supported in generic constants
68
|
7-
= help: the trait `~const PartialEq<_>` is not implemented for `TypeId`
8-
note: the trait `PartialEq<_>` is implemented for `TypeId`, but that implementation is not `const`
9-
--> $DIR/issue-90318.rs:14:28
10-
|
11-
LL | If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
12-
| ^^
9+
= help: consider moving this anonymous constant into a `const` function
10+
= note: this operation may be supported in the future
1311

14-
error[E0277]: can't compare `TypeId` with `_` in const contexts
15-
--> $DIR/issue-90318.rs:21:28
12+
error: overly complex generic constant
13+
--> $DIR/issue-90318.rs:21:8
1614
|
1715
LL | If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
18-
| ^^ no implementation for `TypeId == _`
16+
| ^^-----------------^^^^^^^^^^^^^^^^^^^^^^^^
17+
| |
18+
| borrowing is not supported in generic constants
1919
|
20-
= help: the trait `~const PartialEq<_>` is not implemented for `TypeId`
21-
note: the trait `PartialEq<_>` is implemented for `TypeId`, but that implementation is not `const`
22-
--> $DIR/issue-90318.rs:21:28
23-
|
24-
LL | If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
25-
| ^^
20+
= help: consider moving this anonymous constant into a `const` function
21+
= note: this operation may be supported in the future
2622

2723
error: aborting due to 2 previous errors
2824

29-
For more information about this error, try `rustc --explain E0277`.

tests/ui/consts/const_cmp_type_id.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// run-pass
2+
#![feature(const_type_id)]
3+
#![feature(const_trait_impl)]
4+
5+
use std::any::TypeId;
6+
7+
const fn main() {
8+
assert!(TypeId::of::<u8>() == TypeId::of::<u8>());
9+
assert!(TypeId::of::<()>() != TypeId::of::<u8>());
10+
const _A: bool = TypeId::of::<u8>() < TypeId::of::<u16>();
11+
// can't assert `_A` because it is not deterministic
12+
}

tests/ui/consts/issue-73976-monomorphic.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#![feature(const_type_id)]
99
#![feature(const_type_name)]
10+
#![feature(const_trait_impl)]
1011

1112
use std::any::{self, TypeId};
1213

@@ -17,7 +18,7 @@ impl<T: 'static> GetTypeId<T> {
1718
}
1819

1920
const fn check_type_id<T: 'static>() -> bool {
20-
matches!(GetTypeId::<T>::VALUE, GetTypeId::<usize>::VALUE)
21+
GetTypeId::<T>::VALUE == GetTypeId::<usize>::VALUE
2122
}
2223

2324
pub struct GetTypeNameLen<T>(T);

tests/ui/empty/empty-macro-use.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: cannot find macro `macro_two` in this scope
44
LL | macro_two!();
55
| ^^^^^^^^^
66
|
7-
= note: consider importing this macro:
7+
= help: consider importing this macro:
88
two_macros::macro_two
99

1010
error: aborting due to previous error

tests/ui/extenv/issue-55897.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ LL | use env;
3030
help: consider importing this module instead
3131
|
3232
LL | use std::env;
33-
| ~~~~~~~~~
33+
| ~~~~~~~~
3434

3535
error: cannot determine resolution for the macro `env`
3636
--> $DIR/issue-55897.rs:6:22

tests/ui/hygiene/globs.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ LL | n!(f);
5151
LL | n!(f);
5252
| ^ not found in this scope
5353
|
54-
= note: consider importing this function:
54+
= help: consider importing this function:
5555
foo::f
5656
= note: this error originates in the macro `n` (in Nightly builds, run with -Z macro-backtrace for more info)
5757

@@ -64,7 +64,7 @@ LL | n!(f);
6464
LL | f
6565
| ^ not found in this scope
6666
|
67-
= note: consider importing this function:
67+
= help: consider importing this function:
6868
foo::f
6969
= note: this error originates in the macro `n` (in Nightly builds, run with -Z macro-backtrace for more info)
7070

tests/ui/hygiene/no_implicit_prelude-2018.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: cannot find macro `print` in this scope
44
LL | print!();
55
| ^^^^^
66
|
7-
= note: consider importing this macro:
7+
= help: consider importing this macro:
88
std::print
99

1010
error: aborting due to previous error

0 commit comments

Comments
 (0)