Skip to content

Rollup of 4 pull requests #81639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
2 changes: 1 addition & 1 deletion compiler/rustc_error_codes/src/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ E0516: include_str!("./error_codes/E0516.md"),
E0517: include_str!("./error_codes/E0517.md"),
E0518: include_str!("./error_codes/E0518.md"),
E0520: include_str!("./error_codes/E0520.md"),
E0521: include_str!("./error_codes/E0521.md"),
E0522: include_str!("./error_codes/E0522.md"),
E0524: include_str!("./error_codes/E0524.md"),
E0525: include_str!("./error_codes/E0525.md"),
Expand Down Expand Up @@ -597,7 +598,6 @@ E0780: include_str!("./error_codes/E0780.md"),
E0514, // metadata version mismatch
E0519, // local crate and dependency have same (crate-name, disambiguator)
// two dependencies have same (crate-name, disambiguator) but different SVH
E0521, // borrowed data escapes outside of closure
E0523,
// E0526, // shuffle indices are not constant
// E0540, // multiple rustc_deprecated attributes
Expand Down
28 changes: 28 additions & 0 deletions compiler/rustc_error_codes/src/error_codes/E0521.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Borrowed data escapes outside of closure.

Erroneous code example:

```compile_fail,E0521
let mut list: Vec<&str> = Vec::new();

let _add = |el: &str| {
list.push(el); // error: `el` escapes the closure body here
};
```

A type anotation of a closure parameter implies a new lifetime declaration.
Consider to drop it, the compiler is reliably able to infer them.

```
let mut list: Vec<&str> = Vec::new();

let _add = |el| {
list.push(el);
};
```

See the [Closure type inference and annotation][closure-infere-annotation] and
[Lifetime elision][lifetime-elision] sections of the Book for more details.

[closure-infere-annotation]: https://doc.rust-lang.org/book/ch13-01-closures.html#closure-type-inference-and-annotation
[lifetime-elision]: https://doc.rust-lang.org/reference/lifetime-elision.html
9 changes: 1 addition & 8 deletions compiler/rustc_resolve/src/late/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2083,18 +2083,11 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
output: Option<&'tcx hir::Ty<'tcx>>,
) {
debug!("visit_fn_like_elision: enter");
let mut arg_elide = Elide::FreshLateAnon(Cell::new(0));
let arg_scope = Scope::Elision { elide: arg_elide.clone(), s: self.scope };
let arg_scope = Scope::Elision { elide: Elide::FreshLateAnon(Cell::new(0)), s: self.scope };
self.with(arg_scope, |_, this| {
for input in inputs {
this.visit_ty(input);
}
match *this.scope {
Scope::Elision { ref elide, .. } => {
arg_elide = elide.clone();
}
_ => bug!(),
}
});

let output = match output {
Expand Down
11 changes: 11 additions & 0 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,7 @@ crate enum TypeKind {
Attr,
Derive,
TraitAlias,
Primitive,
}

crate trait GetDefId {
Expand Down Expand Up @@ -1403,6 +1404,16 @@ impl Type {
matches!(self, Type::Generic(_))
}

crate fn is_primitive(&self) -> bool {
match self {
Self::Primitive(_) => true,
Self::BorrowedRef { ref type_, .. } | Self::RawPointer(_, ref type_) => {
type_.is_primitive()
}
_ => false,
}
}

crate fn projection(&self) -> Option<(&Type, DefId, Symbol)> {
let (self_, trait_, name) = match self {
QPath { self_type, trait_, name } => (self_type, trait_, name),
Expand Down
27 changes: 14 additions & 13 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,20 @@ crate fn get_real_types(
cx: &DocContext<'_>,
recurse: i32,
) -> FxHashSet<(Type, TypeKind)> {
fn insert(res: &mut FxHashSet<(Type, TypeKind)>, cx: &DocContext<'_>, ty: Type) {
if let Some(kind) = ty.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
res.insert((ty, kind));
} else if ty.is_primitive() {
// This is a primitive, let's store it as such.
res.insert((ty, TypeKind::Primitive));
}
}
let mut res = FxHashSet::default();
if recurse >= 10 {
// FIXME: remove this whole recurse thing when the recursion bug is fixed
return res;
}

if arg.is_full_generic() {
let arg_s = Symbol::intern(&arg.print(&cx.cache).to_string());
if let Some(where_pred) = generics.where_predicates.iter().find(|g| match g {
Expand All @@ -194,11 +203,7 @@ crate fn get_real_types(
if !adds.is_empty() {
res.extend(adds);
} else if !ty.is_full_generic() {
if let Some(kind) =
ty.def_id().map(|did| cx.tcx.def_kind(did).clean(cx))
{
res.insert((ty, kind));
}
insert(&mut res, cx, ty);
}
}
}
Expand All @@ -212,26 +217,22 @@ crate fn get_real_types(
if !adds.is_empty() {
res.extend(adds);
} else if !ty.is_full_generic() {
if let Some(kind) = ty.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
res.insert((ty.clone(), kind));
}
insert(&mut res, cx, ty);
}
}
}
}
} else {
if let Some(kind) = arg.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
res.insert((arg.clone(), kind));
}
insert(&mut res, cx, arg.clone());
if let Some(gens) = arg.generics() {
for gen in gens.iter() {
if gen.is_full_generic() {
let adds = get_real_types(generics, gen, cx, recurse + 1);
if !adds.is_empty() {
res.extend(adds);
}
} else if let Some(kind) = gen.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
res.insert((gen.clone(), kind));
} else {
insert(&mut res, cx, gen.clone());
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/formats/item_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ impl From<clean::TypeKind> for ItemType {
clean::TypeKind::Attr => ItemType::ProcAttribute,
clean::TypeKind::Derive => ItemType::ProcDerive,
clean::TypeKind::TraitAlias => ItemType::TraitAlias,
clean::TypeKind::Primitive => ItemType::Primitive,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ crate fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
desc: item.doc_value().map_or_else(String::new, |s| short_markdown_summary(&s)),
parent: Some(did),
parent_idx: None,
search_type: get_index_search_type(&item, None),
search_type: get_index_search_type(&item, Some(cache)),
});
for alias in item.attrs.get_doc_aliases() {
cache
Expand Down
25 changes: 25 additions & 0 deletions src/test/rustdoc-js/primitive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// exact-check

const QUERY = [
"i32",
"str",
"TotoIsSomewhere",
];

const EXPECTED = [
{
'in_args': [
{ 'path': 'primitive', 'name': 'foo' },
],
},
{
'returned': [
{ 'path': 'primitive', 'name': 'foo' },
],
},
{
'others': [],
'in_args': [],
'returned': [],
},
];
5 changes: 5 additions & 0 deletions src/test/rustdoc-js/primitive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub fn foo(i: i32) -> &'static str {
"hello"
}

pub fn foo2<TotoIsSomewhere>(i: &TotoIsSomewhere, j: TotoIsSomewhere) {}
1 change: 1 addition & 0 deletions src/test/ui/borrowck/issue-45983.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ LL | give_any(|y| x = Some(y));

error: aborting due to previous error

For more information about this error, try `rustc --explain E0521`.
1 change: 1 addition & 0 deletions src/test/ui/borrowck/issue-7573.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ LL | lines_to_use.push(installed_id);

error: aborting due to previous error

For more information about this error, try `rustc --explain E0521`.
1 change: 1 addition & 0 deletions src/test/ui/borrowck/regions-escape-bound-fn-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ LL | with_int(|y| x = Some(y));

error: aborting due to previous error

For more information about this error, try `rustc --explain E0521`.
1 change: 1 addition & 0 deletions src/test/ui/borrowck/regions-escape-bound-fn.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ LL | with_int(|y| x = Some(y));

error: aborting due to previous error

For more information about this error, try `rustc --explain E0521`.
1 change: 1 addition & 0 deletions src/test/ui/borrowck/regions-escape-unboxed-closure.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ LL | with_int(&mut |y| x = Some(y));

error: aborting due to previous error

For more information about this error, try `rustc --explain E0521`.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions src/test/ui/cast/unsupported-cast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
struct A;

fn main() {
println!("{:?}", 1.0 as *const A); //~ERROR casting `f64` as `*const A` is invalid
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
error[E0606]: casting `f64` as `*const A` is invalid
--> $DIR/unsupported-cast.rs:6:20
--> $DIR/unsupported-cast.rs:4:20
|
LL | println!("{:?}", 1.0 as *const A); // Can't cast float to foreign.
LL | println!("{:?}", 1.0 as *const A);
| ^^^^^^^^^^^^^^^

error: aborting due to previous error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ LL | f = Some(x);

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0521`.
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ LL | a = &b;

error: aborting due to previous error

For more information about this error, try `rustc --explain E0521`.
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ LL | }

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0597`.
Some errors have detailed explanations: E0521, E0597.
For more information about an error, try `rustc --explain E0521`.
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ LL | | });

error: aborting due to previous error

For more information about this error, try `rustc --explain E0521`.
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ LL | | });

error: aborting due to previous error

For more information about this error, try `rustc --explain E0521`.
1 change: 1 addition & 0 deletions src/test/ui/nll/outlives-suggestion-simple.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,4 @@ LL | Bar2::new(&self)

error: aborting due to 9 previous errors

For more information about this error, try `rustc --explain E0521`.
1 change: 1 addition & 0 deletions src/test/ui/nll/user-annotations/closure-substs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ LL | b(x);

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0521`.
1 change: 1 addition & 0 deletions src/test/ui/regions/issue-78262.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ LL | let f = |x: &dyn TT| x.func();

error: aborting due to previous error

For more information about this error, try `rustc --explain E0521`.
7 changes: 0 additions & 7 deletions src/test/ui/unsupported-cast.rs

This file was deleted.

4 changes: 4 additions & 0 deletions src/tools/rustdoc-js/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ function betterLookingDiff(entry, data) {
if (!entry.hasOwnProperty(key)) {
continue;
}
if (!data || !data.hasOwnProperty(key)) {
output += '-' + spaces + contentToDiffLine(key, entry[key]) + '\n';
continue;
}
let value = data[key];
if (value !== entry[key]) {
output += '-' + spaces + contentToDiffLine(key, entry[key]) + '\n';
Expand Down