Skip to content

Commit 03b17b1

Browse files
committed
Auto merge of #93762 - matthiaskrgr:rollup-vdjpfmz, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - #86497 (Add {floor,ceil}_char_boundary methods to str) - #92695 (Add `#[no_coverage]` tests for nested functions) - #93521 (Fix hover effects in sidebar) - #93568 (Include all contents of first line of scraped item in Rustdoc) - #93569 (rustdoc: correct unclosed HTML tags as generics) - #93672 (update comment wrt const param defaults) - #93715 (Fix horizontal trim for block doc comments) - #93721 (rustdoc: Special-case macro lookups less) - #93728 (Add in ValuePair::Term) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 2a8dbdb + 25ce315 commit 03b17b1

File tree

40 files changed

+748
-176
lines changed

40 files changed

+748
-176
lines changed

compiler/rustc_ast/src/util/comments.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol {
4343
if i != 0 || j != lines.len() { Some((i, j)) } else { None }
4444
}
4545

46-
fn get_horizontal_trim(lines: &[&str], kind: CommentKind) -> Option<usize> {
46+
fn get_horizontal_trim<'a>(lines: &'a [&str], kind: CommentKind) -> Option<String> {
4747
let mut i = usize::MAX;
4848
let mut first = true;
4949

5050
// In case we have doc comments like `/**` or `/*!`, we want to remove stars if they are
5151
// present. However, we first need to strip the empty lines so they don't get in the middle
5252
// when we try to compute the "horizontal trim".
5353
let lines = if kind == CommentKind::Block {
54-
let mut i = 0;
54+
// Whatever happens, we skip the first line.
55+
let mut i = if lines[0].trim_start().starts_with('*') { 0 } else { 1 };
5556
let mut j = lines.len();
5657

5758
while i < j && lines[i].trim().is_empty() {
@@ -84,7 +85,7 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol {
8485
return None;
8586
}
8687
}
87-
Some(i)
88+
if lines.is_empty() { None } else { Some(lines[0][..i].into()) }
8889
}
8990

9091
let data_s = data.as_str();
@@ -102,8 +103,13 @@ pub fn beautify_doc_string(data: Symbol, kind: CommentKind) -> Symbol {
102103
changes = true;
103104
// remove a "[ \t]*\*" block from each line, if possible
104105
for line in lines.iter_mut() {
105-
if horizontal + 1 < line.len() {
106-
*line = &line[horizontal + 1..];
106+
if let Some(tmp) = line.strip_prefix(&horizontal) {
107+
*line = tmp;
108+
if kind == CommentKind::Block
109+
&& (*line == "*" || line.starts_with("* ") || line.starts_with("**"))
110+
{
111+
*line = &line[1..];
112+
}
107113
}
108114
}
109115
}

compiler/rustc_ast/src/util/comments/tests.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn test_block_doc_comment_3() {
2424
create_default_session_globals_then(|| {
2525
let comment = "\n let a: *i32;\n *a = 5;\n";
2626
let stripped = beautify_doc_string(Symbol::intern(comment), CommentKind::Block);
27-
assert_eq!(stripped.as_str(), " let a: *i32;\n *a = 5;");
27+
assert_eq!(stripped.as_str(), "let a: *i32;\n*a = 5;");
2828
})
2929
}
3030

@@ -41,3 +41,21 @@ fn test_line_doc_comment() {
4141
assert_eq!(stripped.as_str(), "!test");
4242
})
4343
}
44+
45+
#[test]
46+
fn test_doc_blocks() {
47+
create_default_session_globals_then(|| {
48+
let stripped =
49+
beautify_doc_string(Symbol::intern(" # Returns\n *\n "), CommentKind::Block);
50+
assert_eq!(stripped.as_str(), " # Returns\n\n");
51+
52+
let stripped = beautify_doc_string(
53+
Symbol::intern("\n * # Returns\n *\n "),
54+
CommentKind::Block,
55+
);
56+
assert_eq!(stripped.as_str(), " # Returns\n\n");
57+
58+
let stripped = beautify_doc_string(Symbol::intern("\n * a\n "), CommentKind::Block);
59+
assert_eq!(stripped.as_str(), " a\n");
60+
})
61+
}

compiler/rustc_builtin_macros/src/deriving/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ fn inject_impl_of_structural_trait(
134134

135135
// Create the type of `self`.
136136
//
137-
// in addition, remove defaults from type params (impls cannot have them).
137+
// in addition, remove defaults from generic params (impls cannot have them).
138138
let self_params: Vec<_> = generics
139139
.params
140140
.iter_mut()

compiler/rustc_infer/src/infer/at.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,10 @@ impl<'tcx> ToTrace<'tcx> for Ty<'tcx> {
268268
a: Self,
269269
b: Self,
270270
) -> TypeTrace<'tcx> {
271-
TypeTrace { cause: cause.clone(), values: Types(ExpectedFound::new(a_is_expected, a, b)) }
271+
TypeTrace {
272+
cause: cause.clone(),
273+
values: Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
274+
}
272275
}
273276
}
274277

@@ -292,27 +295,22 @@ impl<'tcx> ToTrace<'tcx> for &'tcx Const<'tcx> {
292295
a: Self,
293296
b: Self,
294297
) -> TypeTrace<'tcx> {
295-
TypeTrace { cause: cause.clone(), values: Consts(ExpectedFound::new(a_is_expected, a, b)) }
298+
TypeTrace {
299+
cause: cause.clone(),
300+
values: Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
301+
}
296302
}
297303
}
298304

299305
impl<'tcx> ToTrace<'tcx> for ty::Term<'tcx> {
300306
fn to_trace(
301-
tcx: TyCtxt<'tcx>,
307+
_: TyCtxt<'tcx>,
302308
cause: &ObligationCause<'tcx>,
303309
a_is_expected: bool,
304310
a: Self,
305311
b: Self,
306312
) -> TypeTrace<'tcx> {
307-
match (a, b) {
308-
(ty::Term::Ty(a), ty::Term::Ty(b)) => {
309-
ToTrace::to_trace(tcx, cause, a_is_expected, a, b)
310-
}
311-
(ty::Term::Const(a), ty::Term::Const(b)) => {
312-
ToTrace::to_trace(tcx, cause, a_is_expected, a, b)
313-
}
314-
(_, _) => todo!(),
315-
}
313+
TypeTrace { cause: cause.clone(), values: Terms(ExpectedFound::new(a_is_expected, a, b)) }
316314
}
317315
}
318316

@@ -358,7 +356,7 @@ impl<'tcx> ToTrace<'tcx> for ty::ProjectionTy<'tcx> {
358356
let b_ty = tcx.mk_projection(b.item_def_id, b.substs);
359357
TypeTrace {
360358
cause: cause.clone(),
361-
values: Types(ExpectedFound::new(a_is_expected, a_ty, b_ty)),
359+
values: Terms(ExpectedFound::new(a_is_expected, a_ty.into(), b_ty.into())),
362360
}
363361
}
364362
}

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+23-20
Original file line numberDiff line numberDiff line change
@@ -1582,18 +1582,18 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15821582
None => (None, Mismatch::Fixed("type"), false),
15831583
Some(values) => {
15841584
let (is_simple_error, exp_found) = match values {
1585-
ValuePairs::Types(exp_found) => {
1586-
let is_simple_err =
1587-
exp_found.expected.is_simple_text() && exp_found.found.is_simple_text();
1588-
OpaqueTypesVisitor::visit_expected_found(
1589-
self.tcx,
1590-
exp_found.expected,
1591-
exp_found.found,
1592-
span,
1593-
)
1594-
.report(diag);
1585+
ValuePairs::Terms(infer::ExpectedFound {
1586+
expected: ty::Term::Ty(expected),
1587+
found: ty::Term::Ty(found),
1588+
}) => {
1589+
let is_simple_err = expected.is_simple_text() && found.is_simple_text();
1590+
OpaqueTypesVisitor::visit_expected_found(self.tcx, expected, found, span)
1591+
.report(diag);
15951592

1596-
(is_simple_err, Mismatch::Variable(exp_found))
1593+
(
1594+
is_simple_err,
1595+
Mismatch::Variable(infer::ExpectedFound { expected, found }),
1596+
)
15971597
}
15981598
ValuePairs::TraitRefs(_) => (false, Mismatch::Fixed("trait")),
15991599
_ => (false, Mismatch::Fixed("type")),
@@ -1624,7 +1624,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
16241624
};
16251625
if let Some((sp, msg)) = secondary_span {
16261626
if swap_secondary_and_primary {
1627-
let terr = if let Some(infer::ValuePairs::Types(infer::ExpectedFound {
1627+
let terr = if let Some(infer::ValuePairs::Terms(infer::ExpectedFound {
16281628
expected,
16291629
..
16301630
})) = values
@@ -2036,9 +2036,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20362036
}
20372037
FailureCode::Error0308(failure_str) => {
20382038
let mut err = struct_span_err!(self.tcx.sess, span, E0308, "{}", failure_str);
2039-
if let ValuePairs::Types(ty::error::ExpectedFound { expected, found }) =
2040-
trace.values
2041-
{
2039+
if let Some((expected, found)) = trace.values.ty() {
20422040
match (expected.kind(), found.kind()) {
20432041
(ty::Tuple(_), ty::Tuple(_)) => {}
20442042
// If a tuple of length one was expected and the found expression has
@@ -2148,9 +2146,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
21482146
values: ValuePairs<'tcx>,
21492147
) -> Option<(DiagnosticStyledString, DiagnosticStyledString)> {
21502148
match values {
2151-
infer::Types(exp_found) => self.expected_found_str_ty(exp_found),
21522149
infer::Regions(exp_found) => self.expected_found_str(exp_found),
2153-
infer::Consts(exp_found) => self.expected_found_str(exp_found),
2150+
infer::Terms(exp_found) => self.expected_found_str_term(exp_found),
21542151
infer::TraitRefs(exp_found) => {
21552152
let pretty_exp_found = ty::error::ExpectedFound {
21562153
expected: exp_found.expected.print_only_trait_path(),
@@ -2178,16 +2175,22 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
21782175
}
21792176
}
21802177

2181-
fn expected_found_str_ty(
2178+
fn expected_found_str_term(
21822179
&self,
2183-
exp_found: ty::error::ExpectedFound<Ty<'tcx>>,
2180+
exp_found: ty::error::ExpectedFound<ty::Term<'tcx>>,
21842181
) -> Option<(DiagnosticStyledString, DiagnosticStyledString)> {
21852182
let exp_found = self.resolve_vars_if_possible(exp_found);
21862183
if exp_found.references_error() {
21872184
return None;
21882185
}
21892186

2190-
Some(self.cmp(exp_found.expected, exp_found.found))
2187+
Some(match (exp_found.expected, exp_found.found) {
2188+
(ty::Term::Ty(expected), ty::Term::Ty(found)) => self.cmp(expected, found),
2189+
(expected, found) => (
2190+
DiagnosticStyledString::highlighted(expected.to_string()),
2191+
DiagnosticStyledString::highlighted(found.to_string()),
2192+
),
2193+
})
21912194
}
21922195

21932196
/// Returns a string of the form "expected `{}`, found `{}`".

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/trait_impl_difference.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
44
use crate::infer::lexical_region_resolve::RegionResolutionError;
5-
use crate::infer::{SubregionOrigin, Subtype, ValuePairs};
5+
use crate::infer::{SubregionOrigin, Subtype};
66
use crate::traits::ObligationCauseCode::CompareImplMethodObligation;
77
use rustc_errors::ErrorReported;
88
use rustc_hir as hir;
@@ -34,16 +34,16 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
3434
{
3535
if let (&Subtype(ref sup_trace), &Subtype(ref sub_trace)) = (&sup_origin, &sub_origin) {
3636
if let (
37-
ValuePairs::Types(sub_expected_found),
38-
ValuePairs::Types(sup_expected_found),
37+
sub_expected_found @ Some((sub_expected, sub_found)),
38+
sup_expected_found @ Some(_),
3939
CompareImplMethodObligation { trait_item_def_id, .. },
40-
) = (&sub_trace.values, &sup_trace.values, sub_trace.cause.code())
40+
) = (&sub_trace.values.ty(), &sup_trace.values.ty(), sub_trace.cause.code())
4141
{
4242
if sup_expected_found == sub_expected_found {
4343
self.emit_err(
4444
var_origin.span(),
45-
sub_expected_found.expected,
46-
sub_expected_found.found,
45+
sub_expected,
46+
sub_found,
4747
*trait_item_def_id,
4848
);
4949
return Some(ErrorReported);

compiler/rustc_infer/src/infer/mod.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,26 @@ pub struct InferCtxt<'a, 'tcx> {
366366
/// See the `error_reporting` module for more details.
367367
#[derive(Clone, Copy, Debug, PartialEq, Eq, TypeFoldable)]
368368
pub enum ValuePairs<'tcx> {
369-
Types(ExpectedFound<Ty<'tcx>>),
370369
Regions(ExpectedFound<ty::Region<'tcx>>),
371-
Consts(ExpectedFound<&'tcx ty::Const<'tcx>>),
370+
Terms(ExpectedFound<ty::Term<'tcx>>),
372371
TraitRefs(ExpectedFound<ty::TraitRef<'tcx>>),
373372
PolyTraitRefs(ExpectedFound<ty::PolyTraitRef<'tcx>>),
374373
}
375374

375+
impl<'tcx> ValuePairs<'tcx> {
376+
pub fn ty(&self) -> Option<(Ty<'tcx>, Ty<'tcx>)> {
377+
if let ValuePairs::Terms(ExpectedFound {
378+
expected: ty::Term::Ty(expected),
379+
found: ty::Term::Ty(found),
380+
}) = self
381+
{
382+
Some((expected, found))
383+
} else {
384+
None
385+
}
386+
}
387+
}
388+
376389
/// The trace designates the path through inference that we took to
377390
/// encounter an error or subtyping constraint.
378391
///
@@ -1817,7 +1830,10 @@ impl<'tcx> TypeTrace<'tcx> {
18171830
a: Ty<'tcx>,
18181831
b: Ty<'tcx>,
18191832
) -> TypeTrace<'tcx> {
1820-
TypeTrace { cause: cause.clone(), values: Types(ExpectedFound::new(a_is_expected, a, b)) }
1833+
TypeTrace {
1834+
cause: cause.clone(),
1835+
values: Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
1836+
}
18211837
}
18221838

18231839
pub fn consts(
@@ -1826,7 +1842,10 @@ impl<'tcx> TypeTrace<'tcx> {
18261842
a: &'tcx ty::Const<'tcx>,
18271843
b: &'tcx ty::Const<'tcx>,
18281844
) -> TypeTrace<'tcx> {
1829-
TypeTrace { cause: cause.clone(), values: Consts(ExpectedFound::new(a_is_expected, a, b)) }
1845+
TypeTrace {
1846+
cause: cause.clone(),
1847+
values: Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())),
1848+
}
18301849
}
18311850
}
18321851

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+5-20
Original file line numberDiff line numberDiff line change
@@ -1382,26 +1382,11 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
13821382
normalized_ty,
13831383
data.term,
13841384
) {
1385-
values = Some(match (normalized_ty, data.term) {
1386-
(ty::Term::Ty(normalized_ty), ty::Term::Ty(ty)) => {
1387-
infer::ValuePairs::Types(ExpectedFound::new(
1388-
is_normalized_ty_expected,
1389-
normalized_ty,
1390-
ty,
1391-
))
1392-
}
1393-
(ty::Term::Const(normalized_ct), ty::Term::Const(ct)) => {
1394-
infer::ValuePairs::Consts(ExpectedFound::new(
1395-
is_normalized_ty_expected,
1396-
normalized_ct,
1397-
ct,
1398-
))
1399-
}
1400-
(_, _) => span_bug!(
1401-
obligation.cause.span,
1402-
"found const or type where other expected"
1403-
),
1404-
});
1385+
values = Some(infer::ValuePairs::Terms(ExpectedFound::new(
1386+
is_normalized_ty_expected,
1387+
normalized_ty,
1388+
data.term,
1389+
)));
14051390
err_buf = error;
14061391
err = &err_buf;
14071392
}

compiler/rustc_typeck/src/check/compare_method.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,9 @@ fn compare_predicate_entailment<'tcx>(
377377
&mut diag,
378378
&cause,
379379
trait_err_span.map(|sp| (sp, "type in trait".to_owned())),
380-
Some(infer::ValuePairs::Types(ExpectedFound {
381-
expected: trait_fty,
382-
found: impl_fty,
380+
Some(infer::ValuePairs::Terms(ExpectedFound {
381+
expected: trait_fty.into(),
382+
found: impl_fty.into(),
383383
})),
384384
&terr,
385385
false,
@@ -1068,9 +1068,9 @@ crate fn compare_const_impl<'tcx>(
10681068
&mut diag,
10691069
&cause,
10701070
trait_c_span.map(|span| (span, "type in trait".to_owned())),
1071-
Some(infer::ValuePairs::Types(ExpectedFound {
1072-
expected: trait_ty,
1073-
found: impl_ty,
1071+
Some(infer::ValuePairs::Terms(ExpectedFound {
1072+
expected: trait_ty.into(),
1073+
found: impl_ty.into(),
10741074
})),
10751075
&terr,
10761076
false,

library/alloc/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#![feature(binary_heap_as_slice)]
3030
#![feature(inplace_iteration)]
3131
#![feature(iter_advance_by)]
32+
#![feature(round_char_boundary)]
3233
#![feature(slice_group_by)]
3334
#![feature(slice_partition_dedup)]
3435
#![feature(string_remove_matches)]

0 commit comments

Comments
 (0)