Skip to content

Commit ad3a6f7

Browse files
committed
Auto merge of #76582 - tmandry:rollup-lwwc93b, r=tmandry
Rollup of 11 pull requests Successful merges: - #75857 (Syntactically permit unsafety on mods) - #76289 (Add docs about crate level documentation support) - #76514 (Add revisions to const generic issue UI tests.) - #76524 (typeck: don't suggest inaccessible private fields) - #76548 (Validate removal of AscribeUserType, FakeRead, and Shallow borrow) - #76555 (Reword `trivial_casts` lint in rustc book to better explain what it does.) - #76559 (add the `const_evaluatable_checked` feature) - #76563 (small typo fix in rustc_parse docs) - #76565 (take reference to Place directly instead of taking reference to Box<Place>) - #76567 (use push(char) to add chars (single-char &strs) to strings instead of push_str(&str)) - #76568 (Add missing examples on core traits' method) Failed merges: r? `@ghost`
2 parents 8c35a92 + 044f717 commit ad3a6f7

File tree

104 files changed

+1320
-301
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+1320
-301
lines changed

Diff for: compiler/rustc_ast/src/ast.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -2289,22 +2289,28 @@ impl FnRetTy {
22892289
/// Module declaration.
22902290
///
22912291
/// E.g., `mod foo;` or `mod foo { .. }`.
2292-
#[derive(Clone, Encodable, Decodable, Debug, Default)]
2292+
#[derive(Clone, Encodable, Decodable, Debug)]
22932293
pub struct Mod {
22942294
/// A span from the first token past `{` to the last token until `}`.
22952295
/// For `mod foo;`, the inner span ranges from the first token
22962296
/// to the last token in the external file.
22972297
pub inner: Span,
2298+
/// `unsafe` keyword accepted syntactically for macro DSLs, but not
2299+
/// semantically by Rust.
2300+
pub unsafety: Unsafe,
22982301
pub items: Vec<P<Item>>,
22992302
/// `true` for `mod foo { .. }`; `false` for `mod foo;`.
23002303
pub inline: bool,
23012304
}
23022305

23032306
/// Foreign module declaration.
23042307
///
2305-
/// E.g., `extern { .. }` or `extern C { .. }`.
2308+
/// E.g., `extern { .. }` or `extern "C" { .. }`.
23062309
#[derive(Clone, Encodable, Decodable, Debug)]
23072310
pub struct ForeignMod {
2311+
/// `unsafe` keyword accepted syntactically for macro DSLs, but not
2312+
/// semantically by Rust.
2313+
pub unsafety: Unsafe,
23082314
pub abi: Option<StrLit>,
23092315
pub items: Vec<P<ForeignItem>>,
23102316
}

Diff for: compiler/rustc_ast/src/mut_visit.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
490490
}
491491

492492
pub fn noop_visit_foreign_mod<T: MutVisitor>(foreign_mod: &mut ForeignMod, vis: &mut T) {
493-
let ForeignMod { abi: _, items } = foreign_mod;
493+
let ForeignMod { unsafety: _, abi: _, items } = foreign_mod;
494494
items.flat_map_in_place(|item| vis.flat_map_foreign_item(item));
495495
}
496496

@@ -970,7 +970,8 @@ pub fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) {
970970
vis.visit_asyncness(asyncness);
971971
}
972972

973-
pub fn noop_visit_mod<T: MutVisitor>(Mod { inner, items, inline: _ }: &mut Mod, vis: &mut T) {
973+
pub fn noop_visit_mod<T: MutVisitor>(module: &mut Mod, vis: &mut T) {
974+
let Mod { inner, unsafety: _, items, inline: _ } = module;
974975
vis.visit_span(inner);
975976
items.flat_map_in_place(|item| vis.flat_map_item(item));
976977
}
@@ -990,7 +991,7 @@ pub fn noop_visit_crate<T: MutVisitor>(krate: &mut Crate, vis: &mut T) {
990991

991992
let len = items.len();
992993
if len == 0 {
993-
let module = Mod { inner: span, items: vec![], inline: true };
994+
let module = Mod { inner: span, unsafety: Unsafe::No, items: vec![], inline: true };
994995
Crate { module, attrs: vec![], span, proc_macros }
995996
} else if len == 1 {
996997
let Item { attrs, span, kind, .. } = items.into_iter().next().unwrap().into_inner();

Diff for: compiler/rustc_ast_passes/src/ast_validation.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -990,12 +990,15 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
990990
self.error_item_without_body(item.span, "function", msg, " { <body> }");
991991
}
992992
}
993-
ItemKind::ForeignMod(_) => {
993+
ItemKind::ForeignMod(ForeignMod { unsafety, .. }) => {
994994
let old_item = mem::replace(&mut self.extern_mod, Some(item));
995995
self.invalid_visibility(
996996
&item.vis,
997997
Some("place qualifiers on individual foreign items instead"),
998998
);
999+
if let Unsafe::Yes(span) = unsafety {
1000+
self.err_handler().span_err(span, "extern block cannot be declared unsafe");
1001+
}
9991002
visit::walk_item(self, item);
10001003
self.extern_mod = old_item;
10011004
return; // Avoid visiting again.
@@ -1029,7 +1032,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10291032
walk_list!(self, visit_attribute, &item.attrs);
10301033
return;
10311034
}
1032-
ItemKind::Mod(Mod { inline, .. }) => {
1035+
ItemKind::Mod(Mod { inline, unsafety, .. }) => {
1036+
if let Unsafe::Yes(span) = unsafety {
1037+
self.err_handler().span_err(span, "module cannot be declared unsafe");
1038+
}
10331039
// Ensure that `path` attributes on modules are recorded as used (cf. issue #35584).
10341040
if !inline && !self.session.contains_name(&item.attrs, sym::path) {
10351041
self.check_mod_file_item_asciionly(item.ident);

Diff for: compiler/rustc_ast_pretty/src/pprust.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,11 @@ impl<'a> State<'a> {
11391139
self.print_fn_full(sig, item.ident, gen, &item.vis, def, body, &item.attrs);
11401140
}
11411141
ast::ItemKind::Mod(ref _mod) => {
1142-
self.head(visibility_qualified(&item.vis, "mod"));
1142+
self.head(to_string(|s| {
1143+
s.print_visibility(&item.vis);
1144+
s.print_unsafety(_mod.unsafety);
1145+
s.word("mod");
1146+
}));
11431147
self.print_ident(item.ident);
11441148

11451149
if _mod.inline || self.is_expanded {
@@ -1154,7 +1158,10 @@ impl<'a> State<'a> {
11541158
}
11551159
}
11561160
ast::ItemKind::ForeignMod(ref nmod) => {
1157-
self.head("extern");
1161+
self.head(to_string(|s| {
1162+
s.print_unsafety(nmod.unsafety);
1163+
s.word("extern");
1164+
}));
11581165
if let Some(abi) = nmod.abi {
11591166
self.print_literal(&abi.as_lit());
11601167
self.nbsp();

Diff for: compiler/rustc_builtin_macros/src/format_foreign.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,14 @@ pub mod printf {
166166
let cap = self.span.len() + if has_options { 2 } else { 0 };
167167
let mut s = String::with_capacity(cap);
168168

169-
s.push_str("{");
169+
s.push('{');
170170

171171
if let Some(arg) = self.parameter {
172172
write!(s, "{}", arg.checked_sub(1)?).ok()?;
173173
}
174174

175175
if has_options {
176-
s.push_str(":");
176+
s.push(':');
177177

178178
let align = if let Some(fill) = fill {
179179
s.push_str(fill);
@@ -191,19 +191,19 @@ pub mod printf {
191191
}
192192

193193
if alt {
194-
s.push_str("#");
194+
s.push('#');
195195
}
196196

197197
if zero_fill {
198-
s.push_str("0");
198+
s.push('0');
199199
}
200200

201201
if let Some(width) = width {
202202
width.translate(&mut s).ok()?;
203203
}
204204

205205
if let Some(precision) = precision {
206-
s.push_str(".");
206+
s.push('.');
207207
precision.translate(&mut s).ok()?;
208208
}
209209

@@ -212,7 +212,7 @@ pub mod printf {
212212
}
213213
}
214214

215-
s.push_str("}");
215+
s.push('}');
216216
Some(s)
217217
}
218218
}

Diff for: compiler/rustc_codegen_ssa/src/back/link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ fn exec_linker(
10761076
}
10771077
.to_string(),
10781078
);
1079-
args.push_str("\n");
1079+
args.push('\n');
10801080
}
10811081
let file = tmpdir.join("linker-arguments");
10821082
let bytes = if sess.target.target.options.is_like_msvc {

Diff for: compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn push_debuginfo_type_name<'tcx>(
3737
ty::Bool => output.push_str("bool"),
3838
ty::Char => output.push_str("char"),
3939
ty::Str => output.push_str("str"),
40-
ty::Never => output.push_str("!"),
40+
ty::Never => output.push('!'),
4141
ty::Int(int_ty) => output.push_str(int_ty.name_str()),
4242
ty::Uint(uint_ty) => output.push_str(uint_ty.name_str()),
4343
ty::Float(float_ty) => output.push_str(float_ty.name_str()),

Diff for: compiler/rustc_expand/src/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ impl<'a> StripUnconfigured<'a> {
399399
}
400400

401401
pub fn configure_foreign_mod(&mut self, foreign_mod: &mut ast::ForeignMod) {
402-
let ast::ForeignMod { abi: _, items } = foreign_mod;
402+
let ast::ForeignMod { unsafety: _, abi: _, items } = foreign_mod;
403403
items.flat_map_in_place(|item| self.configure(item));
404404
}
405405

Diff for: compiler/rustc_expand/src/expand.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_ast::token;
1313
use rustc_ast::tokenstream::TokenStream;
1414
use rustc_ast::visit::{self, AssocCtxt, Visitor};
1515
use rustc_ast::{self as ast, AttrItem, Block, LitKind, NodeId, PatKind, Path};
16-
use rustc_ast::{ItemKind, MacArgs, MacCallStmt, MacStmtStyle, StmtKind};
16+
use rustc_ast::{ItemKind, MacArgs, MacCallStmt, MacStmtStyle, StmtKind, Unsafe};
1717
use rustc_ast_pretty::pprust;
1818
use rustc_attr::{self as attr, is_builtin_attr, HasAttrs};
1919
use rustc_data_structures::map_in_place::MapInPlace;
@@ -370,11 +370,21 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
370370
None => {
371371
// Resolution failed so we return an empty expansion
372372
krate.attrs = vec![];
373-
krate.module = ast::Mod { inner: orig_mod_span, items: vec![], inline: true };
373+
krate.module = ast::Mod {
374+
inner: orig_mod_span,
375+
unsafety: Unsafe::No,
376+
items: vec![],
377+
inline: true,
378+
};
374379
}
375380
Some(ast::Item { span, kind, .. }) => {
376381
krate.attrs = vec![];
377-
krate.module = ast::Mod { inner: orig_mod_span, items: vec![], inline: true };
382+
krate.module = ast::Mod {
383+
inner: orig_mod_span,
384+
unsafety: Unsafe::No,
385+
items: vec![],
386+
inline: true,
387+
};
378388
self.cx.span_err(
379389
span,
380390
&format!(
@@ -1441,8 +1451,15 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
14411451
push_directory(&self.cx.sess, ident, &item.attrs, dir)
14421452
} else {
14431453
// We have an outline `mod foo;` so we need to parse the file.
1444-
let (new_mod, dir) =
1445-
parse_external_mod(&self.cx.sess, ident, span, dir, &mut attrs, pushed);
1454+
let (new_mod, dir) = parse_external_mod(
1455+
&self.cx.sess,
1456+
ident,
1457+
span,
1458+
old_mod.unsafety,
1459+
dir,
1460+
&mut attrs,
1461+
pushed,
1462+
);
14461463

14471464
let krate = ast::Crate {
14481465
span: new_mod.inner,

Diff for: compiler/rustc_expand/src/module.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_ast::{token, Attribute, Mod};
1+
use rustc_ast::{token, Attribute, Mod, Unsafe};
22
use rustc_errors::{struct_span_err, PResult};
33
use rustc_parse::new_parser_from_file;
44
use rustc_session::parse::ParseSess;
@@ -42,6 +42,7 @@ crate fn parse_external_mod(
4242
sess: &Session,
4343
id: Ident,
4444
span: Span, // The span to blame on errors.
45+
unsafety: Unsafe,
4546
Directory { mut ownership, path }: Directory,
4647
attrs: &mut Vec<Attribute>,
4748
pop_mod_stack: &mut bool,
@@ -60,13 +61,16 @@ crate fn parse_external_mod(
6061
drop(included_mod_stack);
6162

6263
// Actually parse the external file as a module.
63-
let mut module =
64-
new_parser_from_file(&sess.parse_sess, &mp.path, Some(span)).parse_mod(&token::Eof)?;
64+
let mut parser = new_parser_from_file(&sess.parse_sess, &mp.path, Some(span));
65+
let mut module = parser.parse_mod(&token::Eof, unsafety)?;
6566
module.0.inline = false;
6667
module
6768
};
6869
// (1) ...instead, we return a dummy module.
69-
let (module, mut new_attrs) = result.map_err(|mut err| err.emit()).unwrap_or_default();
70+
let (module, mut new_attrs) = result.map_err(|mut err| err.emit()).unwrap_or_else(|_| {
71+
let module = Mod { inner: Span::default(), unsafety, items: Vec::new(), inline: false };
72+
(module, Vec::new())
73+
});
7074
attrs.append(&mut new_attrs);
7175

7276
// Extract the directory path for submodules of `module`.

Diff for: compiler/rustc_feature/src/active.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,9 @@ declare_features! (
585585
/// Allows `if let` guard in match arms.
586586
(active, if_let_guard, "1.47.0", Some(51114), None),
587587

588+
/// Allows non trivial generic constants which have to be manually propageted upwards.
589+
(active, const_evaluatable_checked, "1.48.0", Some(76560), None),
590+
588591
// -------------------------------------------------------------------------
589592
// feature-group-end: actual feature gates
590593
// -------------------------------------------------------------------------
@@ -600,13 +603,14 @@ pub const INCOMPLETE_FEATURES: &[Symbol] = &[
600603
sym::const_generics,
601604
sym::let_chains,
602605
sym::raw_dylib,
606+
sym::const_evaluatable_checked,
603607
sym::const_trait_impl,
604608
sym::const_trait_bound_opt_out,
605609
sym::lazy_normalization_consts,
606610
sym::specialization,
607611
];
608612

609613
/// Some features are not allowed to be used together at the same time, if
610-
/// the two are present, produce an error
614+
/// the two are present, produce an error.
611615
pub const INCOMPATIBLE_FEATURES: &[(Symbol, Symbol)] =
612616
&[(sym::const_generics, sym::min_const_generics)];

Diff for: compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20932093
_ => String::new(),
20942094
};
20952095
if !s.is_empty() {
2096-
s.push_str(" ");
2096+
s.push(' ');
20972097
}
20982098
s
20992099
};

Diff for: compiler/rustc_infer/src/infer/lub.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl TypeRelation<'tcx> for Lub<'combine, 'infcx, 'tcx> {
5050
ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b),
5151
ty::Covariant => self.relate(a, b),
5252
// FIXME(#41044) -- not correct, need test
53-
ty::Bivariant => Ok(a.clone()),
53+
ty::Bivariant => Ok(a),
5454
ty::Contravariant => self.fields.glb(self.a_is_expected).relate(a, b),
5555
}
5656
}

Diff for: compiler/rustc_mir/src/borrow_check/diagnostics/mod.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
150150
Some(mut descr) => {
151151
// Surround descr with `backticks`.
152152
descr.reserve(2);
153-
descr.insert_str(0, "`");
154-
descr.push_str("`");
153+
descr.insert(0, '`');
154+
descr.push('`');
155155
descr
156156
}
157157
None => "value".to_string(),
@@ -222,7 +222,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
222222
if self.upvars[var_index].by_ref {
223223
buf.push_str(&name);
224224
} else {
225-
buf.push_str(&format!("*{}", &name));
225+
buf.push('*');
226+
buf.push_str(&name);
226227
}
227228
} else {
228229
if autoderef {
@@ -234,7 +235,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
234235
&including_downcast,
235236
)?;
236237
} else {
237-
buf.push_str(&"*");
238+
buf.push('*');
238239
self.append_place_to_string(
239240
PlaceRef { local, projection: proj_base },
240241
buf,
@@ -272,7 +273,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
272273
autoderef,
273274
&including_downcast,
274275
)?;
275-
buf.push_str(&format!(".{}", field_name));
276+
buf.push('.');
277+
buf.push_str(&field_name);
276278
}
277279
}
278280
ProjectionElem::Index(index) => {
@@ -284,11 +286,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
284286
autoderef,
285287
&including_downcast,
286288
)?;
287-
buf.push_str("[");
289+
buf.push('[');
288290
if self.append_local_to_string(*index, buf).is_err() {
289-
buf.push_str("_");
291+
buf.push('_');
290292
}
291-
buf.push_str("]");
293+
buf.push(']');
292294
}
293295
ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {
294296
autoderef = true;
@@ -301,7 +303,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
301303
autoderef,
302304
&including_downcast,
303305
)?;
304-
buf.push_str(&"[..]");
306+
buf.push_str("[..]");
305307
}
306308
};
307309
}
@@ -648,7 +650,7 @@ impl UseSpans {
648650
" in closure".to_string()
649651
}
650652
}
651-
_ => "".to_string(),
653+
_ => String::new(),
652654
}
653655
}
654656

0 commit comments

Comments
 (0)