Skip to content

Commit 8ea62f6

Browse files
committed
Auto merge of #106588 - JohnTitor:rollup-4z80tjx, r=JohnTitor
Rollup of 8 pull requests Successful merges: - #103104 (Stabilize `main_separator_str`) - #106410 (Suggest `mut self: &mut Self` for `?Sized` impls) - #106457 (Adjust comments about pre-push.sh hook) - #106546 (jsondoclint: Check local items in `paths` are also in `index`.) - #106557 (Add some UI tests and reword error-code docs) - #106562 (Clarify examples for `VecDeque::get/get_mut`) - #106580 (remove unreachable error code `E0313`) - #106581 (Do not emit wrong E0308 suggestion for closure mismatch) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 2afe585 + 789ebdc commit 8ea62f6

File tree

22 files changed

+268
-94
lines changed

22 files changed

+268
-94
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -344,20 +344,25 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
344344
} else {
345345
err.span_help(source_info.span, "try removing `&mut` here");
346346
}
347-
} else if decl.mutability == Mutability::Not
348-
&& !matches!(
347+
} else if decl.mutability == Mutability::Not {
348+
if matches!(
349349
decl.local_info,
350350
Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::ImplicitSelf(
351351
hir::ImplicitSelfKind::MutRef
352-
))))
353-
)
354-
{
355-
err.span_suggestion_verbose(
356-
decl.source_info.span.shrink_to_lo(),
357-
"consider making the binding mutable",
358-
"mut ",
359-
Applicability::MachineApplicable,
360-
);
352+
),)))
353+
) {
354+
err.note(
355+
"as `Self` may be unsized, this call attempts to take `&mut &mut self`",
356+
);
357+
err.note("however, `&mut self` expands to `self: &mut Self`, therefore `self` cannot be borrowed mutably");
358+
} else {
359+
err.span_suggestion_verbose(
360+
decl.source_info.span.shrink_to_lo(),
361+
"consider making the binding mutable",
362+
"mut ",
363+
Applicability::MachineApplicable,
364+
);
365+
};
361366
}
362367
}
363368

compiler/rustc_error_codes/src/error_codes.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -579,8 +579,7 @@ E0791: include_str!("./error_codes/E0791.md"),
579579
// E0300, // unexpanded macro
580580
// E0304, // expected signed integer constant
581581
// E0305, // expected constant
582-
E0313, // lifetime of borrowed pointer outlives lifetime of captured
583-
// variable
582+
// E0313, // removed: found unreachable
584583
// E0314, // closure outlives stack frame
585584
// E0315, // cannot invoke closure outside of its lifetime
586585
// E0319, // trait impls for defaulted traits allowed just for structs/enums
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
A constant item was initialized with something that is not a constant
2-
expression.
1+
A non-`const` function was called in a `const` context.
32

43
Erroneous code example:
54

@@ -8,26 +7,20 @@ fn create_some() -> Option<u8> {
87
Some(1)
98
}
109
11-
const FOO: Option<u8> = create_some(); // error!
10+
// error: cannot call non-const fn `create_some` in constants
11+
const FOO: Option<u8> = create_some();
1212
```
1313

14-
The only functions that can be called in static or constant expressions are
15-
`const` functions, and struct/enum constructors.
14+
All functions used in a `const` context (constant or static expression) must
15+
be marked `const`.
1616

1717
To fix this error, you can declare `create_some` as a constant function:
1818

1919
```
20-
const fn create_some() -> Option<u8> { // declared as a const function
20+
// declared as a `const` function:
21+
const fn create_some() -> Option<u8> {
2122
Some(1)
2223
}
2324
24-
const FOO: Option<u8> = create_some(); // ok!
25-
26-
// These are also working:
27-
struct Bar {
28-
x: u8,
29-
}
30-
31-
const OTHER_FOO: Option<u8> = Some(1);
32-
const BAR: Bar = Bar {x: 1};
25+
const FOO: Option<u8> = create_some(); // no error!
3326
```

compiler/rustc_hir_typeck/src/demand.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13791379
}
13801380
}
13811381
// If we've reached our target type with just removing `&`, then just print now.
1382-
if steps == 0 {
1382+
if steps == 0 && !remove.trim().is_empty() {
13831383
return Some((
13841384
prefix_span,
13851385
format!("consider removing the `{}`", remove.trim()),
@@ -1438,6 +1438,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14381438
} else {
14391439
(prefix_span, format!("{}{}", prefix, "*".repeat(steps)))
14401440
};
1441+
if suggestion.trim().is_empty() {
1442+
return None;
1443+
}
14411444

14421445
return Some((
14431446
span,

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

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// ignore-tidy-filelength
21
//! Error Reporting Code for the inference engine
32
//!
43
//! Because of the way inference, and in particular region inference,

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

-37
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
2525
infer::Reborrow(span) => {
2626
RegionOriginNote::Plain { span, msg: fluent::infer_reborrow }.add_to_diagnostic(err)
2727
}
28-
infer::ReborrowUpvar(span, ref upvar_id) => {
29-
let var_name = self.tcx.hir().name(upvar_id.var_path.hir_id);
30-
RegionOriginNote::WithName {
31-
span,
32-
msg: fluent::infer_reborrow,
33-
name: &var_name.to_string(),
34-
continues: false,
35-
}
36-
.add_to_diagnostic(err);
37-
}
3828
infer::RelateObjectBound(span) => {
3929
RegionOriginNote::Plain { span, msg: fluent::infer_relate_object_bound }
4030
.add_to_diagnostic(err);
@@ -162,33 +152,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
162152
);
163153
err
164154
}
165-
infer::ReborrowUpvar(span, ref upvar_id) => {
166-
let var_name = self.tcx.hir().name(upvar_id.var_path.hir_id);
167-
let mut err = struct_span_err!(
168-
self.tcx.sess,
169-
span,
170-
E0313,
171-
"lifetime of borrowed pointer outlives lifetime of captured variable `{}`...",
172-
var_name
173-
);
174-
note_and_explain_region(
175-
self.tcx,
176-
&mut err,
177-
"...the borrowed pointer is valid for ",
178-
sub,
179-
"...",
180-
None,
181-
);
182-
note_and_explain_region(
183-
self.tcx,
184-
&mut err,
185-
&format!("...but `{}` is only valid for ", var_name),
186-
sup,
187-
"",
188-
None,
189-
);
190-
err
191-
}
192155
infer::RelateObjectBound(span) => {
193156
let mut err = struct_span_err!(
194157
self.tcx.sess,

compiler/rustc_infer/src/infer/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,6 @@ pub enum SubregionOrigin<'tcx> {
409409
/// Creating a pointer `b` to contents of another reference
410410
Reborrow(Span),
411411

412-
/// Creating a pointer `b` to contents of an upvar
413-
ReborrowUpvar(Span, ty::UpvarId),
414-
415412
/// Data with type `Ty<'tcx>` was borrowed
416413
DataBorrowed(Ty<'tcx>, Span),
417414

@@ -1954,7 +1951,6 @@ impl<'tcx> SubregionOrigin<'tcx> {
19541951
RelateParamBound(a, ..) => a,
19551952
RelateRegionParamBound(a) => a,
19561953
Reborrow(a) => a,
1957-
ReborrowUpvar(a, _) => a,
19581954
DataBorrowed(_, a) => a,
19591955
ReferenceOutlivesReferent(_, a) => a,
19601956
CompareImplItemObligation { span, .. } => span,

library/alloc/src/collections/vec_deque/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
636636
/// buf.push_back(3);
637637
/// buf.push_back(4);
638638
/// buf.push_back(5);
639+
/// buf.push_back(6);
639640
/// assert_eq!(buf.get(1), Some(&4));
640641
/// ```
641642
#[stable(feature = "rust1", since = "1.0.0")]
@@ -661,10 +662,11 @@ impl<T, A: Allocator> VecDeque<T, A> {
661662
/// buf.push_back(3);
662663
/// buf.push_back(4);
663664
/// buf.push_back(5);
665+
/// buf.push_back(6);
666+
/// assert_eq!(buf[1], 4);
664667
/// if let Some(elem) = buf.get_mut(1) {
665668
/// *elem = 7;
666669
/// }
667-
///
668670
/// assert_eq!(buf[1], 7);
669671
/// ```
670672
#[stable(feature = "rust1", since = "1.0.0")]

library/std/src/path.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ pub const MAIN_SEPARATOR: char = crate::sys::path::MAIN_SEP;
271271
/// The primary separator of path components for the current platform.
272272
///
273273
/// For example, `/` on Unix and `\` on Windows.
274-
#[unstable(feature = "main_separator_str", issue = "94071")]
274+
#[stable(feature = "main_separator_str", since = "CURRENT_RUSTC_VERSION")]
275275
pub const MAIN_SEPARATOR_STR: &str = crate::sys::path::MAIN_SEP_STR;
276276

277277
////////////////////////////////////////////////////////////////////////////////

src/bootstrap/setup.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ pub fn interactive_path() -> io::Result<Profile> {
351351
Ok(template)
352352
}
353353

354-
// install a git hook to automatically run tidy --bless, if they want
354+
// install a git hook to automatically run tidy, if they want
355355
fn install_git_hook_maybe(config: &Config) -> io::Result<()> {
356356
let git = t!(config.git().args(&["rev-parse", "--git-common-dir"]).output().map(|output| {
357357
assert!(output.status.success(), "failed to run `git`");
@@ -367,7 +367,7 @@ fn install_git_hook_maybe(config: &Config) -> io::Result<()> {
367367
println!();
368368
println!(
369369
"Rust's CI will automatically fail if it doesn't pass `tidy`, the internal tool for ensuring code quality.
370-
If you'd like, x.py can install a git hook for you that will automatically run `tidy --bless` before
370+
If you'd like, x.py can install a git hook for you that will automatically run `test tidy` before
371371
pushing your code to ensure your code is up to par. If you decide later that this behavior is
372372
undesirable, simply delete the `pre-push` file from .git/hooks."
373373
);

src/etc/pre-push.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22
#
3-
# Call `tidy --bless` before git push
3+
# Call `tidy` before git push
44
# Copy this script to .git/hooks to activate,
55
# and remove it from .git/hooks to deactivate.
66
#

src/test/ui/borrowck/issue-93078.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
trait Modify {
2+
fn modify(&mut self) ;
3+
}
4+
5+
impl<T> Modify for T {
6+
fn modify(&mut self) {}
7+
}
8+
9+
trait Foo {
10+
fn mute(&mut self) {
11+
self.modify(); //~ ERROR cannot borrow `self` as mutable
12+
}
13+
}
14+
15+
fn main() {}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable
2+
--> $DIR/issue-93078.rs:11:9
3+
|
4+
LL | self.modify();
5+
| ^^^^^^^^^^^^^ cannot borrow as mutable
6+
|
7+
= note: as `Self` may be unsized, this call attempts to take `&mut &mut self`
8+
= note: however, `&mut self` expands to `self: &mut Self`, therefore `self` cannot be borrowed mutably
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0596`.

src/test/ui/error-codes/E0013.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
static X: i32 = 42;
2+
const Y: i32 = X; //~ ERROR constants cannot refer to statics [E0013]
3+
4+
fn main() {}

src/test/ui/error-codes/E0013.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0013]: constants cannot refer to statics
2+
--> $DIR/E0013.rs:2:16
3+
|
4+
LL | const Y: i32 = X;
5+
| ^
6+
|
7+
= help: consider extracting the value of the `static` to a `const`, and referring to that
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0013`.

src/test/ui/error-codes/E0015.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn create_some() -> Option<u8> {
2+
Some(1)
3+
}
4+
5+
const FOO: Option<u8> = create_some();
6+
//~^ ERROR cannot call non-const fn `create_some` in constants [E0015]
7+
8+
fn main() {}

src/test/ui/error-codes/E0015.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0015]: cannot call non-const fn `create_some` in constants
2+
--> $DIR/E0015.rs:5:25
3+
|
4+
LL | const FOO: Option<u8> = create_some();
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0015`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
struct S<'a>(&'a str);
2+
3+
fn f(inner: fn(&str, &S)) {
4+
}
5+
6+
#[allow(unreachable_code)]
7+
fn main() {
8+
let inner: fn(_, _) = unimplemented!();
9+
f(inner); //~ ERROR mismatched types
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/closure-with-wrong-borrows.rs:9:7
3+
|
4+
LL | f(inner);
5+
| - ^^^^^ one type is more general than the other
6+
| |
7+
| arguments to this function are incorrect
8+
|
9+
= note: expected fn pointer `for<'a, 'b, 'c> fn(&'a str, &'b S<'c>)`
10+
found fn pointer `fn(_, _)`
11+
note: function defined here
12+
--> $DIR/closure-with-wrong-borrows.rs:3:4
13+
|
14+
LL | fn f(inner: fn(&str, &S)) {
15+
| ^ -------------------
16+
17+
error: aborting due to previous error
18+
19+
For more information about this error, try `rustc --explain E0308`.

src/tools/jsondoclint/src/validator.rs

+26-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ use std::hash::Hash;
33

44
use rustdoc_json_types::{
55
Constant, Crate, DynTrait, Enum, FnDecl, Function, FunctionPointer, GenericArg, GenericArgs,
6-
GenericBound, GenericParamDef, Generics, Id, Impl, Import, ItemEnum, Module, OpaqueTy, Path,
7-
Primitive, ProcMacro, Static, Struct, StructKind, Term, Trait, TraitAlias, Type, TypeBinding,
8-
TypeBindingKind, Typedef, Union, Variant, VariantKind, WherePredicate,
6+
GenericBound, GenericParamDef, Generics, Id, Impl, Import, ItemEnum, ItemSummary, Module,
7+
OpaqueTy, Path, Primitive, ProcMacro, Static, Struct, StructKind, Term, Trait, TraitAlias,
8+
Type, TypeBinding, TypeBindingKind, Typedef, Union, Variant, VariantKind, WherePredicate,
99
};
1010
use serde_json::Value;
1111

1212
use crate::{item_kind::Kind, json_find, Error, ErrorKind};
1313

14+
// This is a rustc implementation detail that we rely on here
15+
const LOCAL_CRATE_ID: u32 = 0;
16+
1417
/// The Validator walks over the JSON tree, and ensures it is well formed.
1518
/// It is made of several parts.
1619
///
@@ -53,12 +56,19 @@ impl<'a> Validator<'a> {
5356
}
5457

5558
pub fn check_crate(&mut self) {
59+
// Graph traverse the index
5660
let root = &self.krate.root;
5761
self.add_mod_id(root);
5862
while let Some(id) = set_remove(&mut self.todo) {
5963
self.seen_ids.insert(id);
6064
self.check_item(id);
6165
}
66+
67+
let root_crate_id = self.krate.index[root].crate_id;
68+
assert_eq!(root_crate_id, LOCAL_CRATE_ID, "LOCAL_CRATE_ID is wrong");
69+
for (id, item_info) in &self.krate.paths {
70+
self.check_item_info(id, item_info);
71+
}
6272
}
6373

6474
fn check_item(&mut self, id: &'a Id) {
@@ -364,6 +374,19 @@ impl<'a> Validator<'a> {
364374
fp.generic_params.iter().for_each(|gpd| self.check_generic_param_def(gpd));
365375
}
366376

377+
fn check_item_info(&mut self, id: &Id, item_info: &ItemSummary) {
378+
// FIXME: Their should be a better way to determine if an item is local, rather than relying on `LOCAL_CRATE_ID`,
379+
// which encodes rustc implementation details.
380+
if item_info.crate_id == LOCAL_CRATE_ID && !self.krate.index.contains_key(id) {
381+
self.errs.push(Error {
382+
id: id.clone(),
383+
kind: ErrorKind::Custom(
384+
"Id for local item in `paths` but not in `index`".to_owned(),
385+
),
386+
})
387+
}
388+
}
389+
367390
fn add_id_checked(&mut self, id: &'a Id, valid: fn(Kind) -> bool, expected: &str) {
368391
if let Some(kind) = self.kind_of(id) {
369392
if valid(kind) {

0 commit comments

Comments
 (0)