diff --git a/compiler/rustc_ast_pretty/src/lib.rs b/compiler/rustc_ast_pretty/src/lib.rs index 84d9ce278a21a..1079ccccb03e7 100644 --- a/compiler/rustc_ast_pretty/src/lib.rs +++ b/compiler/rustc_ast_pretty/src/lib.rs @@ -2,6 +2,7 @@ #![allow(internal_features)] #![doc(rust_logo)] #![feature(box_patterns)] +#![feature(negative_impls)] #![feature(rustdoc_internals)] // tidy-alphabetical-end diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index e4fd7e94fde14..142c80b8e39e5 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -234,6 +234,38 @@ struct BufEntry { size: isize, } +// Boxes opened with methods like `Printer::{cbox,ibox}` must be closed with +// `Printer::end`. Failure to do so can result in bad indenting, or in extreme +// cases, cause no output to be produced at all. +// +// Box opening and closing used to be entirely implicit, which was hard to +// understand and easy to get wrong. This marker type is now returned from the +// box opening methods and forgotten by `Printer::end`. Any marker that isn't +// forgotten will trigger a panic in `drop`. (Closing a box more than once +// isn't possible because `BoxMarker` doesn't implement `Copy` or `Clone`.) +// +// FIXME(nnethercote): the panic in `drop` is currently disabled because a few +// places fail to close their boxes. It can be enabled once they are fixed. +// +// Note: it would be better to make open/close mismatching impossible and avoid +// the need for this marker type altogether by having functions like +// `with_ibox` that open a box, call a closure, and then close the box. That +// would work for simple cases, but box lifetimes sometimes interact with +// complex control flow and across function boundaries in ways that are +// difficult to handle with such a technique. +#[must_use] +pub struct BoxMarker; + +impl !Clone for BoxMarker {} +impl !Copy for BoxMarker {} + +impl Drop for BoxMarker { + fn drop(&mut self) { + // FIXME(nnethercote): enable once the bad cases are fixed + //panic!("BoxMarker not ended with `Printer::end()`"); + } +} + impl Printer { pub fn new() -> Self { Printer { @@ -270,7 +302,8 @@ impl Printer { } } - fn scan_begin(&mut self, token: BeginToken) { + // This is is where `BoxMarker`s are produced. + fn scan_begin(&mut self, token: BeginToken) -> BoxMarker { if self.scan_stack.is_empty() { self.left_total = 1; self.right_total = 1; @@ -278,15 +311,18 @@ impl Printer { } let right = self.buf.push(BufEntry { token: Token::Begin(token), size: -self.right_total }); self.scan_stack.push_back(right); + BoxMarker } - fn scan_end(&mut self) { + // This is is where `BoxMarker`s are consumed. + fn scan_end(&mut self, b: BoxMarker) { if self.scan_stack.is_empty() { self.print_end(); } else { let right = self.buf.push(BufEntry { token: Token::End, size: -1 }); self.scan_stack.push_back(right); } + std::mem::forget(b) } fn scan_break(&mut self, token: BreakToken) { diff --git a/compiler/rustc_ast_pretty/src/pp/convenience.rs b/compiler/rustc_ast_pretty/src/pp/convenience.rs index a1c07bb07e42a..9b902b38122c8 100644 --- a/compiler/rustc_ast_pretty/src/pp/convenience.rs +++ b/compiler/rustc_ast_pretty/src/pp/convenience.rs @@ -1,25 +1,27 @@ use std::borrow::Cow; -use crate::pp::{BeginToken, BreakToken, Breaks, IndentStyle, Printer, SIZE_INFINITY, Token}; +use crate::pp::{ + BeginToken, BoxMarker, BreakToken, Breaks, IndentStyle, Printer, SIZE_INFINITY, Token, +}; impl Printer { /// "raw box" - pub fn rbox(&mut self, indent: isize, breaks: Breaks) { + pub fn rbox(&mut self, indent: isize, breaks: Breaks) -> BoxMarker { self.scan_begin(BeginToken { indent: IndentStyle::Block { offset: indent }, breaks }) } /// Inconsistent breaking box - pub fn ibox(&mut self, indent: isize) { + pub fn ibox(&mut self, indent: isize) -> BoxMarker { self.rbox(indent, Breaks::Inconsistent) } /// Consistent breaking box - pub fn cbox(&mut self, indent: isize) { + pub fn cbox(&mut self, indent: isize) -> BoxMarker { self.rbox(indent, Breaks::Consistent) } - pub fn visual_align(&mut self) { - self.scan_begin(BeginToken { indent: IndentStyle::Visual, breaks: Breaks::Consistent }); + pub fn visual_align(&mut self) -> BoxMarker { + self.scan_begin(BeginToken { indent: IndentStyle::Visual, breaks: Breaks::Consistent }) } pub fn break_offset(&mut self, n: usize, off: isize) { @@ -30,8 +32,8 @@ impl Printer { }); } - pub fn end(&mut self) { - self.scan_end() + pub fn end(&mut self, b: BoxMarker) { + self.scan_end(b) } pub fn eof(mut self) -> String { diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 62a50c73855fc..985359e1234e8 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -26,7 +26,7 @@ use rustc_span::symbol::IdentPrinter; use rustc_span::{BytePos, CharPos, DUMMY_SP, FileName, Ident, Pos, Span, Symbol, kw, sym}; use crate::pp::Breaks::{Consistent, Inconsistent}; -use crate::pp::{self, Breaks}; +use crate::pp::{self, BoxMarker, Breaks}; use crate::pprust::state::fixup::FixupContext; pub enum MacHeader<'a> { @@ -419,7 +419,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere { let mut it = elts.into_iter(); - self.rbox(0, b); + let rb = self.rbox(0, b); if let Some(first) = it.next() { op(self, first); for elt in it { @@ -430,7 +430,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere op(self, elt); } } - self.end(); + self.end(rb); } fn commasep<'x, T: 'x, F, I>(&mut self, b: Breaks, elts: I, op: F) @@ -461,7 +461,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere self.zerobreak(); } if let Some((last, lines)) = cmnt.lines.split_last() { - self.ibox(0); + let ib = self.ibox(0); for line in lines { self.word(line.clone()); @@ -471,7 +471,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere self.word(last.clone()); self.space(); - self.end(); + self.end(ib); } self.zerobreak() } @@ -494,14 +494,14 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere self.word(line.clone()); self.hardbreak() } else { - self.visual_align(); + let vb = self.visual_align(); for line in &cmnt.lines { if !line.is_empty() { self.word(line.clone()); } self.hardbreak(); } - self.end(); + self.end(vb); } } CommentStyle::BlankLine => { @@ -620,7 +620,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere } fn print_attr_item(&mut self, item: &ast::AttrItem, span: Span) { - self.ibox(0); + let ib = self.ibox(0); match item.unsafety { ast::Safety::Unsafe(_) => { self.word("unsafe"); @@ -653,7 +653,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere ast::Safety::Unsafe(_) => self.pclose(), ast::Safety::Default | ast::Safety::Safe(_) => {} } - self.end(); + self.end(ib); } /// This doesn't deserve to be called "pretty" printing, but it should be @@ -739,9 +739,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere convert_dollar_crate: bool, span: Span, ) { - if delim == Delimiter::Brace { - self.cbox(INDENT_UNIT); - } + let cb = (delim == Delimiter::Brace).then(|| self.cbox(INDENT_UNIT)); match header { Some(MacHeader::Path(path)) => self.print_path(path, false, 0), Some(MacHeader::Keyword(kw)) => self.word(kw), @@ -763,18 +761,18 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere if !tts.is_empty() { self.space(); } - self.ibox(0); + let ib = self.ibox(0); self.print_tts(tts, convert_dollar_crate); - self.end(); + self.end(ib); let empty = tts.is_empty(); - self.bclose(span, empty); + self.bclose(span, empty, cb.unwrap()); } delim => { let token_str = self.token_kind_to_string(&delim.as_open_token_kind()); self.word(token_str); - self.ibox(0); + let ib = self.ibox(0); self.print_tts(tts, convert_dollar_crate); - self.end(); + self.end(ib); let token_str = self.token_kind_to_string(&delim.as_close_token_kind()); self.word(token_str); } @@ -828,37 +826,38 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere } } - fn head>>(&mut self, w: S) { + fn head>>(&mut self, w: S) -> (BoxMarker, BoxMarker) { let w = w.into(); // Outer-box is consistent. - self.cbox(INDENT_UNIT); + let cb = self.cbox(INDENT_UNIT); // Head-box is inconsistent. - self.ibox(0); + let ib = self.ibox(0); // Keyword that starts the head. if !w.is_empty() { self.word_nbsp(w); } + (cb, ib) } - fn bopen(&mut self) { + fn bopen(&mut self, ib: BoxMarker) { self.word("{"); - self.end(); // Close the head-box. + self.end(ib); } - fn bclose_maybe_open(&mut self, span: rustc_span::Span, empty: bool, close_box: bool) { + fn bclose_maybe_open(&mut self, span: rustc_span::Span, empty: bool, cb: Option) { let has_comment = self.maybe_print_comment(span.hi()); if !empty || has_comment { self.break_offset_if_not_bol(1, -INDENT_UNIT); } self.word("}"); - if close_box { - self.end(); // Close the outer-box. + if let Some(cb) = cb { + self.end(cb); } } - fn bclose(&mut self, span: rustc_span::Span, empty: bool) { - let close_box = true; - self.bclose_maybe_open(span, empty, close_box) + fn bclose(&mut self, span: rustc_span::Span, empty: bool, cb: BoxMarker) { + let cb = Some(cb); + self.bclose_maybe_open(span, empty, cb) } fn break_offset_if_not_bol(&mut self, n: usize, off: isize) { @@ -1014,11 +1013,8 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::Dere fn block_to_string(&self, blk: &ast::Block) -> String { Self::to_string(|s| { - // Containing cbox, will be closed by `print_block` at `}`. - s.cbox(INDENT_UNIT); - // Head-ibox, will be closed by `print_block` after `{`. - s.ibox(0); - s.print_block(blk) + let (cb, ib) = s.head(""); + s.print_block(blk, cb, ib) }) } @@ -1090,7 +1086,7 @@ impl<'a> State<'a> { F: FnMut(&mut State<'_>, &T), G: FnMut(&T) -> rustc_span::Span, { - self.rbox(0, b); + let rb = self.rbox(0, b); let len = elts.len(); let mut i = 0; for elt in elts { @@ -1103,7 +1099,7 @@ impl<'a> State<'a> { self.space_if_not_bol(); } } - self.end(); + self.end(rb); } fn commasep_exprs(&mut self, b: Breaks, exprs: &[P]) { @@ -1172,7 +1168,7 @@ impl<'a> State<'a> { pub fn print_type(&mut self, ty: &ast::Ty) { self.maybe_print_comment(ty.span.lo()); - self.ibox(0); + let ib = self.ibox(0); match &ty.kind { ast::TyKind::Slice(ty) => { self.word("["); @@ -1214,12 +1210,12 @@ impl<'a> State<'a> { self.print_ty_fn(f.ext, f.safety, &f.decl, None, &f.generic_params); } ast::TyKind::UnsafeBinder(f) => { - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); self.word("unsafe"); self.print_generic_params(&f.generic_params); self.nbsp(); self.print_type(&f.inner_ty); - self.end(); + self.end(ib); } ast::TyKind::Path(None, path) => { self.print_path(path, false, 0); @@ -1277,7 +1273,7 @@ impl<'a> State<'a> { self.print_ty_pat(pat); } } - self.end(); + self.end(ib); } fn print_trait_ref(&mut self, t: &ast::TraitRef) { @@ -1324,15 +1320,15 @@ impl<'a> State<'a> { ast::StmtKind::Let(loc) => { self.print_outer_attributes(&loc.attrs); self.space_if_not_bol(); - self.ibox(INDENT_UNIT); + let ib1 = self.ibox(INDENT_UNIT); if loc.super_.is_some() { self.word_nbsp("super"); } self.word_nbsp("let"); - self.ibox(INDENT_UNIT); + let ib2 = self.ibox(INDENT_UNIT); self.print_local_decl(loc); - self.end(); + self.end(ib2); if let Some((init, els)) = loc.kind.init_else_opt() { self.nbsp(); self.word_space("="); @@ -1342,14 +1338,14 @@ impl<'a> State<'a> { FixupContext::default(), ); if let Some(els) = els { - self.cbox(INDENT_UNIT); - self.ibox(INDENT_UNIT); + let cb = self.cbox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); self.word(" else "); - self.print_block(els); + self.print_block(els, cb, ib); } } self.word(";"); - self.end(); // `let` ibox + self.end(ib1); } ast::StmtKind::Item(item) => self.print_item(item), ast::StmtKind::Expr(expr) => { @@ -1380,23 +1376,30 @@ impl<'a> State<'a> { self.maybe_print_trailing_comment(st.span, None) } - fn print_block(&mut self, blk: &ast::Block) { - self.print_block_with_attrs(blk, &[]) + fn print_block(&mut self, blk: &ast::Block, cb: BoxMarker, ib: BoxMarker) { + self.print_block_with_attrs(blk, &[], cb, ib) } - fn print_block_unclosed_indent(&mut self, blk: &ast::Block) { - self.print_block_maybe_unclosed(blk, &[], false) + fn print_block_unclosed_indent(&mut self, blk: &ast::Block, ib: BoxMarker) { + self.print_block_maybe_unclosed(blk, &[], None, ib) } - fn print_block_with_attrs(&mut self, blk: &ast::Block, attrs: &[ast::Attribute]) { - self.print_block_maybe_unclosed(blk, attrs, true) + fn print_block_with_attrs( + &mut self, + blk: &ast::Block, + attrs: &[ast::Attribute], + cb: BoxMarker, + ib: BoxMarker, + ) { + self.print_block_maybe_unclosed(blk, attrs, Some(cb), ib) } fn print_block_maybe_unclosed( &mut self, blk: &ast::Block, attrs: &[ast::Attribute], - close_box: bool, + cb: Option, + ib: BoxMarker, ) { match blk.rules { BlockCheckMode::Unsafe(..) => self.word_space("unsafe"), @@ -1404,7 +1407,7 @@ impl<'a> State<'a> { } self.maybe_print_comment(blk.span.lo()); self.ann.pre(self, AnnNode::Block(blk)); - self.bopen(); + self.bopen(ib); let has_attrs = self.print_inner_attributes(attrs); @@ -1421,7 +1424,7 @@ impl<'a> State<'a> { } let empty = !has_attrs && blk.stmts.is_empty(); - self.bclose_maybe_open(blk.span, empty, close_box); + self.bclose_maybe_open(blk.span, empty, cb); self.ann.post(self, AnnNode::Block(blk)) } @@ -1556,8 +1559,8 @@ impl<'a> State<'a> { } } InlineAsmOperand::Label { block } => { - s.head("label"); - s.print_block(block); + let (cb, ib) = s.head("label"); + s.print_block(block, cb, ib); } } } @@ -1671,13 +1674,13 @@ impl<'a> State<'a> { Consistent, fields, |s, f| { - s.cbox(INDENT_UNIT); + let cb = s.cbox(INDENT_UNIT); if !f.is_shorthand { s.print_ident(f.ident); s.word_nbsp(":"); } s.print_pat(&f.pat); - s.end(); + s.end(cb); }, |f| f.pat.span, ); @@ -1928,7 +1931,7 @@ impl<'a> State<'a> { } fn print_param(&mut self, input: &ast::Param, is_closure: bool) { - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); self.print_outer_attributes_inline(&input.attrs); @@ -1947,16 +1950,16 @@ impl<'a> State<'a> { } } } - self.end(); + self.end(ib); } fn print_fn_ret_ty(&mut self, fn_ret_ty: &ast::FnRetTy) { if let ast::FnRetTy::Ty(ty) = fn_ret_ty { self.space_if_not_bol(); - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); self.word_space("->"); self.print_type(ty); - self.end(); + self.end(ib); self.maybe_print_comment(ty.span.lo()); } } @@ -1969,12 +1972,12 @@ impl<'a> State<'a> { name: Option, generic_params: &[ast::GenericParam], ) { - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); self.print_formal_generic_params(generic_params); let generics = ast::Generics::default(); let header = ast::FnHeader { safety, ext, ..ast::FnHeader::default() }; self.print_fn(decl, header, name, &generics); - self.end(); + self.end(ib); } fn print_fn_header_info(&mut self, header: ast::FnHeader) { @@ -2052,7 +2055,7 @@ impl<'a> State<'a> { } fn print_meta_item(&mut self, item: &ast::MetaItem) { - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); match &item.kind { ast::MetaItemKind::Word => self.print_path(&item.path, false, 0), ast::MetaItemKind::NameValue(value) => { @@ -2068,7 +2071,7 @@ impl<'a> State<'a> { self.pclose(); } } - self.end(); + self.end(ib); } pub(crate) fn bounds_to_string(&self, bounds: &[ast::GenericBound]) -> String { diff --git a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs index 48da9fc63b860..38cadc77b7771 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs @@ -13,7 +13,7 @@ use rustc_ast::{ use crate::pp::Breaks::Inconsistent; use crate::pprust::state::fixup::FixupContext; -use crate::pprust::state::{AnnNode, INDENT_UNIT, PrintState, State}; +use crate::pprust::state::{AnnNode, BoxMarker, INDENT_UNIT, PrintState, State}; impl<'a> State<'a> { fn print_else(&mut self, els: Option<&ast::Expr>) { @@ -21,20 +21,20 @@ impl<'a> State<'a> { match &_else.kind { // Another `else if` block. ast::ExprKind::If(i, then, e) => { - self.cbox(0); - self.ibox(0); + let cb = self.cbox(0); + let ib = self.ibox(0); self.word(" else if "); self.print_expr_as_cond(i); self.space(); - self.print_block(then); + self.print_block(then, cb, ib); self.print_else(e.as_deref()) } // Final `else` block. ast::ExprKind::Block(b, None) => { - self.cbox(0); - self.ibox(0); + let cb = self.cbox(0); + let ib = self.ibox(0); self.word(" else "); - self.print_block(b) + self.print_block(b, cb, ib) } // Constraints would be great here! _ => { @@ -45,12 +45,12 @@ impl<'a> State<'a> { } fn print_if(&mut self, test: &ast::Expr, blk: &ast::Block, elseopt: Option<&ast::Expr>) { - self.cbox(0); - self.ibox(0); + let cb = self.cbox(0); + let ib = self.ibox(0); self.word_nbsp("if"); self.print_expr_as_cond(test); self.space(); - self.print_block(blk); + self.print_block(blk, cb, ib); self.print_else(elseopt) } @@ -112,11 +112,11 @@ impl<'a> State<'a> { } fn print_expr_vec(&mut self, exprs: &[P]) { - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); self.word("["); self.commasep_exprs(Inconsistent, exprs); self.word("]"); - self.end(); + self.end(ib); } pub(super) fn print_expr_anon_const( @@ -124,27 +124,27 @@ impl<'a> State<'a> { expr: &ast::AnonConst, attrs: &[ast::Attribute], ) { - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); self.word("const"); self.nbsp(); if let ast::ExprKind::Block(block, None) = &expr.value.kind { - self.cbox(0); - self.ibox(0); - self.print_block_with_attrs(block, attrs); + let cb = self.cbox(0); + let ib = self.ibox(0); + self.print_block_with_attrs(block, attrs, cb, ib); } else { self.print_expr(&expr.value, FixupContext::default()); } - self.end(); + self.end(ib); } fn print_expr_repeat(&mut self, element: &ast::Expr, count: &ast::AnonConst) { - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); self.word("["); self.print_expr(element, FixupContext::default()); self.word_space(";"); self.print_expr(&count.value, FixupContext::default()); self.word("]"); - self.end(); + self.end(ib); } fn print_expr_struct( @@ -169,7 +169,7 @@ impl<'a> State<'a> { self.word("}"); return; } - self.cbox(0); + let cb = self.cbox(0); for (pos, field) in fields.iter().with_position() { let is_first = matches!(pos, Position::First | Position::Only); let is_last = matches!(pos, Position::Last | Position::Only); @@ -200,7 +200,7 @@ impl<'a> State<'a> { self.space(); } self.offset(-INDENT_UNIT); - self.end(); + self.end(cb); self.word("}"); } @@ -368,7 +368,7 @@ impl<'a> State<'a> { self.print_outer_attributes(attrs); } - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); // The Match subexpression in `match x {} - 1` must be parenthesized if // it is the leftmost subexpression in a statement: @@ -440,14 +440,14 @@ impl<'a> State<'a> { ast::ExprKind::Type(expr, ty) => { self.word("builtin # type_ascribe"); self.popen(); - self.ibox(0); + let ib = self.ibox(0); self.print_expr(expr, FixupContext::default()); self.word(","); self.space_if_not_bol(); self.print_type(ty); - self.end(); + self.end(ib); self.pclose(); } ast::ExprKind::Let(pat, scrutinee, _, _) => { @@ -459,20 +459,20 @@ impl<'a> State<'a> { self.print_ident(label.ident); self.word_space(":"); } - self.cbox(0); - self.ibox(0); + let cb = self.cbox(0); + let ib = self.ibox(0); self.word_nbsp("while"); self.print_expr_as_cond(test); self.space(); - self.print_block_with_attrs(blk, attrs); + self.print_block_with_attrs(blk, attrs, cb, ib); } ast::ExprKind::ForLoop { pat, iter, body, label, kind } => { if let Some(label) = label { self.print_ident(label.ident); self.word_space(":"); } - self.cbox(0); - self.ibox(0); + let cb = self.cbox(0); + let ib = self.ibox(0); self.word_nbsp("for"); if kind == &ForLoopKind::ForAwait { self.word_nbsp("await"); @@ -482,21 +482,21 @@ impl<'a> State<'a> { self.word_space("in"); self.print_expr_as_cond(iter); self.space(); - self.print_block_with_attrs(body, attrs); + self.print_block_with_attrs(body, attrs, cb, ib); } ast::ExprKind::Loop(blk, opt_label, _) => { if let Some(label) = opt_label { self.print_ident(label.ident); self.word_space(":"); } - self.cbox(0); - self.ibox(0); + let cb = self.cbox(0); + let ib = self.ibox(0); self.word_nbsp("loop"); - self.print_block_with_attrs(blk, attrs); + self.print_block_with_attrs(blk, attrs, cb, ib); } ast::ExprKind::Match(expr, arms, match_kind) => { - self.cbox(0); - self.ibox(0); + let cb = self.cbox(0); + let ib = self.ibox(0); match match_kind { MatchKind::Prefix => { @@ -514,13 +514,13 @@ impl<'a> State<'a> { } } - self.bopen(); + self.bopen(ib); self.print_inner_attributes_no_trailing_hardbreak(attrs); for arm in arms { self.print_arm(arm); } let empty = attrs.is_empty() && arms.is_empty(); - self.bclose(expr.span, empty); + self.bclose(expr.span, empty, cb); } ast::ExprKind::Closure(box ast::Closure { binder, @@ -542,12 +542,15 @@ impl<'a> State<'a> { self.print_fn_params_and_ret(fn_decl, true); self.space(); self.print_expr(body, FixupContext::default()); - self.end(); // need to close a box + // FIXME(nnethercote): Bogus. Reduce visibility of `ended` once it's fixed. + let fake_ib = BoxMarker; + self.end(fake_ib); - // a box will be closed by print_expr, but we didn't want an overall + // A box will be closed by print_expr, but we didn't want an overall // wrapper so we closed the corresponding opening. so create an // empty box to satisfy the close. - self.ibox(0); + // FIXME(nnethercote): Bogus. + let _ib = self.ibox(0); } ast::ExprKind::Block(blk, opt_label) => { if let Some(label) = opt_label { @@ -555,18 +558,18 @@ impl<'a> State<'a> { self.word_space(":"); } // containing cbox, will be closed by print-block at } - self.cbox(0); + let cb = self.cbox(0); // head-box, will be closed by print-block after { - self.ibox(0); - self.print_block_with_attrs(blk, attrs); + let ib = self.ibox(0); + self.print_block_with_attrs(blk, attrs, cb, ib); } ast::ExprKind::Gen(capture_clause, blk, kind, _decl_span) => { self.word_nbsp(kind.modifier()); self.print_capture_clause(*capture_clause); // cbox/ibox in analogy to the `ExprKind::Block` arm above - self.cbox(0); - self.ibox(0); - self.print_block_with_attrs(blk, attrs); + let cb = self.cbox(0); + let ib = self.ibox(0); + self.print_block_with_attrs(blk, attrs, cb, ib); } ast::ExprKind::Await(expr, _) => { self.print_expr_cond_paren( @@ -728,19 +731,19 @@ impl<'a> State<'a> { // FIXME: Print `builtin # format_args` once macro `format_args` uses `builtin_syntax`. self.word("format_args!"); self.popen(); - self.ibox(0); + let ib = self.ibox(0); self.word(reconstruct_format_args_template_string(&fmt.template)); for arg in fmt.arguments.all_args() { self.word_space(","); self.print_expr(&arg.expr, FixupContext::default()); } - self.end(); + self.end(ib); self.pclose(); } ast::ExprKind::OffsetOf(container, fields) => { self.word("builtin # offset_of"); self.popen(); - self.ibox(0); + let ib = self.ibox(0); self.print_type(container); self.word(","); self.space(); @@ -753,7 +756,7 @@ impl<'a> State<'a> { self.print_ident(field); } } - self.end(); + self.end(ib); self.pclose(); } ast::ExprKind::MacCall(m) => self.print_mac(m), @@ -791,10 +794,10 @@ impl<'a> State<'a> { self.word("?") } ast::ExprKind::TryBlock(blk) => { - self.cbox(0); - self.ibox(0); + let cb = self.cbox(0); + let ib = self.ibox(0); self.word_nbsp("try"); - self.print_block_with_attrs(blk, attrs) + self.print_block_with_attrs(blk, attrs, cb, ib) } ast::ExprKind::UnsafeBinderCast(kind, expr, ty) => { self.word("builtin # "); @@ -803,7 +806,7 @@ impl<'a> State<'a> { ast::UnsafeBinderCastKind::Unwrap => self.word("unwrap_binder"), } self.popen(); - self.ibox(0); + let ib = self.ibox(0); self.print_expr(expr, FixupContext::default()); if let Some(ty) = ty { @@ -812,7 +815,7 @@ impl<'a> State<'a> { self.print_type(ty); } - self.end(); + self.end(ib); self.pclose(); } ast::ExprKind::Err(_) => { @@ -833,7 +836,7 @@ impl<'a> State<'a> { self.pclose(); } - self.end(); + self.end(ib); } fn print_arm(&mut self, arm: &ast::Arm) { @@ -841,8 +844,8 @@ impl<'a> State<'a> { if arm.attrs.is_empty() { self.space(); } - self.cbox(INDENT_UNIT); - self.ibox(0); + let cb = self.cbox(INDENT_UNIT); + let ib = self.ibox(0); self.maybe_print_comment(arm.pat.span.lo()); self.print_outer_attributes(&arm.attrs); self.print_pat(&arm.pat); @@ -863,8 +866,7 @@ impl<'a> State<'a> { self.word_space(":"); } - // The block will close the pattern's ibox. - self.print_block_unclosed_indent(blk); + self.print_block_unclosed_indent(blk, ib); // If it is a user-provided unsafe block, print a comma after it. if let BlockCheckMode::Unsafe(ast::UserProvided) = blk.rules { @@ -872,16 +874,16 @@ impl<'a> State<'a> { } } _ => { - self.end(); // Close the ibox for the pattern. + self.end(ib); self.print_expr(body, FixupContext::new_match_arm()); self.word(","); } } } else { - self.end(); // Close the ibox for the pattern. + self.end(ib); self.word(","); } - self.end(); // Close enclosing cbox. + self.end(cb); } fn print_closure_binder(&mut self, binder: &ast::ClosureBinder) { diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs index 653bd77cc4dd8..1e02ac8fd5d15 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -5,6 +5,7 @@ use rustc_ast::ModKind; use rustc_ast::ptr::P; use rustc_span::Ident; +use crate::pp::BoxMarker; use crate::pp::Breaks::Inconsistent; use crate::pprust::state::fixup::FixupContext; use crate::pprust::state::{AnnNode, INDENT_UNIT, PrintState, State}; @@ -96,7 +97,7 @@ impl<'a> State<'a> { define_opaque: Option<&[(ast::NodeId, ast::Path)]>, ) { self.print_define_opaques(define_opaque); - self.head(""); + let (cb, ib) = self.head(""); self.print_visibility(vis); self.print_safety(safety); self.print_defaultness(defaultness); @@ -113,14 +114,14 @@ impl<'a> State<'a> { if body.is_some() { self.space(); } - self.end(); // end the head-ibox + self.end(ib); if let Some(body) = body { self.word_space("="); self.print_expr(body, FixupContext::default()); } self.print_where_clause(&generics.where_clause); self.word(";"); - self.end(); // end the outer cbox + self.end(cb); } fn print_associated_type( @@ -135,7 +136,7 @@ impl<'a> State<'a> { ) { let (before_predicates, after_predicates) = generics.where_clause.predicates.split_at(where_clauses.split); - self.head(""); + let (cb, ib) = self.head(""); self.print_visibility(vis); self.print_defaultness(defaultness); self.word_space("type"); @@ -153,8 +154,8 @@ impl<'a> State<'a> { } self.print_where_clause_parts(where_clauses.after.has_where_token, after_predicates); self.word(";"); - self.end(); // end inner head-block - self.end(); // end outer head-block + self.end(ib); + self.end(cb); } /// Pretty-prints an item. @@ -165,7 +166,7 @@ impl<'a> State<'a> { self.ann.pre(self, AnnNode::Item(item)); match &item.kind { ast::ItemKind::ExternCrate(orig_name, ident) => { - self.head(visibility_qualified(&item.vis, "extern crate")); + let (cb, ib) = self.head(visibility_qualified(&item.vis, "extern crate")); if let &Some(orig_name) = orig_name { self.print_name(orig_name); self.space(); @@ -174,8 +175,8 @@ impl<'a> State<'a> { } self.print_ident(*ident); self.word(";"); - self.end(); // end inner head-block - self.end(); // end outer head-block + self.end(ib); + self.end(cb); } ast::ItemKind::Use(tree) => { self.print_visibility(&item.vis); @@ -228,7 +229,7 @@ impl<'a> State<'a> { self.print_fn_full(&item.vis, &item.attrs, &*func); } ast::ItemKind::Mod(safety, ident, mod_kind) => { - self.head(Self::to_string(|s| { + let (cb, ib) = self.head(Self::to_string(|s| { s.print_visibility(&item.vis); s.print_safety(*safety); s.word("mod"); @@ -238,23 +239,23 @@ impl<'a> State<'a> { match mod_kind { ModKind::Loaded(items, ..) => { self.nbsp(); - self.bopen(); + self.bopen(ib); self.print_inner_attributes(&item.attrs); for item in items { self.print_item(item); } let empty = item.attrs.is_empty() && items.is_empty(); - self.bclose(item.span, empty); + self.bclose(item.span, empty, cb); } ModKind::Unloaded => { self.word(";"); - self.end(); // end inner head-block - self.end(); // end outer head-block + self.end(ib); + self.end(cb); } } } ast::ItemKind::ForeignMod(nmod) => { - self.head(Self::to_string(|s| { + let (cb, ib) = self.head(Self::to_string(|s| { s.print_safety(nmod.safety); s.word("extern"); })); @@ -262,18 +263,18 @@ impl<'a> State<'a> { self.print_token_literal(abi.as_token_lit(), abi.span); self.nbsp(); } - self.bopen(); + self.bopen(ib); self.print_foreign_mod(nmod, &item.attrs); let empty = item.attrs.is_empty() && nmod.items.is_empty(); - self.bclose(item.span, empty); + self.bclose(item.span, empty, cb); } ast::ItemKind::GlobalAsm(asm) => { // FIXME: Print `builtin # global_asm` once macro `global_asm` uses `builtin_syntax`. - self.head(visibility_qualified(&item.vis, "global_asm!")); + let (cb, ib) = self.head(visibility_qualified(&item.vis, "global_asm!")); self.print_inline_asm(asm); self.word(";"); - self.end(); - self.end(); + self.end(ib); + self.end(cb); } ast::ItemKind::TyAlias(box ast::TyAlias { defaultness, @@ -297,12 +298,12 @@ impl<'a> State<'a> { self.print_enum_def(enum_definition, params, *ident, item.span, &item.vis); } ast::ItemKind::Struct(ident, struct_def, generics) => { - self.head(visibility_qualified(&item.vis, "struct")); - self.print_struct(struct_def, generics, *ident, item.span, true); + let (cb, ib) = self.head(visibility_qualified(&item.vis, "struct")); + self.print_struct(struct_def, generics, *ident, item.span, true, cb, ib); } ast::ItemKind::Union(ident, struct_def, generics) => { - self.head(visibility_qualified(&item.vis, "union")); - self.print_struct(struct_def, generics, *ident, item.span, true); + let (cb, ib) = self.head(visibility_qualified(&item.vis, "union")); + self.print_struct(struct_def, generics, *ident, item.span, true, cb, ib); } ast::ItemKind::Impl(box ast::Impl { safety, @@ -314,7 +315,7 @@ impl<'a> State<'a> { self_ty, items, }) => { - self.head(""); + let (cb, ib) = self.head(""); self.print_visibility(&item.vis); self.print_defaultness(*defaultness); self.print_safety(*safety); @@ -343,13 +344,13 @@ impl<'a> State<'a> { self.print_where_clause(&generics.where_clause); self.space(); - self.bopen(); + self.bopen(ib); self.print_inner_attributes(&item.attrs); for impl_item in items { self.print_assoc_item(impl_item); } let empty = item.attrs.is_empty() && items.is_empty(); - self.bclose(item.span, empty); + self.bclose(item.span, empty, cb); } ast::ItemKind::Trait(box ast::Trait { safety, @@ -359,7 +360,7 @@ impl<'a> State<'a> { bounds, items, }) => { - self.head(""); + let (cb, ib) = self.head(""); self.print_visibility(&item.vis); self.print_safety(*safety); self.print_is_auto(*is_auto); @@ -372,16 +373,16 @@ impl<'a> State<'a> { } self.print_where_clause(&generics.where_clause); self.word(" "); - self.bopen(); + self.bopen(ib); self.print_inner_attributes(&item.attrs); for trait_item in items { self.print_assoc_item(trait_item); } let empty = item.attrs.is_empty() && items.is_empty(); - self.bclose(item.span, empty); + self.bclose(item.span, empty, cb); } ast::ItemKind::TraitAlias(ident, generics, bounds) => { - self.head(visibility_qualified(&item.vis, "trait")); + let (cb, ib) = self.head(visibility_qualified(&item.vis, "trait")); self.print_ident(*ident); self.print_generic_params(&generics.params); self.nbsp(); @@ -391,8 +392,8 @@ impl<'a> State<'a> { } self.print_where_clause(&generics.where_clause); self.word(";"); - self.end(); // end inner head-block - self.end(); // end outer head-block + self.end(ib); + self.end(cb); } ast::ItemKind::MacCall(mac) => { self.print_mac(mac); @@ -433,28 +434,24 @@ impl<'a> State<'a> { span: rustc_span::Span, visibility: &ast::Visibility, ) { - self.head(visibility_qualified(visibility, "enum")); + let (cb, ib) = self.head(visibility_qualified(visibility, "enum")); self.print_ident(ident); self.print_generic_params(&generics.params); self.print_where_clause(&generics.where_clause); self.space(); - self.print_variants(&enum_definition.variants, span) - } - - fn print_variants(&mut self, variants: &[ast::Variant], span: rustc_span::Span) { - self.bopen(); - for v in variants { + self.bopen(ib); + for v in enum_definition.variants.iter() { self.space_if_not_bol(); self.maybe_print_comment(v.span.lo()); self.print_outer_attributes(&v.attrs); - self.ibox(0); + let ib = self.ibox(0); self.print_variant(v); self.word(","); - self.end(); + self.end(ib); self.maybe_print_trailing_comment(v.span, None); } - let empty = variants.is_empty(); - self.bclose(span, empty) + let empty = enum_definition.variants.is_empty(); + self.bclose(span, empty, cb) } pub(crate) fn print_visibility(&mut self, vis: &ast::Visibility) { @@ -478,33 +475,6 @@ impl<'a> State<'a> { } } - pub(crate) fn print_record_struct_body( - &mut self, - fields: &[ast::FieldDef], - span: rustc_span::Span, - ) { - self.nbsp(); - self.bopen(); - - let empty = fields.is_empty(); - if !empty { - self.hardbreak_if_not_bol(); - - for field in fields { - self.hardbreak_if_not_bol(); - self.maybe_print_comment(field.span.lo()); - self.print_outer_attributes(&field.attrs); - self.print_visibility(&field.vis); - self.print_ident(field.ident.unwrap()); - self.word_nbsp(":"); - self.print_type(&field.ty); - self.word(","); - } - } - - self.bclose(span, empty); - } - fn print_struct( &mut self, struct_def: &ast::VariantData, @@ -512,6 +482,8 @@ impl<'a> State<'a> { ident: Ident, span: rustc_span::Span, print_finalizer: bool, + cb: BoxMarker, + ib: BoxMarker, ) { self.print_ident(ident); self.print_generic_params(&generics.params); @@ -531,21 +503,40 @@ impl<'a> State<'a> { if print_finalizer { self.word(";"); } - self.end(); - self.end(); // Close the outer-box. + self.end(ib); + self.end(cb); } ast::VariantData::Struct { fields, .. } => { self.print_where_clause(&generics.where_clause); - self.print_record_struct_body(fields, span); + self.nbsp(); + self.bopen(ib); + + let empty = fields.is_empty(); + if !empty { + self.hardbreak_if_not_bol(); + + for field in fields { + self.hardbreak_if_not_bol(); + self.maybe_print_comment(field.span.lo()); + self.print_outer_attributes(&field.attrs); + self.print_visibility(&field.vis); + self.print_ident(field.ident.unwrap()); + self.word_nbsp(":"); + self.print_type(&field.ty); + self.word(","); + } + } + + self.bclose(span, empty, cb); } } } pub(crate) fn print_variant(&mut self, v: &ast::Variant) { - self.head(""); + let (cb, ib) = self.head(""); self.print_visibility(&v.vis); let generics = ast::Generics::default(); - self.print_struct(&v.data, &generics, v.ident, v.span, false); + self.print_struct(&v.data, &generics, v.ident, v.span, false, cb, ib); if let Some(d) = &v.disr_expr { self.space(); self.word_space("="); @@ -636,9 +627,7 @@ impl<'a> State<'a> { kind: DelegationKind<'_>, body: &Option>, ) { - if body.is_some() { - self.head(""); - } + let body_cb_ib = body.as_ref().map(|body| (body, self.head(""))); self.print_visibility(vis); self.word_nbsp("reuse"); @@ -670,9 +659,9 @@ impl<'a> State<'a> { self.word("*"); } } - if let Some(body) = body { + if let Some((body, (cb, ib))) = body_cb_ib { self.nbsp(); - self.print_block_with_attrs(body, attrs); + self.print_block_with_attrs(body, attrs, cb, ib); } else { self.word(";"); } @@ -683,9 +672,8 @@ impl<'a> State<'a> { self.print_define_opaques(define_opaque.as_deref()); - if body.is_some() { - self.head(""); - } + let body_cb_ib = body.as_ref().map(|body| (body, self.head(""))); + self.print_visibility(vis); self.print_defaultness(*defaultness); self.print_fn(&sig.decl, sig.header, Some(*ident), generics); @@ -693,9 +681,9 @@ impl<'a> State<'a> { self.nbsp(); self.print_contract(contract); } - if let Some(body) = body { + if let Some((body, (cb, ib))) = body_cb_ib { self.nbsp(); - self.print_block_with_attrs(body, attrs); + self.print_block_with_attrs(body, attrs, cb, ib); } else { self.word(";"); } @@ -851,10 +839,10 @@ impl<'a> State<'a> { } else if let [(item, _)] = items.as_slice() { self.print_use_tree(item); } else { - self.cbox(INDENT_UNIT); + let cb = self.cbox(INDENT_UNIT); self.word("{"); self.zerobreak(); - self.ibox(0); + let ib = self.ibox(0); for (pos, use_tree) in items.iter().with_position() { let is_last = matches!(pos, Position::Last | Position::Only); self.print_use_tree(&use_tree.0); @@ -867,11 +855,11 @@ impl<'a> State<'a> { } } } - self.end(); + self.end(ib); self.trailing_comma(); self.offset(-INDENT_UNIT); self.word("}"); - self.end(); + self.end(cb); } } } diff --git a/compiler/rustc_ast_pretty/src/pprust/tests.rs b/compiler/rustc_ast_pretty/src/pprust/tests.rs index bc7f22766a5ca..786de529c5b89 100644 --- a/compiler/rustc_ast_pretty/src/pprust/tests.rs +++ b/compiler/rustc_ast_pretty/src/pprust/tests.rs @@ -11,10 +11,10 @@ fn fun_to_string( generics: &ast::Generics, ) -> String { to_string(|s| { - s.head(""); + let (cb, ib) = s.head(""); s.print_fn(decl, header, Some(ident), generics); - s.end(); // Close the head box. - s.end(); // Close the outer box. + s.end(ib); + s.end(cb); }) } diff --git a/compiler/rustc_const_eval/src/util/type_name.rs b/compiler/rustc_const_eval/src/util/type_name.rs index e14cd603c5828..30e96ae414359 100644 --- a/compiler/rustc_const_eval/src/util/type_name.rs +++ b/compiler/rustc_const_eval/src/util/type_name.rs @@ -56,7 +56,7 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> { | ty::Coroutine(def_id, args) => self.print_def_path(def_id, args), ty::Foreign(def_id) => self.print_def_path(def_id, &[]), - ty::Alias(ty::Weak, _) => bug!("type_name: unexpected weak projection"), + ty::Alias(ty::Free, _) => bug!("type_name: unexpected free alias"), ty::Alias(ty::Inherent, _) => bug!("type_name: unexpected inherent projection"), ty::CoroutineWitness(..) => bug!("type_name: unexpected `CoroutineWitness`"), } diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index d13fafae4e88a..fa36fe7971610 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -2019,7 +2019,7 @@ fn check_variances_for_type_defn<'tcx>( ItemKind::TyAlias(..) => { assert!( tcx.type_alias_is_lazy(item.owner_id), - "should not be computing variance of non-weak type alias" + "should not be computing variance of non-free type alias" ); } kind => span_bug!(item.span, "cannot compute the variances of {kind:?}"), @@ -2251,7 +2251,7 @@ impl<'tcx> TypeVisitor> for IsProbablyCyclical<'tcx> { fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<(), ()> { let def_id = match ty.kind() { ty::Adt(adt_def, _) => Some(adt_def.did()), - ty::Alias(ty::Weak, alias_ty) => Some(alias_ty.def_id), + ty::Alias(ty::Free, alias_ty) => Some(alias_ty.def_id), _ => None, }; if let Some(def_id) = def_id { diff --git a/compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs b/compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs index c9a9180c5c9dc..bd25b4a326086 100644 --- a/compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs +++ b/compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs @@ -150,7 +150,7 @@ impl<'tcx> InherentCollect<'tcx> { let id = id.owner_id.def_id; let item_span = self.tcx.def_span(id); let self_ty = self.tcx.type_of(id).instantiate_identity(); - let mut self_ty = self.tcx.peel_off_weak_alias_tys(self_ty); + let mut self_ty = self.tcx.peel_off_free_alias_tys(self_ty); // We allow impls on pattern types exactly when we allow impls on the base type. // FIXME(pattern_types): Figure out the exact coherence rules we want here. while let ty::Pat(base, _) = *self_ty.kind() { @@ -188,7 +188,7 @@ impl<'tcx> InherentCollect<'tcx> { | ty::CoroutineClosure(..) | ty::Coroutine(..) | ty::CoroutineWitness(..) - | ty::Alias(ty::Weak, _) + | ty::Alias(ty::Free, _) | ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) => { diff --git a/compiler/rustc_hir_analysis/src/coherence/orphan.rs b/compiler/rustc_hir_analysis/src/coherence/orphan.rs index 74ba4ffe25ea1..c75fef9f716d6 100644 --- a/compiler/rustc_hir_analysis/src/coherence/orphan.rs +++ b/compiler/rustc_hir_analysis/src/coherence/orphan.rs @@ -189,7 +189,7 @@ pub(crate) fn orphan_check_impl( ty::Projection => "associated type", // type Foo = (impl Sized, bool) // impl AutoTrait for Foo {} - ty::Weak => "type alias", + ty::Free => "type alias", // type Opaque = impl Trait; // impl AutoTrait for Opaque {} ty::Opaque => "opaque type", diff --git a/compiler/rustc_hir_analysis/src/constrained_generic_params.rs b/compiler/rustc_hir_analysis/src/constrained_generic_params.rs index 951eda72ffeda..366b3943a0589 100644 --- a/compiler/rustc_hir_analysis/src/constrained_generic_params.rs +++ b/compiler/rustc_hir_analysis/src/constrained_generic_params.rs @@ -49,7 +49,7 @@ pub(crate) fn parameters_for<'tcx>( include_nonconstraining: bool, ) -> Vec { let mut collector = ParameterCollector { parameters: vec![], include_nonconstraining }; - let value = if !include_nonconstraining { tcx.expand_weak_alias_tys(value) } else { value }; + let value = if !include_nonconstraining { tcx.expand_free_alias_tys(value) } else { value }; value.visit_with(&mut collector); collector.parameters } @@ -68,9 +68,9 @@ impl<'tcx> TypeVisitor> for ParameterCollector { { return; } - // All weak alias types should've been expanded beforehand. - ty::Alias(ty::Weak, _) if !self.include_nonconstraining => { - bug!("unexpected weak alias type") + // All free alias types should've been expanded beforehand. + ty::Alias(ty::Free, _) if !self.include_nonconstraining => { + bug!("unexpected free alias type") } ty::Param(param) => self.parameters.push(Parameter::from(param)), _ => {} diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index 22162b8b3649a..8153e6f87f8ab 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -958,7 +958,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { // feature `lazy_type_alias` enabled get encoded as a type alias that normalization will // then actually instantiate the where bounds of. let alias_ty = ty::AliasTy::new_from_args(tcx, did, args); - Ty::new_alias(tcx, ty::Weak, alias_ty) + Ty::new_alias(tcx, ty::Free, alias_ty) } else { tcx.at(span).type_of(did).instantiate(tcx, args) } diff --git a/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs b/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs index 780c27d45954d..c99eb12efcca2 100644 --- a/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs +++ b/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs @@ -157,10 +157,10 @@ fn insert_required_predicates_to_be_wf<'tcx>( ); } - ty::Alias(ty::Weak, alias) => { + ty::Alias(ty::Free, alias) => { // This corresponds to a type like `Type<'a, T>`. // We check inferred and explicit predicates. - debug!("Weak"); + debug!("Free"); check_inferred_predicates( tcx, alias.def_id, diff --git a/compiler/rustc_hir_analysis/src/variance/constraints.rs b/compiler/rustc_hir_analysis/src/variance/constraints.rs index 23223de918cfc..dc3ce1dd76c42 100644 --- a/compiler/rustc_hir_analysis/src/variance/constraints.rs +++ b/compiler/rustc_hir_analysis/src/variance/constraints.rs @@ -107,7 +107,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { let current_item = &CurrentItem { inferred_start }; let ty = tcx.type_of(def_id).instantiate_identity(); - // The type as returned by `type_of` is the underlying type and generally not a weak projection. + // The type as returned by `type_of` is the underlying type and generally not a free alias. // Therefore we need to check the `DefKind` first. if let DefKind::TyAlias = tcx.def_kind(def_id) && tcx.type_alias_is_lazy(def_id) @@ -282,7 +282,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { self.add_constraints_from_invariant_args(current, data.args, variance); } - ty::Alias(ty::Weak, ref data) => { + ty::Alias(ty::Free, ref data) => { self.add_constraints_from_args(current, data.def_id, data.args, variance); } diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index c95d6a277c71f..b878147522dc8 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -13,7 +13,7 @@ use rustc_abi::ExternAbi; use rustc_ast::util::parser::{self, ExprPrecedence, Fixity}; use rustc_ast::{AttrStyle, DUMMY_NODE_ID, DelimArgs}; use rustc_ast_pretty::pp::Breaks::{Consistent, Inconsistent}; -use rustc_ast_pretty::pp::{self, Breaks}; +use rustc_ast_pretty::pp::{self, BoxMarker, Breaks}; use rustc_ast_pretty::pprust::state::MacHeader; use rustc_ast_pretty::pprust::{Comments, PrintState}; use rustc_attr_data_structures::{AttributeKind, PrintAttribute}; @@ -127,7 +127,7 @@ impl<'a> State<'a> { } fn print_attr_item(&mut self, item: &hir::AttrItem, span: Span) { - self.ibox(0); + let ib = self.ibox(0); let path = ast::Path { span, segments: item @@ -161,7 +161,7 @@ impl<'a> State<'a> { self.word(token_str); } } - self.end(); + self.end(ib); } fn print_node(&mut self, node: Node<'_>) { @@ -192,10 +192,10 @@ impl<'a> State<'a> { Node::PreciseCapturingNonLifetimeArg(param) => self.print_ident(param.ident), Node::Block(a) => { // Containing cbox, will be closed by print-block at `}`. - self.cbox(INDENT_UNIT); + let cb = self.cbox(INDENT_UNIT); // Head-ibox, will be closed by print-block after `{`. - self.ibox(0); - self.print_block(a); + let ib = self.ibox(0); + self.print_block(a, cb, ib); } Node::Lifetime(a) => self.print_lifetime(a), Node::GenericParam(_) => panic!("cannot print Node::GenericParam"), @@ -315,17 +315,17 @@ pub fn item_to_string(ann: &dyn PpAnn, pat: &hir::Item<'_>) -> String { } impl<'a> State<'a> { - fn bclose_maybe_open(&mut self, span: rustc_span::Span, close_box: bool) { + fn bclose_maybe_open(&mut self, span: rustc_span::Span, cb: Option) { self.maybe_print_comment(span.hi()); self.break_offset_if_not_bol(1, -INDENT_UNIT); self.word("}"); - if close_box { - self.end(); // close the outer-box + if let Some(cb) = cb { + self.end(cb); } } - fn bclose(&mut self, span: rustc_span::Span) { - self.bclose_maybe_open(span, true) + fn bclose(&mut self, span: rustc_span::Span, cb: BoxMarker) { + self.bclose_maybe_open(span, Some(cb)) } fn commasep_cmnt(&mut self, b: Breaks, elts: &[T], mut op: F, mut get_span: G) @@ -333,7 +333,7 @@ impl<'a> State<'a> { F: FnMut(&mut State<'_>, &T), G: FnMut(&T) -> rustc_span::Span, { - self.rbox(0, b); + let rb = self.rbox(0, b); let len = elts.len(); let mut i = 0; for elt in elts { @@ -346,7 +346,7 @@ impl<'a> State<'a> { self.space_if_not_bol(); } } - self.end(); + self.end(rb); } fn commasep_exprs(&mut self, b: Breaks, exprs: &[hir::Expr<'_>]) { @@ -369,7 +369,7 @@ impl<'a> State<'a> { fn print_type(&mut self, ty: &hir::Ty<'_>) { self.maybe_print_comment(ty.span.lo()); - self.ibox(0); + let ib = self.ibox(0); match ty.kind { hir::TyKind::Slice(ty) => { self.word("["); @@ -456,16 +456,16 @@ impl<'a> State<'a> { self.print_ty_pat(pat); } } - self.end() + self.end(ib) } fn print_unsafe_binder(&mut self, unsafe_binder: &hir::UnsafeBinderTy<'_>) { - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); self.word("unsafe"); self.print_generic_params(unsafe_binder.generic_params); self.nbsp(); self.print_type(unsafe_binder.inner_ty); - self.end(); + self.end(ib); } fn print_foreign_item(&mut self, item: &hir::ForeignItem<'_>) { @@ -474,7 +474,7 @@ impl<'a> State<'a> { self.print_attrs_as_outer(self.attrs(item.hir_id())); match item.kind { hir::ForeignItemKind::Fn(sig, arg_idents, generics) => { - self.head(""); + let (cb, ib) = self.head(""); self.print_fn( sig.decl, sig.header, @@ -483,13 +483,13 @@ impl<'a> State<'a> { arg_idents, None, ); - self.end(); // end head-ibox + self.end(ib); self.word(";"); - self.end() // end the outer fn box + self.end(cb) } hir::ForeignItemKind::Static(t, m, safety) => { self.print_safety(safety); - self.head("static"); + let (cb, ib) = self.head("static"); if m.is_mut() { self.word_space("mut"); } @@ -497,15 +497,15 @@ impl<'a> State<'a> { self.word_space(":"); self.print_type(t); self.word(";"); - self.end(); // end the head-ibox - self.end() // end the outer cbox + self.end(ib); + self.end(cb) } hir::ForeignItemKind::Type => { - self.head("type"); + let (cb, ib) = self.head("type"); self.print_ident(item.ident); self.word(";"); - self.end(); // end the head-ibox - self.end() // end the outer cbox + self.end(ib); + self.end(cb) } } } @@ -561,7 +561,7 @@ impl<'a> State<'a> { self.ann.pre(self, AnnNode::Item(item)); match item.kind { hir::ItemKind::ExternCrate(orig_name, ident) => { - self.head("extern crate"); + let (cb, ib) = self.head("extern crate"); if let Some(orig_name) = orig_name { self.print_name(orig_name); self.space(); @@ -570,11 +570,11 @@ impl<'a> State<'a> { } self.print_ident(ident); self.word(";"); - self.end(); // end inner head-block - self.end(); // end outer head-block + self.end(ib); + self.end(cb); } hir::ItemKind::Use(path, kind) => { - self.head("use"); + let (cb, ib) = self.head("use"); self.print_path(path, false); match kind { @@ -589,11 +589,11 @@ impl<'a> State<'a> { hir::UseKind::Glob => self.word("::*;"), hir::UseKind::ListStem => self.word("::{};"), } - self.end(); // end inner head-block - self.end(); // end outer head-block + self.end(ib); + self.end(cb); } hir::ItemKind::Static(ident, ty, m, expr) => { - self.head("static"); + let (cb, ib) = self.head("static"); if m.is_mut() { self.word_space("mut"); } @@ -601,85 +601,86 @@ impl<'a> State<'a> { self.word_space(":"); self.print_type(ty); self.space(); - self.end(); // end the head-ibox + self.end(ib); self.word_space("="); self.ann.nested(self, Nested::Body(expr)); self.word(";"); - self.end(); // end the outer cbox + self.end(cb); } hir::ItemKind::Const(ident, ty, generics, expr) => { - self.head("const"); + let (cb, ib) = self.head("const"); self.print_ident(ident); self.print_generic_params(generics.params); self.word_space(":"); self.print_type(ty); self.space(); - self.end(); // end the head-ibox + self.end(ib); self.word_space("="); self.ann.nested(self, Nested::Body(expr)); self.print_where_clause(generics); self.word(";"); - self.end(); // end the outer cbox + self.end(cb); } hir::ItemKind::Fn { ident, sig, generics, body, .. } => { - self.head(""); + let (cb, ib) = self.head(""); self.print_fn(sig.decl, sig.header, Some(ident.name), generics, &[], Some(body)); self.word(" "); - self.end(); // need to close a box - self.end(); // need to close a box + self.end(ib); + self.end(cb); self.ann.nested(self, Nested::Body(body)); } hir::ItemKind::Macro(ident, macro_def, _) => { self.print_mac_def(macro_def, &ident, item.span, |_| {}); } hir::ItemKind::Mod(ident, mod_) => { - self.head("mod"); + let (cb, ib) = self.head("mod"); self.print_ident(ident); self.nbsp(); - self.bopen(); + self.bopen(ib); self.print_mod(mod_, attrs); - self.bclose(item.span); + self.bclose(item.span, cb); } hir::ItemKind::ForeignMod { abi, items } => { - self.head("extern"); + let (cb, ib) = self.head("extern"); self.word_nbsp(abi.to_string()); - self.bopen(); + self.bopen(ib); self.print_attrs_as_inner(self.attrs(item.hir_id())); for item in items { self.ann.nested(self, Nested::ForeignItem(item.id)); } - self.bclose(item.span); + self.bclose(item.span, cb); } hir::ItemKind::GlobalAsm { asm, .. } => { - self.head("global_asm!"); + // FIXME(nnethercote): `ib` is unclosed + let (cb, _ib) = self.head("global_asm!"); self.print_inline_asm(asm); - self.end() + self.end(cb) } hir::ItemKind::TyAlias(ident, ty, generics) => { - self.head("type"); + let (cb, ib) = self.head("type"); self.print_ident(ident); self.print_generic_params(generics.params); - self.end(); // end the inner ibox + self.end(ib); self.print_where_clause(generics); self.space(); self.word_space("="); self.print_type(ty); self.word(";"); - self.end(); // end the outer ibox + self.end(cb); } hir::ItemKind::Enum(ident, ref enum_definition, params) => { self.print_enum_def(enum_definition, params, ident.name, item.span); } hir::ItemKind::Struct(ident, ref struct_def, generics) => { - self.head("struct"); - self.print_struct(struct_def, generics, ident.name, item.span, true); + let (cb, ib) = self.head("struct"); + self.print_struct(struct_def, generics, ident.name, item.span, true, cb, ib); } hir::ItemKind::Union(ident, ref struct_def, generics) => { - self.head("union"); - self.print_struct(struct_def, generics, ident.name, item.span, true); + let (cb, ib) = self.head("union"); + self.print_struct(struct_def, generics, ident.name, item.span, true, cb, ib); } hir::ItemKind::Impl(&hir::Impl { constness, @@ -692,7 +693,7 @@ impl<'a> State<'a> { self_ty, items, }) => { - self.head(""); + let (cb, ib) = self.head(""); self.print_defaultness(defaultness); self.print_safety(safety); self.word_nbsp("impl"); @@ -720,15 +721,15 @@ impl<'a> State<'a> { self.print_where_clause(generics); self.space(); - self.bopen(); + self.bopen(ib); self.print_attrs_as_inner(attrs); for impl_item in items { self.ann.nested(self, Nested::ImplItem(impl_item.id)); } - self.bclose(item.span); + self.bclose(item.span, cb); } hir::ItemKind::Trait(is_auto, safety, ident, generics, bounds, trait_items) => { - self.head(""); + let (cb, ib) = self.head(""); self.print_is_auto(is_auto); self.print_safety(safety); self.word_nbsp("trait"); @@ -737,22 +738,22 @@ impl<'a> State<'a> { self.print_bounds(":", bounds); self.print_where_clause(generics); self.word(" "); - self.bopen(); + self.bopen(ib); for trait_item in trait_items { self.ann.nested(self, Nested::TraitItem(trait_item.id)); } - self.bclose(item.span); + self.bclose(item.span, cb); } hir::ItemKind::TraitAlias(ident, generics, bounds) => { - self.head("trait"); + let (cb, ib) = self.head("trait"); self.print_ident(ident); self.print_generic_params(generics.params); self.nbsp(); self.print_bounds("=", bounds); self.print_where_clause(generics); self.word(";"); - self.end(); // end inner head-block - self.end(); // end outer head-block + self.end(ib); + self.end(cb); } } self.ann.post(self, AnnNode::Item(item)) @@ -763,7 +764,8 @@ impl<'a> State<'a> { } fn print_opaque_ty(&mut self, o: &hir::OpaqueTy<'_>) { - self.head("opaque"); + // FIXME(nnethercote): `cb` and `ib` are unclosed + let (_cb, _ib) = self.head("opaque"); self.word("{"); self.print_bounds("impl", o.bounds); self.word("}"); @@ -800,27 +802,33 @@ impl<'a> State<'a> { name: Symbol, span: rustc_span::Span, ) { - self.head("enum"); + let (cb, ib) = self.head("enum"); self.print_name(name); self.print_generic_params(generics.params); self.print_where_clause(generics); self.space(); - self.print_variants(enum_definition.variants, span); + self.print_variants(enum_definition.variants, span, cb, ib); } - fn print_variants(&mut self, variants: &[hir::Variant<'_>], span: rustc_span::Span) { - self.bopen(); + fn print_variants( + &mut self, + variants: &[hir::Variant<'_>], + span: rustc_span::Span, + cb: BoxMarker, + ib: BoxMarker, + ) { + self.bopen(ib); for v in variants { self.space_if_not_bol(); self.maybe_print_comment(v.span.lo()); self.print_attrs_as_outer(self.attrs(v.hir_id)); - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); self.print_variant(v); self.word(","); - self.end(); + self.end(ib); self.maybe_print_trailing_comment(v.span, None); } - self.bclose(span) + self.bclose(span, cb) } fn print_defaultness(&mut self, defaultness: hir::Defaultness) { @@ -837,6 +845,8 @@ impl<'a> State<'a> { name: Symbol, span: rustc_span::Span, print_finalizer: bool, + cb: BoxMarker, + ib: BoxMarker, ) { self.print_name(name); self.print_generic_params(generics.params); @@ -855,38 +865,34 @@ impl<'a> State<'a> { if print_finalizer { self.word(";"); } - self.end(); - self.end() // close the outer-box + self.end(ib); + self.end(cb); } hir::VariantData::Struct { .. } => { self.print_where_clause(generics); - self.print_variant_struct(span, struct_def.fields()) - } - } - } - - fn print_variant_struct(&mut self, span: rustc_span::Span, fields: &[hir::FieldDef<'_>]) { - self.nbsp(); - self.bopen(); - self.hardbreak_if_not_bol(); + self.nbsp(); + self.bopen(ib); + self.hardbreak_if_not_bol(); + + for field in struct_def.fields() { + self.hardbreak_if_not_bol(); + self.maybe_print_comment(field.span.lo()); + self.print_attrs_as_outer(self.attrs(field.hir_id)); + self.print_ident(field.ident); + self.word_nbsp(":"); + self.print_type(field.ty); + self.word(","); + } - for field in fields { - self.hardbreak_if_not_bol(); - self.maybe_print_comment(field.span.lo()); - self.print_attrs_as_outer(self.attrs(field.hir_id)); - self.print_ident(field.ident); - self.word_nbsp(":"); - self.print_type(field.ty); - self.word(","); + self.bclose(span, cb) + } } - - self.bclose(span) } pub fn print_variant(&mut self, v: &hir::Variant<'_>) { - self.head(""); + let (cb, ib) = self.head(""); let generics = hir::Generics::empty(); - self.print_struct(&v.data, generics, v.ident.name, v.span, false); + self.print_struct(&v.data, generics, v.ident.name, v.span, false, cb, ib); if let Some(ref d) = v.disr_expr { self.space(); self.word_space("="); @@ -919,11 +925,11 @@ impl<'a> State<'a> { self.word(";"); } hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => { - self.head(""); + let (cb, ib) = self.head(""); self.print_method_sig(ti.ident, sig, ti.generics, &[], Some(body)); self.nbsp(); - self.end(); // need to close a box - self.end(); // need to close a box + self.end(ib); + self.end(cb); self.ann.nested(self, Nested::Body(body)); } hir::TraitItemKind::Type(bounds, default) => { @@ -944,11 +950,11 @@ impl<'a> State<'a> { self.print_associated_const(ii.ident, ii.generics, ty, Some(expr)); } hir::ImplItemKind::Fn(ref sig, body) => { - self.head(""); + let (cb, ib) = self.head(""); self.print_method_sig(ii.ident, sig, ii.generics, &[], Some(body)); self.nbsp(); - self.end(); // need to close a box - self.end(); // need to close a box + self.end(ib); + self.end(cb); self.ann.nested(self, Nested::Body(body)); } hir::ImplItemKind::Type(ty) => { @@ -966,15 +972,15 @@ impl<'a> State<'a> { decl: impl Fn(&mut Self), ) { self.space_if_not_bol(); - self.ibox(INDENT_UNIT); + let ibm1 = self.ibox(INDENT_UNIT); if super_ { self.word_nbsp("super"); } self.word_nbsp("let"); - self.ibox(INDENT_UNIT); + let ibm2 = self.ibox(INDENT_UNIT); decl(self); - self.end(); + self.end(ibm2); if let Some(init) = init { self.nbsp(); @@ -986,13 +992,13 @@ impl<'a> State<'a> { self.nbsp(); self.word_space("else"); // containing cbox, will be closed by print-block at `}` - self.cbox(0); + let cb = self.cbox(0); // head-box, will be closed by print-block after `{` - self.ibox(0); - self.print_block(els); + let ib = self.ibox(0); + self.print_block(els, cb, ib); } - self.end() + self.end(ibm1) } fn print_stmt(&mut self, st: &hir::Stmt<'_>) { @@ -1020,23 +1026,30 @@ impl<'a> State<'a> { self.maybe_print_trailing_comment(st.span, None) } - fn print_block(&mut self, blk: &hir::Block<'_>) { - self.print_block_with_attrs(blk, &[]) + fn print_block(&mut self, blk: &hir::Block<'_>, cb: BoxMarker, ib: BoxMarker) { + self.print_block_with_attrs(blk, &[], cb, ib) } - fn print_block_unclosed(&mut self, blk: &hir::Block<'_>) { - self.print_block_maybe_unclosed(blk, &[], false) + fn print_block_unclosed(&mut self, blk: &hir::Block<'_>, ib: BoxMarker) { + self.print_block_maybe_unclosed(blk, &[], None, ib) } - fn print_block_with_attrs(&mut self, blk: &hir::Block<'_>, attrs: &[hir::Attribute]) { - self.print_block_maybe_unclosed(blk, attrs, true) + fn print_block_with_attrs( + &mut self, + blk: &hir::Block<'_>, + attrs: &[hir::Attribute], + cb: BoxMarker, + ib: BoxMarker, + ) { + self.print_block_maybe_unclosed(blk, attrs, Some(cb), ib) } fn print_block_maybe_unclosed( &mut self, blk: &hir::Block<'_>, attrs: &[hir::Attribute], - close_box: bool, + cb: Option, + ib: BoxMarker, ) { match blk.rules { hir::BlockCheckMode::UnsafeBlock(..) => self.word_space("unsafe"), @@ -1044,7 +1057,7 @@ impl<'a> State<'a> { } self.maybe_print_comment(blk.span.lo()); self.ann.pre(self, AnnNode::Block(blk)); - self.bopen(); + self.bopen(ib); self.print_attrs_as_inner(attrs); @@ -1056,7 +1069,7 @@ impl<'a> State<'a> { self.print_expr(expr); self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi())); } - self.bclose_maybe_open(blk.span, close_box); + self.bclose_maybe_open(blk.span, cb); self.ann.post(self, AnnNode::Block(blk)) } @@ -1065,20 +1078,20 @@ impl<'a> State<'a> { match els_inner.kind { // Another `else if` block. hir::ExprKind::If(i, hir::Expr { kind: hir::ExprKind::Block(t, None), .. }, e) => { - self.cbox(0); - self.ibox(0); + let cb = self.cbox(0); + let ib = self.ibox(0); self.word(" else if "); self.print_expr_as_cond(i); self.space(); - self.print_block(t); + self.print_block(t, cb, ib); self.print_else(e); } // Final `else` block. hir::ExprKind::Block(b, None) => { - self.cbox(0); - self.ibox(0); + let cb = self.cbox(0); + let ib = self.ibox(0); self.word(" else "); - self.print_block(b); + self.print_block(b, cb, ib); } // Constraints would be great here! _ => { @@ -1094,14 +1107,14 @@ impl<'a> State<'a> { blk: &hir::Expr<'_>, elseopt: Option<&hir::Expr<'_>>, ) { - self.cbox(0); - self.ibox(0); - self.word_nbsp("if"); - self.print_expr_as_cond(test); - self.space(); match blk.kind { hir::ExprKind::Block(blk, None) => { - self.print_block(blk); + let cb = self.cbox(0); + let ib = self.ibox(0); + self.word_nbsp("if"); + self.print_expr_as_cond(test); + self.space(); + self.print_block(blk, cb, ib); self.print_else(elseopt) } _ => panic!("non-block then expr"), @@ -1175,28 +1188,28 @@ impl<'a> State<'a> { } fn print_expr_vec(&mut self, exprs: &[hir::Expr<'_>]) { - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); self.word("["); self.commasep_exprs(Inconsistent, exprs); self.word("]"); - self.end() + self.end(ib) } fn print_inline_const(&mut self, constant: &hir::ConstBlock) { - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); self.word_space("const"); self.ann.nested(self, Nested::Body(constant.body)); - self.end() + self.end(ib) } fn print_expr_repeat(&mut self, element: &hir::Expr<'_>, count: &hir::ConstArg<'_>) { - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); self.word("["); self.print_expr(element); self.word_space(";"); self.print_const_arg(count); self.word("]"); - self.end() + self.end(ib) } fn print_expr_struct( @@ -1211,23 +1224,23 @@ impl<'a> State<'a> { self.commasep_cmnt(Consistent, fields, |s, field| s.print_expr_field(field), |f| f.span); match wth { hir::StructTailExpr::Base(expr) => { - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); if !fields.is_empty() { self.word(","); self.space(); } self.word(".."); self.print_expr(expr); - self.end(); + self.end(ib); } hir::StructTailExpr::DefaultFields(_) => { - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); if !fields.is_empty() { self.word(","); self.space(); } self.word(".."); - self.end(); + self.end(ib); } hir::StructTailExpr::None => {} } @@ -1236,14 +1249,14 @@ impl<'a> State<'a> { } fn print_expr_field(&mut self, field: &hir::ExprField<'_>) { - self.cbox(INDENT_UNIT); + let cb = self.cbox(INDENT_UNIT); self.print_attrs_as_outer(self.attrs(field.hir_id)); if !field.is_shorthand { self.print_ident(field.ident); self.word_space(":"); } self.print_expr(field.expr); - self.end() + self.end(cb) } fn print_expr_tup(&mut self, exprs: &[hir::Expr<'_>]) { @@ -1416,8 +1429,8 @@ impl<'a> State<'a> { s.print_qpath(path, true); } hir::InlineAsmOperand::Label { block } => { - s.head("label"); - s.print_block(block); + let (cb, ib) = s.head("label"); + s.print_block(block, cb, ib); } }, AsmArg::Options(opts) => { @@ -1435,7 +1448,7 @@ impl<'a> State<'a> { fn print_expr(&mut self, expr: &hir::Expr<'_>) { self.maybe_print_comment(expr.span.lo()); self.print_attrs_as_outer(self.attrs(expr.hir_id)); - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); self.ann.pre(self, AnnNode::Expr(expr)); match expr.kind { hir::ExprKind::Array(exprs) => { @@ -1483,21 +1496,21 @@ impl<'a> State<'a> { } hir::ExprKind::Type(expr, ty) => { self.word("type_ascribe!("); - self.ibox(0); + let ib = self.ibox(0); self.print_expr(expr); self.word(","); self.space_if_not_bol(); self.print_type(ty); - self.end(); + self.end(ib); self.word(")"); } hir::ExprKind::DropTemps(init) => { // Print `{`: - self.cbox(INDENT_UNIT); - self.ibox(0); - self.bopen(); + let cb = self.cbox(INDENT_UNIT); + let ib = self.ibox(0); + self.bopen(ib); // Print `let _t = $init;`: let temp = Ident::from_str("_t"); @@ -1509,7 +1522,7 @@ impl<'a> State<'a> { self.print_ident(temp); // Print `}`: - self.bclose_maybe_open(expr.span, true); + self.bclose_maybe_open(expr.span, Some(cb)); } hir::ExprKind::Let(&hir::LetExpr { pat, ty, init, .. }) => { self.print_let(pat, ty, init); @@ -1522,20 +1535,20 @@ impl<'a> State<'a> { self.print_ident(label.ident); self.word_space(":"); } - self.head("loop"); - self.print_block(blk); + let (cb, ib) = self.head("loop"); + self.print_block(blk, cb, ib); } hir::ExprKind::Match(expr, arms, _) => { - self.cbox(INDENT_UNIT); - self.ibox(INDENT_UNIT); + let cb = self.cbox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); self.word_nbsp("match"); self.print_expr_as_cond(expr); self.space(); - self.bopen(); + self.bopen(ib); for arm in arms { self.print_arm(arm); } - self.bclose(expr.span); + self.bclose(expr.span, cb); } hir::ExprKind::Closure(&hir::Closure { binder, @@ -1558,12 +1571,15 @@ impl<'a> State<'a> { // This is a bare expression. self.ann.nested(self, Nested::Body(body)); - self.end(); // need to close a box + // FIXME(nnethercote): this is bogus + let fake_ib = BoxMarker; + self.end(fake_ib); // A box will be closed by `print_expr`, but we didn't want an overall // wrapper so we closed the corresponding opening. so create an // empty box to satisfy the close. - self.ibox(0); + // FIXME(nnethercote): this is bogus, and `print_expr` is missing + let _ib = self.ibox(0); } hir::ExprKind::Block(blk, opt_label) => { if let Some(label) = opt_label { @@ -1571,10 +1587,10 @@ impl<'a> State<'a> { self.word_space(":"); } // containing cbox, will be closed by print-block at `}` - self.cbox(0); + let cb = self.cbox(0); // head-box, will be closed by print-block after `{` - self.ibox(0); - self.print_block(blk); + let ib = self.ibox(0); + self.print_block(blk, cb, ib); } hir::ExprKind::Assign(lhs, rhs, _) => { self.print_expr_cond_paren(lhs, lhs.precedence() <= ExprPrecedence::Assign); @@ -1675,7 +1691,7 @@ impl<'a> State<'a> { } } self.ann.post(self, AnnNode::Expr(expr)); - self.end() + self.end(ib) } fn print_local_decl(&mut self, loc: &hir::LetStmt<'_>) { @@ -2043,14 +2059,14 @@ impl<'a> State<'a> { if self.attrs(field.hir_id).is_empty() { self.space(); } - self.cbox(INDENT_UNIT); + let cb = self.cbox(INDENT_UNIT); self.print_attrs_as_outer(self.attrs(field.hir_id)); if !field.is_shorthand { self.print_ident(field.ident); self.word_nbsp(":"); } self.print_pat(field.pat); - self.end(); + self.end(cb); } fn print_param(&mut self, arg: &hir::Param<'_>) { @@ -2086,9 +2102,9 @@ impl<'a> State<'a> { if self.attrs(arm.hir_id).is_empty() { self.space(); } - self.cbox(INDENT_UNIT); + let cb = self.cbox(INDENT_UNIT); self.ann.pre(self, AnnNode::Arm(arm)); - self.ibox(0); + let ib = self.ibox(0); self.print_attrs_as_outer(self.attrs(arm.hir_id)); self.print_pat(arm.pat); self.space(); @@ -2105,8 +2121,7 @@ impl<'a> State<'a> { self.print_ident(label.ident); self.word_space(":"); } - // the block will close the pattern's ibox - self.print_block_unclosed(blk); + self.print_block_unclosed(blk, ib); // If it is a user-provided unsafe block, print a comma after it if let hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::UserProvided) = blk.rules @@ -2115,13 +2130,13 @@ impl<'a> State<'a> { } } _ => { - self.end(); // close the ibox for the pattern + self.end(ib); self.print_expr(arm.body); self.word(","); } } self.ann.post(self, AnnNode::Arm(arm)); - self.end() // close enclosing cbox + self.end(cb) } fn print_fn( @@ -2167,9 +2182,9 @@ impl<'a> State<'a> { i += 1; }; self.commasep(Inconsistent, decl.inputs, |s, ty| { - s.ibox(INDENT_UNIT); + let ib = s.ibox(INDENT_UNIT); print_arg(s, Some(ty)); - s.end(); + s.end(ib); }); if decl.c_variadic { if !decl.inputs.is_empty() { @@ -2188,7 +2203,7 @@ impl<'a> State<'a> { self.word("|"); let mut i = 0; self.commasep(Inconsistent, decl.inputs, |s, ty| { - s.ibox(INDENT_UNIT); + let ib = s.ibox(INDENT_UNIT); s.ann.nested(s, Nested::BodyParamPat(body_id, i)); i += 1; @@ -2200,7 +2215,7 @@ impl<'a> State<'a> { s.space(); s.print_type(ty); } - s.end(); + s.end(ib); }); self.word("|"); @@ -2442,16 +2457,16 @@ impl<'a> State<'a> { match decl.output { hir::FnRetTy::Return(ty) => { self.space_if_not_bol(); - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); self.word_space("->"); self.print_type(ty); - } - hir::FnRetTy::DefaultReturn(..) => return, - } - self.end(); + self.end(ib); - if let hir::FnRetTy::Return(output) = decl.output { - self.maybe_print_comment(output.span.lo()); + if let hir::FnRetTy::Return(output) = decl.output { + self.maybe_print_comment(output.span.lo()); + } + } + hir::FnRetTy::DefaultReturn(..) => {} } } @@ -2464,7 +2479,7 @@ impl<'a> State<'a> { generic_params: &[hir::GenericParam<'_>], arg_idents: &[Option], ) { - self.ibox(INDENT_UNIT); + let ib = self.ibox(INDENT_UNIT); self.print_formal_generic_params(generic_params); let generics = hir::Generics::empty(); self.print_fn( @@ -2480,7 +2495,7 @@ impl<'a> State<'a> { arg_idents, None, ); - self.end(); + self.end(ib); } fn print_fn_header_info(&mut self, header: hir::FnHeader) { diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs index 934820eb4dafb..de189b301092c 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs @@ -337,7 +337,7 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> { match ty.kind() { ty::Adt(adt_def, _) => Some(*adt_def), // FIXME(#104767): Should we handle bound regions here? - ty::Alias(ty::Projection | ty::Inherent | ty::Weak, _) + ty::Alias(ty::Projection | ty::Inherent | ty::Free, _) if !ty.has_escaping_bound_vars() => { if self.next_trait_solver() { @@ -357,7 +357,7 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> { // WF obligations that are registered elsewhere, but they have a // better cause code assigned to them in `add_required_obligations_for_hir`. // This means that they should shadow obligations with worse spans. - if let ty::Alias(ty::Projection | ty::Weak, ty::AliasTy { args, def_id, .. }) = + if let ty::Alias(ty::Projection | ty::Free, ty::AliasTy { args, def_id, .. }) = ty.kind() { self.add_required_obligations_for_hir(span, *def_id, args, hir_id); diff --git a/compiler/rustc_infer/src/infer/relate/generalize.rs b/compiler/rustc_infer/src/infer/relate/generalize.rs index b0ccd35e8f0f0..210b8f37d883d 100644 --- a/compiler/rustc_infer/src/infer/relate/generalize.rs +++ b/compiler/rustc_infer/src/infer/relate/generalize.rs @@ -113,7 +113,7 @@ impl<'tcx> InferCtxt<'tcx> { }]); } // The old solver only accepts projection predicates for associated types. - ty::Alias(ty::Inherent | ty::Weak | ty::Opaque, _) => { + ty::Alias(ty::Inherent | ty::Free | ty::Opaque, _) => { return Err(TypeError::CyclicTy(source_ty)); } _ => bug!("generalized `{source_ty:?} to infer, not an alias"), diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index 60c183bd56b1f..9025c41240bf7 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -757,7 +757,7 @@ lint_single_use_lifetime = lifetime parameter `{$ident}` only used once lint_span_use_eq_ctxt = use `.eq_ctxt()` instead of `.ctxt() == .ctxt()` -lint_static_mut_refs_lint = creating a {$shared_label}reference to mutable static is discouraged +lint_static_mut_refs_lint = creating a {$shared_label}reference to mutable static .label = {$shared_label}reference to mutable static .suggestion = use `&raw const` instead to create a raw pointer .suggestion_mut = use `&raw mut` instead to create a raw pointer diff --git a/compiler/rustc_lint/src/static_mut_refs.rs b/compiler/rustc_lint/src/static_mut_refs.rs index 50021157ddab7..4dda3c7951b87 100644 --- a/compiler/rustc_lint/src/static_mut_refs.rs +++ b/compiler/rustc_lint/src/static_mut_refs.rs @@ -51,7 +51,7 @@ declare_lint! { /// This lint is "warn" by default on editions up to 2021, in 2024 is "deny". pub STATIC_MUT_REFS, Warn, - "shared references or mutable references of mutable static is discouraged", + "creating a shared reference to mutable static", @future_incompatible = FutureIncompatibleInfo { reason: FutureIncompatibilityReason::EditionError(Edition::Edition2024), reference: "", diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs index f9dce5a5198d0..7b6a723b0b40e 100644 --- a/compiler/rustc_lint/src/types.rs +++ b/compiler/rustc_lint/src/types.rs @@ -1381,7 +1381,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> { ty::UnsafeBinder(_) => todo!("FIXME(unsafe_binder)"), ty::Param(..) - | ty::Alias(ty::Projection | ty::Inherent | ty::Weak, ..) + | ty::Alias(ty::Projection | ty::Inherent | ty::Free, ..) | ty::Infer(..) | ty::Bound(..) | ty::Error(_) diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 3432648feab07..19a15041b4105 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -289,7 +289,7 @@ rustc_queries! { /// Returns whether the type alias given by `DefId` is lazy. /// - /// I.e., if the type alias expands / ought to expand to a [weak] [alias type] + /// I.e., if the type alias expands / ought to expand to a [free] [alias type] /// instead of the underyling aliased type. /// /// Relevant for features `lazy_type_alias` and `type_alias_impl_trait`. @@ -298,7 +298,7 @@ rustc_queries! { /// /// This query *may* panic if the given definition is not a type alias. /// - /// [weak]: rustc_middle::ty::Weak + /// [free]: rustc_middle::ty::Free /// [alias type]: rustc_middle::ty::AliasTy query type_alias_is_lazy(key: DefId) -> bool { desc { |tcx| @@ -2280,7 +2280,7 @@ rustc_queries! { /// Do not call this query directly: Invoke `normalize` instead. /// /// - query normalize_canonicalized_weak_ty( + query normalize_canonicalized_free_alias( goal: CanonicalAliasGoal<'tcx> ) -> Result< &'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, NormalizationResult<'tcx>>>, diff --git a/compiler/rustc_middle/src/traits/mod.rs b/compiler/rustc_middle/src/traits/mod.rs index 7e6151745e2f8..27079af06fcd3 100644 --- a/compiler/rustc_middle/src/traits/mod.rs +++ b/compiler/rustc_middle/src/traits/mod.rs @@ -404,7 +404,7 @@ pub enum ObligationCauseCode<'tcx> { /// Requirement for a `const N: Ty` to implement `Ty: ConstParamTy` ConstParam(Ty<'tcx>), - /// Obligations emitted during the normalization of a weak type alias. + /// Obligations emitted during the normalization of a free type alias. TypeAlias(ObligationCauseCodeHandle<'tcx>, Span, DefId), } diff --git a/compiler/rustc_middle/src/traits/query.rs b/compiler/rustc_middle/src/traits/query.rs index 76f3d2bab9cf4..3f6faa1a572d9 100644 --- a/compiler/rustc_middle/src/traits/query.rs +++ b/compiler/rustc_middle/src/traits/query.rs @@ -181,7 +181,7 @@ pub struct MethodAutoderefBadTy<'tcx> { pub ty: Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>, } -/// Result of the `normalize_canonicalized_{{,inherent_}projection,weak}_ty` queries. +/// Result of the `normalize_canonicalized_{{,inherent_}projection,free}_ty` queries. #[derive(Clone, Debug, HashStable, TypeFoldable, TypeVisitable)] pub struct NormalizationResult<'tcx> { /// Result of the normalization. diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index e8dad1e056cbd..4de070b451c52 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -226,7 +226,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> { } } DefKind::OpaqueTy => ty::Opaque, - DefKind::TyAlias => ty::Weak, + DefKind::TyAlias => ty::Free, kind => bug!("unexpected DefKind in AliasTy: {kind:?}"), } } @@ -242,7 +242,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> { } } DefKind::OpaqueTy => ty::AliasTermKind::OpaqueTy, - DefKind::TyAlias => ty::AliasTermKind::WeakTy, + DefKind::TyAlias => ty::AliasTermKind::FreeTy, DefKind::AssocConst => ty::AliasTermKind::ProjectionConst, DefKind::AnonConst | DefKind::Const | DefKind::Ctor(_, CtorKind::Const) => { ty::AliasTermKind::UnevaluatedConst diff --git a/compiler/rustc_middle/src/ty/error.rs b/compiler/rustc_middle/src/ty/error.rs index 5ecf1174defe0..13723874ad3a1 100644 --- a/compiler/rustc_middle/src/ty/error.rs +++ b/compiler/rustc_middle/src/ty/error.rs @@ -205,7 +205,7 @@ impl<'tcx> Ty<'tcx> { ty::Placeholder(..) => "higher-ranked type".into(), ty::Bound(..) => "bound type variable".into(), ty::Alias(ty::Projection | ty::Inherent, _) => "associated type".into(), - ty::Alias(ty::Weak, _) => "type alias".into(), + ty::Alias(ty::Free, _) => "type alias".into(), ty::Param(_) => "type parameter".into(), ty::Alias(ty::Opaque, ..) => "opaque type".into(), } diff --git a/compiler/rustc_middle/src/ty/inhabitedness/mod.rs b/compiler/rustc_middle/src/ty/inhabitedness/mod.rs index 32988965a35bc..d8bab58545fcc 100644 --- a/compiler/rustc_middle/src/ty/inhabitedness/mod.rs +++ b/compiler/rustc_middle/src/ty/inhabitedness/mod.rs @@ -127,7 +127,7 @@ impl<'tcx> Ty<'tcx> { InhabitedPredicate::True } Never => InhabitedPredicate::False, - Param(_) | Alias(ty::Projection | ty::Weak, _) => InhabitedPredicate::GenericType(self), + Param(_) | Alias(ty::Projection | ty::Free, _) => InhabitedPredicate::GenericType(self), Alias(ty::Opaque, alias_ty) => { match alias_ty.def_id.as_local() { // Foreign opaque is considered inhabited. diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 1df3bff524422..ad8677f7c7da2 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -820,7 +820,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { ty::Foreign(def_id) => { p!(print_def_path(def_id, &[])); } - ty::Alias(ty::Projection | ty::Inherent | ty::Weak, ref data) => { + ty::Alias(ty::Projection | ty::Inherent | ty::Free, ref data) => { p!(print(data)) } ty::Placeholder(placeholder) => match placeholder.bound.kind { @@ -3205,7 +3205,7 @@ define_print! { p!(print_def_path(self.def_id, self.args)); } } - | ty::AliasTermKind::WeakTy + | ty::AliasTermKind::FreeTy | ty::AliasTermKind::OpaqueTy | ty::AliasTermKind::UnevaluatedConst | ty::AliasTermKind::ProjectionConst => { diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index affb7b31ae1d8..ddce400e5b1a9 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -489,7 +489,7 @@ impl<'tcx> Ty<'tcx> { (kind, tcx.def_kind(alias_ty.def_id)), (ty::Opaque, DefKind::OpaqueTy) | (ty::Projection | ty::Inherent, DefKind::AssocTy) - | (ty::Weak, DefKind::TyAlias) + | (ty::Free, DefKind::TyAlias) ); Ty::new(tcx, Alias(kind, alias_ty)) } diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index 08cee1101d0a9..269425d67f386 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -911,7 +911,7 @@ impl<'tcx> TyCtxt<'tcx> { || self.extern_crate(key).is_some_and(|e| e.is_direct()) } - /// Expand any [weak alias types][weak] contained within the given `value`. + /// Expand any [free alias types][free] contained within the given `value`. /// /// This should be used over other normalization routines in situations where /// it's important not to normalize other alias types and where the predicates @@ -926,19 +926,19 @@ impl<'tcx> TyCtxt<'tcx> { ///
/// This delays a bug on overflow! Therefore you need to be certain that the /// contained types get fully normalized at a later stage. Note that even on - /// overflow all well-behaved weak alias types get expanded correctly, so the + /// overflow all well-behaved free alias types get expanded correctly, so the /// result is still useful. ///
/// - /// [weak]: ty::Weak - pub fn expand_weak_alias_tys>>(self, value: T) -> T { - value.fold_with(&mut WeakAliasTypeExpander { tcx: self, depth: 0 }) + /// [free]: ty::Free + pub fn expand_free_alias_tys>>(self, value: T) -> T { + value.fold_with(&mut FreeAliasTypeExpander { tcx: self, depth: 0 }) } - /// Peel off all [weak alias types] in this type until there are none left. + /// Peel off all [free alias types] in this type until there are none left. /// - /// This only expands weak alias types in “head” / outermost positions. It can - /// be used over [expand_weak_alias_tys] as an optimization in situations where + /// This only expands free alias types in “head” / outermost positions. It can + /// be used over [expand_free_alias_tys] as an optimization in situations where /// one only really cares about the *kind* of the final aliased type but not /// the types the other constituent types alias. /// @@ -947,17 +947,17 @@ impl<'tcx> TyCtxt<'tcx> { /// type gets fully normalized at a later stage. /// /// - /// [weak]: ty::Weak - /// [expand_weak_alias_tys]: Self::expand_weak_alias_tys - pub fn peel_off_weak_alias_tys(self, mut ty: Ty<'tcx>) -> Ty<'tcx> { - let ty::Alias(ty::Weak, _) = ty.kind() else { return ty }; + /// [free]: ty::Free + /// [expand_free_alias_tys]: Self::expand_free_alias_tys + pub fn peel_off_free_alias_tys(self, mut ty: Ty<'tcx>) -> Ty<'tcx> { + let ty::Alias(ty::Free, _) = ty.kind() else { return ty }; let limit = self.recursion_limit(); let mut depth = 0; - while let ty::Alias(ty::Weak, alias) = ty.kind() { + while let ty::Alias(ty::Free, alias) = ty.kind() { if !limit.value_within_limit(depth) { - let guar = self.dcx().delayed_bug("overflow expanding weak alias type"); + let guar = self.dcx().delayed_bug("overflow expanding free alias type"); return Ty::new_error(self, guar); } @@ -985,7 +985,7 @@ impl<'tcx> TyCtxt<'tcx> { } ty::AliasTermKind::OpaqueTy => Some(self.variances_of(def_id)), ty::AliasTermKind::InherentTy - | ty::AliasTermKind::WeakTy + | ty::AliasTermKind::FreeTy | ty::AliasTermKind::UnevaluatedConst | ty::AliasTermKind::ProjectionConst => None, } @@ -1078,25 +1078,25 @@ impl<'tcx> TypeFolder> for OpaqueTypeExpander<'tcx> { } } -struct WeakAliasTypeExpander<'tcx> { +struct FreeAliasTypeExpander<'tcx> { tcx: TyCtxt<'tcx>, depth: usize, } -impl<'tcx> TypeFolder> for WeakAliasTypeExpander<'tcx> { +impl<'tcx> TypeFolder> for FreeAliasTypeExpander<'tcx> { fn cx(&self) -> TyCtxt<'tcx> { self.tcx } fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { - if !ty.has_type_flags(ty::TypeFlags::HAS_TY_WEAK) { + if !ty.has_type_flags(ty::TypeFlags::HAS_TY_FREE_ALIAS) { return ty; } - let ty::Alias(ty::Weak, alias) = ty.kind() else { + let ty::Alias(ty::Free, alias) = ty.kind() else { return ty.super_fold_with(self); }; if !self.tcx.recursion_limit().value_within_limit(self.depth) { - let guar = self.tcx.dcx().delayed_bug("overflow expanding weak alias type"); + let guar = self.tcx.dcx().delayed_bug("overflow expanding free alias type"); return Ty::new_error(self.tcx, guar); } @@ -1107,7 +1107,7 @@ impl<'tcx> TypeFolder> for WeakAliasTypeExpander<'tcx> { } fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> { - if !ct.has_type_flags(ty::TypeFlags::HAS_TY_WEAK) { + if !ct.has_type_flags(ty::TypeFlags::HAS_TY_FREE_ALIAS) { return ct; } ct.super_fold_with(self) diff --git a/compiler/rustc_middle/src/ty/visit.rs b/compiler/rustc_middle/src/ty/visit.rs index 44c7b6a7c9e8d..3853a804a920f 100644 --- a/compiler/rustc_middle/src/ty/visit.rs +++ b/compiler/rustc_middle/src/ty/visit.rs @@ -139,7 +139,7 @@ impl<'tcx> TyCtxt<'tcx> { { let mut collector = LateBoundRegionsCollector::new(just_constrained); let value = value.skip_binder(); - let value = if just_constrained { self.expand_weak_alias_tys(value) } else { value }; + let value = if just_constrained { self.expand_free_alias_tys(value) } else { value }; value.visit_with(&mut collector); collector.regions } @@ -182,8 +182,8 @@ impl<'tcx> TypeVisitor> for LateBoundRegionsCollector { ty::Alias(ty::Projection | ty::Inherent | ty::Opaque, _) => { return; } - // All weak alias types should've been expanded beforehand. - ty::Alias(ty::Weak, _) => bug!("unexpected weak alias type"), + // All free alias types should've been expanded beforehand. + ty::Alias(ty::Free, _) => bug!("unexpected free alias type"), _ => {} } } diff --git a/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs b/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs index 381a732b8dee9..763fccfc2ea89 100644 --- a/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs @@ -596,7 +596,7 @@ where } ty::Alias(kind @ (ty::Projection | ty::Opaque), alias_ty) => (kind, alias_ty), - ty::Alias(ty::Inherent | ty::Weak, _) => { + ty::Alias(ty::Inherent | ty::Free, _) => { self.cx().delay_bug(format!("could not normalize {self_ty:?}, it is not WF")); return; } diff --git a/compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs b/compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs index 1526049719ea0..035bfff89b53c 100644 --- a/compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs +++ b/compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs @@ -48,7 +48,7 @@ where ty::Dynamic(..) | ty::Param(..) - | ty::Alias(ty::Projection | ty::Inherent | ty::Weak, ..) + | ty::Alias(ty::Projection | ty::Inherent | ty::Free, ..) | ty::Placeholder(..) | ty::Bound(..) | ty::Infer(_) => { diff --git a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/weak_types.rs b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/free_alias.rs similarity index 73% rename from compiler/rustc_next_trait_solver/src/solve/normalizes_to/weak_types.rs rename to compiler/rustc_next_trait_solver/src/solve/normalizes_to/free_alias.rs index 14e68dd52b6c1..d077f8a9be8ad 100644 --- a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/weak_types.rs +++ b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/free_alias.rs @@ -1,7 +1,7 @@ //! Computes a normalizes-to (projection) goal for inherent associated types, //! `#![feature(lazy_type_alias)]` and `#![feature(type_alias_impl_trait)]`. //! -//! Since a weak alias is never ambiguous, this just computes the `type_of` of +//! Since a free alias is never ambiguous, this just computes the `type_of` of //! the alias and registers the where-clauses of the type alias. use rustc_type_ir::{self as ty, Interner}; @@ -14,22 +14,22 @@ where D: SolverDelegate, I: Interner, { - pub(super) fn normalize_weak_type( + pub(super) fn normalize_free_alias( &mut self, goal: Goal>, ) -> QueryResult { let cx = self.cx(); - let weak_ty = goal.predicate.alias; + let free_ty = goal.predicate.alias; // Check where clauses self.add_goals( GoalSource::Misc, - cx.predicates_of(weak_ty.def_id) - .iter_instantiated(cx, weak_ty.args) + cx.predicates_of(free_ty.def_id) + .iter_instantiated(cx, free_ty.args) .map(|pred| goal.with(cx, pred)), ); - let actual = cx.type_of(weak_ty.def_id).instantiate(cx, weak_ty.args); + let actual = cx.type_of(free_ty.def_id).instantiate(cx, free_ty.args); self.instantiate_normalizes_to_term(goal, actual.into()); self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes) diff --git a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs index 119d197de134f..d92f71ab36f4b 100644 --- a/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs +++ b/compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs @@ -1,7 +1,7 @@ mod anon_const; +mod free_alias; mod inherent; mod opaque_types; -mod weak_types; use rustc_type_ir::fast_reject::DeepRejectCtxt; use rustc_type_ir::inherent::*; @@ -50,7 +50,7 @@ where } ty::AliasTermKind::InherentTy => self.normalize_inherent_associated_type(goal), ty::AliasTermKind::OpaqueTy => self.normalize_opaque_type(goal), - ty::AliasTermKind::WeakTy => self.normalize_weak_type(goal), + ty::AliasTermKind::FreeTy => self.normalize_free_alias(goal), ty::AliasTermKind::UnevaluatedConst => self.normalize_anon_const(goal), } } diff --git a/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs b/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs index 24657496df6a5..61fdf0a6ca15a 100644 --- a/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs +++ b/compiler/rustc_next_trait_solver/src/solve/trait_goals.rs @@ -1153,7 +1153,7 @@ where ty::Dynamic(..) | ty::Param(..) | ty::Foreign(..) - | ty::Alias(ty::Projection | ty::Weak | ty::Inherent, ..) + | ty::Alias(ty::Projection | ty::Free | ty::Inherent, ..) | ty::Placeholder(..) => Some(Err(NoSolution)), ty::Infer(_) | ty::Bound(_, _) => panic!("unexpected type `{self_ty:?}`"), diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index b56f3e1971cc8..a3b479fdb7a98 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -213,7 +213,7 @@ where } } } - ty::Alias(kind @ (ty::Inherent | ty::Weak | ty::Projection), data) => { + ty::Alias(kind @ (ty::Inherent | ty::Free | ty::Projection), data) => { if self.def_id_visitor.skip_assoc_tys() { // Visitors searching for minimal visibility/reachability want to // conservatively approximate associated types like `Type::Alias` @@ -227,7 +227,7 @@ where data.def_id, match kind { ty::Inherent | ty::Projection => "associated type", - ty::Weak => "type alias", + ty::Free => "type alias", ty::Opaque => unreachable!(), }, &LazyDefPathStr { def_id: data.def_id, tcx }, diff --git a/compiler/rustc_smir/src/rustc_smir/convert/ty.rs b/compiler/rustc_smir/src/rustc_smir/convert/ty.rs index c0ed3b90eb416..6d311030c5c7a 100644 --- a/compiler/rustc_smir/src/rustc_smir/convert/ty.rs +++ b/compiler/rustc_smir/src/rustc_smir/convert/ty.rs @@ -16,7 +16,7 @@ impl<'tcx> Stable<'tcx> for ty::AliasTyKind { ty::Projection => stable_mir::ty::AliasKind::Projection, ty::Inherent => stable_mir::ty::AliasKind::Inherent, ty::Opaque => stable_mir::ty::AliasKind::Opaque, - ty::Weak => stable_mir::ty::AliasKind::Weak, + ty::Free => stable_mir::ty::AliasKind::Free, } } } diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index 0ac9a0fb64785..e331e5934716a 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -1026,7 +1026,7 @@ pub enum AliasKind { Projection, Inherent, Opaque, - Weak, + Free, } #[derive(Clone, Debug, Eq, PartialEq, Serialize)] diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/region.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/region.rs index 1cf1ac5403f05..242469a225a39 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/region.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/region.rs @@ -707,7 +707,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { ty::Projection | ty::Inherent => { format!("the associated type `{p}`") } - ty::Weak => format!("the type alias `{p}`"), + ty::Free => format!("the type alias `{p}`"), ty::Opaque => format!("the opaque type `{p}`"), }, }; diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs index d8e05d9ad9714..ab2aa0ae46976 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs @@ -1659,7 +1659,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { ty::Alias(ty::Projection, ..) => Some(12), ty::Alias(ty::Inherent, ..) => Some(13), ty::Alias(ty::Opaque, ..) => Some(14), - ty::Alias(ty::Weak, ..) => Some(15), + ty::Alias(ty::Free, ..) => Some(15), ty::Never => Some(16), ty::Adt(..) => Some(17), ty::Coroutine(..) => Some(18), diff --git a/compiler/rustc_trait_selection/src/traits/normalize.rs b/compiler/rustc_trait_selection/src/traits/normalize.rs index 7551ac5aa9735..d38ddbc825c4e 100644 --- a/compiler/rustc_trait_selection/src/traits/normalize.rs +++ b/compiler/rustc_trait_selection/src/traits/normalize.rs @@ -333,7 +333,7 @@ impl<'a, 'b, 'tcx> TypeFolder> for AssocTypeNormalizer<'a, 'b, 'tcx ); normalized_ty } - ty::Weak => { + ty::Free => { let recursion_limit = self.cx().recursion_limit(); if !recursion_limit.value_within_limit(self.depth) { self.selcx.infcx.err_ctxt().report_overflow_error( diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 99fa791b37550..de4979cc1ffd7 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -468,7 +468,7 @@ fn normalize_to_error<'a, 'tcx>( ty::AliasTermKind::ProjectionTy | ty::AliasTermKind::InherentTy | ty::AliasTermKind::OpaqueTy - | ty::AliasTermKind::WeakTy => selcx.infcx.next_ty_var(cause.span).into(), + | ty::AliasTermKind::FreeTy => selcx.infcx.next_ty_var(cause.span).into(), ty::AliasTermKind::UnevaluatedConst | ty::AliasTermKind::ProjectionConst => { selcx.infcx.next_const_var(cause.span).into() } diff --git a/compiler/rustc_trait_selection/src/traits/query/normalize.rs b/compiler/rustc_trait_selection/src/traits/query/normalize.rs index 507932699c7cd..a54eb80fedc25 100644 --- a/compiler/rustc_trait_selection/src/traits/query/normalize.rs +++ b/compiler/rustc_trait_selection/src/traits/query/normalize.rs @@ -253,7 +253,7 @@ impl<'a, 'tcx> FallibleTypeFolder> for QueryNormalizer<'a, 'tcx> { } } - ty::Projection | ty::Inherent | ty::Weak => { + ty::Projection | ty::Inherent | ty::Free => { // See note in `rustc_trait_selection::traits::project` let infcx = self.infcx; @@ -275,7 +275,7 @@ impl<'a, 'tcx> FallibleTypeFolder> for QueryNormalizer<'a, 'tcx> { debug!("QueryNormalizer: orig_values = {:#?}", orig_values); let result = match kind { ty::Projection => tcx.normalize_canonicalized_projection_ty(c_data), - ty::Weak => tcx.normalize_canonicalized_weak_ty(c_data), + ty::Free => tcx.normalize_canonicalized_free_alias(c_data), ty::Inherent => tcx.normalize_canonicalized_inherent_projection_ty(c_data), kind => unreachable!("did not expect {kind:?} due to match arm above"), }?; @@ -313,10 +313,10 @@ impl<'a, 'tcx> FallibleTypeFolder> for QueryNormalizer<'a, 'tcx> { }; // `tcx.normalize_canonicalized_projection_ty` may normalize to a type that // still has unevaluated consts, so keep normalizing here if that's the case. - // Similarly, `tcx.normalize_canonicalized_weak_ty` will only unwrap one layer + // Similarly, `tcx.normalize_canonicalized_free_alias` will only unwrap one layer // of type and we need to continue folding it to reveal the TAIT behind it. if res != ty - && (res.has_type_flags(ty::TypeFlags::HAS_CT_PROJECTION) || kind == ty::Weak) + && (res.has_type_flags(ty::TypeFlags::HAS_CT_PROJECTION) || kind == ty::Free) { res.try_fold_with(self)? } else { diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index 00101010f14fa..e9984da003306 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -728,7 +728,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { } } ty::Param(..) - | ty::Alias(ty::Projection | ty::Inherent | ty::Weak, ..) + | ty::Alias(ty::Projection | ty::Inherent | ty::Free, ..) | ty::Placeholder(..) | ty::Bound(..) => { // In these cases, we don't know what the actual diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 89febd6ce3da1..df02a67c2c9b5 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -2323,7 +2323,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> { ty::Placeholder(..) | ty::Dynamic(..) | ty::Param(..) - | ty::Alias(ty::Projection | ty::Inherent | ty::Weak, ..) + | ty::Alias(ty::Projection | ty::Inherent | ty::Free, ..) | ty::Bound(..) | ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => { bug!("asked to assemble constituent types of unexpected type: {:?}", t); diff --git a/compiler/rustc_trait_selection/src/traits/wf.rs b/compiler/rustc_trait_selection/src/traits/wf.rs index 62bd8e1af05de..00a4a58a6d865 100644 --- a/compiler/rustc_trait_selection/src/traits/wf.rs +++ b/compiler/rustc_trait_selection/src/traits/wf.rs @@ -761,7 +761,7 @@ impl<'a, 'tcx> TypeVisitor> for WfPredicates<'a, 'tcx> { // Simple cases that are WF if their type args are WF. } - ty::Alias(ty::Projection | ty::Opaque | ty::Weak, data) => { + ty::Alias(ty::Projection | ty::Opaque | ty::Free, data) => { let obligations = self.nominal_obligations(data.def_id, data.args); self.out.extend(obligations); } diff --git a/compiler/rustc_traits/src/normalize_projection_ty.rs b/compiler/rustc_traits/src/normalize_projection_ty.rs index 4c2b7e4769ab0..14a92ebb9f966 100644 --- a/compiler/rustc_traits/src/normalize_projection_ty.rs +++ b/compiler/rustc_traits/src/normalize_projection_ty.rs @@ -13,7 +13,7 @@ use tracing::debug; pub(crate) fn provide(p: &mut Providers) { *p = Providers { normalize_canonicalized_projection_ty, - normalize_canonicalized_weak_ty, + normalize_canonicalized_free_alias, normalize_canonicalized_inherent_projection_ty, ..*p }; @@ -63,11 +63,11 @@ fn normalize_canonicalized_projection_ty<'tcx>( ) } -fn normalize_canonicalized_weak_ty<'tcx>( +fn normalize_canonicalized_free_alias<'tcx>( tcx: TyCtxt<'tcx>, goal: CanonicalAliasGoal<'tcx>, ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, NormalizationResult<'tcx>>>, NoSolution> { - debug!("normalize_canonicalized_weak_ty(goal={:#?})", goal); + debug!("normalize_canonicalized_free_alias(goal={:#?})", goal); tcx.infer_ctxt().enter_canonical_trait_query( &goal, diff --git a/compiler/rustc_ty_utils/src/opaque_types.rs b/compiler/rustc_ty_utils/src/opaque_types.rs index cd730aeeea900..841f602d985cd 100644 --- a/compiler/rustc_ty_utils/src/opaque_types.rs +++ b/compiler/rustc_ty_utils/src/opaque_types.rs @@ -223,7 +223,7 @@ impl<'tcx> TypeVisitor> for OpaqueTypeCollector<'tcx> { } // Skips type aliases, as they are meant to be transparent. // FIXME(type_alias_impl_trait): can we require mentioning nested type aliases explicitly? - ty::Alias(ty::Weak, alias_ty) if alias_ty.def_id.is_local() => { + ty::Alias(ty::Free, alias_ty) if alias_ty.def_id.is_local() => { self.tcx .type_of(alias_ty.def_id) .instantiate(self.tcx, alias_ty.args) diff --git a/compiler/rustc_type_ir/src/flags.rs b/compiler/rustc_type_ir/src/flags.rs index 74fb148a7cc30..b37347354fabf 100644 --- a/compiler/rustc_type_ir/src/flags.rs +++ b/compiler/rustc_type_ir/src/flags.rs @@ -73,8 +73,8 @@ bitflags::bitflags! { /// Does this have `Projection`? const HAS_TY_PROJECTION = 1 << 10; - /// Does this have `Weak`? - const HAS_TY_WEAK = 1 << 11; + /// Does this have `Free` aliases? + const HAS_TY_FREE_ALIAS = 1 << 11; /// Does this have `Opaque`? const HAS_TY_OPAQUE = 1 << 12; /// Does this have `Inherent`? @@ -86,7 +86,7 @@ bitflags::bitflags! { /// /// Rephrased, could this term be normalized further? const HAS_ALIAS = TypeFlags::HAS_TY_PROJECTION.bits() - | TypeFlags::HAS_TY_WEAK.bits() + | TypeFlags::HAS_TY_FREE_ALIAS.bits() | TypeFlags::HAS_TY_OPAQUE.bits() | TypeFlags::HAS_TY_INHERENT.bits() | TypeFlags::HAS_CT_PROJECTION.bits(); @@ -273,7 +273,7 @@ impl FlagComputation { ty::Alias(kind, data) => { self.add_flags(match kind { ty::Projection => TypeFlags::HAS_TY_PROJECTION, - ty::Weak => TypeFlags::HAS_TY_WEAK, + ty::Free => TypeFlags::HAS_TY_FREE_ALIAS, ty::Opaque => TypeFlags::HAS_TY_OPAQUE, ty::Inherent => TypeFlags::HAS_TY_INHERENT, }); diff --git a/compiler/rustc_type_ir/src/predicate.rs b/compiler/rustc_type_ir/src/predicate.rs index 8e10636ff657d..0411c5c2325eb 100644 --- a/compiler/rustc_type_ir/src/predicate.rs +++ b/compiler/rustc_type_ir/src/predicate.rs @@ -470,10 +470,10 @@ pub enum AliasTermKind { /// An opaque type (usually from `impl Trait` in type aliases or function return types) /// Can only be normalized away in PostAnalysis mode or its defining scope. OpaqueTy, - /// A type alias that actually checks its trait bounds. + /// A free type alias that actually checks its trait bounds. /// Currently only used if the type alias references opaque types. /// Can always be normalized away. - WeakTy, + FreeTy, /// An unevaluated const coming from a generic const expression. UnevaluatedConst, /// An unevaluated const coming from an associated const. @@ -487,7 +487,7 @@ impl AliasTermKind { AliasTermKind::ProjectionConst => "associated const", AliasTermKind::InherentTy => "inherent associated type", AliasTermKind::OpaqueTy => "opaque type", - AliasTermKind::WeakTy => "type alias", + AliasTermKind::FreeTy => "type alias", AliasTermKind::UnevaluatedConst => "unevaluated constant", } } @@ -498,7 +498,7 @@ impl From for AliasTermKind { match value { ty::Projection => AliasTermKind::ProjectionTy, ty::Opaque => AliasTermKind::OpaqueTy, - ty::Weak => AliasTermKind::WeakTy, + ty::Free => AliasTermKind::FreeTy, ty::Inherent => AliasTermKind::InherentTy, } } @@ -565,7 +565,7 @@ impl AliasTerm { AliasTermKind::ProjectionTy | AliasTermKind::InherentTy | AliasTermKind::OpaqueTy - | AliasTermKind::WeakTy => {} + | AliasTermKind::FreeTy => {} AliasTermKind::UnevaluatedConst | AliasTermKind::ProjectionConst => { panic!("Cannot turn `UnevaluatedConst` into `AliasTy`") } @@ -597,9 +597,9 @@ impl AliasTerm { ty::AliasTy { def_id: self.def_id, args: self.args, _use_alias_ty_new_instead: () }, ) .into(), - AliasTermKind::WeakTy => Ty::new_alias( + AliasTermKind::FreeTy => Ty::new_alias( interner, - ty::AliasTyKind::Weak, + ty::AliasTyKind::Free, ty::AliasTy { def_id: self.def_id, args: self.args, _use_alias_ty_new_instead: () }, ) .into(), diff --git a/compiler/rustc_type_ir/src/relate.rs b/compiler/rustc_type_ir/src/relate.rs index d065384b58e23..c80a567117c66 100644 --- a/compiler/rustc_type_ir/src/relate.rs +++ b/compiler/rustc_type_ir/src/relate.rs @@ -273,7 +273,7 @@ impl Relate for ty::AliasTerm { false, // do not fetch `type_of(a_def_id)`, as it will cause a cycle )?, ty::AliasTermKind::ProjectionTy - | ty::AliasTermKind::WeakTy + | ty::AliasTermKind::FreeTy | ty::AliasTermKind::InherentTy | ty::AliasTermKind::UnevaluatedConst | ty::AliasTermKind::ProjectionConst => { diff --git a/compiler/rustc_type_ir/src/ty_kind.rs b/compiler/rustc_type_ir/src/ty_kind.rs index 753a72a051ad8..21adbffc02743 100644 --- a/compiler/rustc_type_ir/src/ty_kind.rs +++ b/compiler/rustc_type_ir/src/ty_kind.rs @@ -54,7 +54,7 @@ pub enum AliasTyKind { /// A type alias that actually checks its trait bounds. /// Currently only used if the type alias references opaque types. /// Can always be normalized away. - Weak, + Free, } impl AliasTyKind { @@ -63,7 +63,7 @@ impl AliasTyKind { AliasTyKind::Projection => "associated type", AliasTyKind::Inherent => "inherent associated type", AliasTyKind::Opaque => "opaque type", - AliasTyKind::Weak => "type alias", + AliasTyKind::Free => "type alias", } } } @@ -223,7 +223,7 @@ pub enum TyKind { /// A tuple type. For example, `(i32, bool)`. Tuple(I::Tys), - /// A projection, opaque type, weak type alias, or inherent associated type. + /// A projection, opaque type, free type alias, or inherent associated type. /// All of these types are represented as pairs of def-id and args, and can /// be normalized, so they are grouped conceptually. Alias(AliasTyKind, AliasTy), diff --git a/src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile b/src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile index ae5bf8946dd94..bedf45c8630cf 100644 --- a/src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile +++ b/src/ci/docker/host-x86_64/dist-x86_64-linux/Dockerfile @@ -2,7 +2,7 @@ # CentOS 7 has headers for kernel 3.10, but that's fine as long as we don't # actually use newer APIs in rustc or std without a fallback. It's more # important that we match glibc for ELF symbol versioning. -FROM centos:7 +FROM ghcr.io/rust-lang/centos:7 WORKDIR /build diff --git a/src/ci/github-actions/jobs.yml b/src/ci/github-actions/jobs.yml index 88b29d2df56ae..afcc092e78e8d 100644 --- a/src/ci/github-actions/jobs.yml +++ b/src/ci/github-actions/jobs.yml @@ -65,6 +65,12 @@ runners: os: codebuild-ubuntu-22-36c-$github.run_id-$github.run_attempt <<: *base-job + - &job-linux-8c-codebuild + free_disk: true + codebuild: true + os: codebuild-ubuntu-22-8c-$github.run_id-$github.run_attempt + <<: *base-job + envs: env-x86_64-apple-tests: &env-x86_64-apple-tests SCRIPT: ./x.py check compiletest --set build.compiletest-use-stage0-libtest=true && ./x.py --stage 2 test --skip tests/ui --skip tests/rustdoc -- --exact @@ -160,7 +166,7 @@ auto: <<: *job-linux-4c - name: dist-arm-linux - <<: *job-linux-36c-codebuild + <<: *job-linux-8c-codebuild - name: dist-armhf-linux <<: *job-linux-4c @@ -213,7 +219,7 @@ auto: - name: dist-x86_64-linux env: CODEGEN_BACKENDS: llvm,cranelift - <<: *job-linux-16c + <<: *job-linux-36c-codebuild - name: dist-x86_64-linux-alt env: diff --git a/src/ci/scripts/free-disk-space.sh b/src/ci/scripts/free-disk-space.sh index ad7ee136e9c27..173f64858b371 100755 --- a/src/ci/scripts/free-disk-space.sh +++ b/src/ci/scripts/free-disk-space.sh @@ -87,59 +87,80 @@ printDF() { removeUnusedFilesAndDirs() { local to_remove=( - "/usr/local/aws-sam-cli" - "/usr/local/doc/cmake" - "/usr/local/julia"* - "/usr/local/lib/android" - "/usr/local/share/chromedriver-"* - "/usr/local/share/chromium" - "/usr/local/share/cmake-"* - "/usr/local/share/edge_driver" - "/usr/local/share/gecko_driver" - "/usr/local/share/icons" - "/usr/local/share/vim" - "/usr/local/share/emacs" - "/usr/local/share/powershell" - "/usr/local/share/vcpkg" - "/usr/share/apache-maven-"* - "/usr/share/gradle-"* "/usr/share/java" - "/usr/share/kotlinc" - "/usr/share/miniconda" - "/usr/share/php" - "/usr/share/ri" - "/usr/share/swift" - - # binaries - "/usr/local/bin/azcopy" - "/usr/local/bin/bicep" - "/usr/local/bin/ccmake" - "/usr/local/bin/cmake-"* - "/usr/local/bin/cmake" - "/usr/local/bin/cpack" - "/usr/local/bin/ctest" - "/usr/local/bin/helm" - "/usr/local/bin/kind" - "/usr/local/bin/kustomize" - "/usr/local/bin/minikube" - "/usr/local/bin/packer" - "/usr/local/bin/phpunit" - "/usr/local/bin/pulumi-"* - "/usr/local/bin/pulumi" - "/usr/local/bin/stack" - - # Haskell runtime - "/usr/local/.ghcup" - - # Azure - "/opt/az" - "/usr/share/az_"* ) - if [ -n "${AGENT_TOOLSDIRECTORY:-}" ]; then - # Environment variable set by GitHub Actions + if isGitHubRunner; then + to_remove+=( + "/usr/local/aws-sam-cli" + "/usr/local/doc/cmake" + "/usr/local/julia"* + "/usr/local/lib/android" + "/usr/local/share/chromedriver-"* + "/usr/local/share/chromium" + "/usr/local/share/cmake-"* + "/usr/local/share/edge_driver" + "/usr/local/share/emacs" + "/usr/local/share/gecko_driver" + "/usr/local/share/icons" + "/usr/local/share/powershell" + "/usr/local/share/vcpkg" + "/usr/local/share/vim" + "/usr/share/apache-maven-"* + "/usr/share/gradle-"* + "/usr/share/kotlinc" + "/usr/share/miniconda" + "/usr/share/php" + "/usr/share/ri" + "/usr/share/swift" + + # binaries + "/usr/local/bin/azcopy" + "/usr/local/bin/bicep" + "/usr/local/bin/ccmake" + "/usr/local/bin/cmake-"* + "/usr/local/bin/cmake" + "/usr/local/bin/cpack" + "/usr/local/bin/ctest" + "/usr/local/bin/helm" + "/usr/local/bin/kind" + "/usr/local/bin/kustomize" + "/usr/local/bin/minikube" + "/usr/local/bin/packer" + "/usr/local/bin/phpunit" + "/usr/local/bin/pulumi-"* + "/usr/local/bin/pulumi" + "/usr/local/bin/stack" + + # Haskell runtime + "/usr/local/.ghcup" + + # Azure + "/opt/az" + "/usr/share/az_"* + ) + + if [ -n "${AGENT_TOOLSDIRECTORY:-}" ]; then + # Environment variable set by GitHub Actions + to_remove+=( + "${AGENT_TOOLSDIRECTORY}" + ) + else + echo "::warning::AGENT_TOOLSDIRECTORY is not set. Skipping removal." + fi + else + # Remove folders and files present in AWS CodeBuild to_remove+=( - "${AGENT_TOOLSDIRECTORY}" + # binaries + "/usr/local/bin/ecs-cli" + "/usr/local/bin/eksctl" + "/usr/local/bin/kubectl" + + "${HOME}/.gradle" + "${HOME}/.dotnet" + "${HOME}/.goenv" + "${HOME}/.phpenv" + ) fi @@ -194,6 +215,10 @@ cleanPackages() { 'powershell' ) fi + else + packages+=( + 'google-chrome-stable' + ) fi sudo apt-get -qq remove -y --fix-missing "${packages[@]}" diff --git a/src/doc/rustc-dev-guide/.github/workflows/rustc-pull.yml b/src/doc/rustc-dev-guide/.github/workflows/rustc-pull.yml index b19eccf9eb8c1..1e430d8b4e65a 100644 --- a/src/doc/rustc-dev-guide/.github/workflows/rustc-pull.yml +++ b/src/doc/rustc-dev-guide/.github/workflows/rustc-pull.yml @@ -28,7 +28,7 @@ jobs: # Cache the josh directory with checked out rustc cache-directories: "/home/runner/.cache/rustc-dev-guide-josh" - name: Install josh - run: RUSTFLAGS="--cap-lints warn" cargo +stable install josh-proxy --git https://github.com/josh-project/josh --tag r24.10.04 + run: RUSTFLAGS="--cap-lints warn" cargo install josh-proxy --git https://github.com/josh-project/josh --tag r24.10.04 - name: Setup bot git name and email run: | git config --global user.name 'The rustc-dev-guide Cronjob Bot' diff --git a/src/doc/rustc-dev-guide/README.md b/src/doc/rustc-dev-guide/README.md index 9f468b933d792..08158801788c9 100644 --- a/src/doc/rustc-dev-guide/README.md +++ b/src/doc/rustc-dev-guide/README.md @@ -77,7 +77,7 @@ This repository is linked to `rust-lang/rust` as a [josh](https://josh-project.g You'll need to install `josh-proxy` locally via ``` -cargo +stable install josh-proxy --git https://github.com/josh-project/josh --tag r24.10.04 +cargo install josh-proxy --git https://github.com/josh-project/josh --tag r24.10.04 ``` Older versions of `josh-proxy` may not round trip commits losslessly so it is important to install this exact version. @@ -106,5 +106,5 @@ You may observe "Nothing to pull" even if you *know* rustc-pull has something to To minimize the likelihood of this happening, you may wish to keep a separate *minimal* git config that *only* has `[user]` entries from global git config, then repoint system git to use the minimal git config instead. E.g. ``` -GIT_CONFIG_GLOBAL=/path/to/minimal/gitconfig GIT_CONFIG_SYSTEM='' cargo +stable run --manifest-path josh-sync/Cargo.toml -- rustc-pull +GIT_CONFIG_GLOBAL=/path/to/minimal/gitconfig GIT_CONFIG_SYSTEM='' cargo run --manifest-path josh-sync/Cargo.toml -- rustc-pull ``` diff --git a/src/doc/rustc-dev-guide/rust-version b/src/doc/rustc-dev-guide/rust-version index dc52e0928cc4c..67fa25f22288f 100644 --- a/src/doc/rustc-dev-guide/rust-version +++ b/src/doc/rustc-dev-guide/rust-version @@ -1 +1 @@ -b8005bff3248cfc6e327faf4fa631ac49bb49ba9 +deb947971c8748f5c6203548ce4af9022f21eaf0 diff --git a/src/doc/rustc-dev-guide/src/SUMMARY.md b/src/doc/rustc-dev-guide/src/SUMMARY.md index 68112d06167a0..d6ec803a60a38 100644 --- a/src/doc/rustc-dev-guide/src/SUMMARY.md +++ b/src/doc/rustc-dev-guide/src/SUMMARY.md @@ -1,6 +1,7 @@ # Summary [Getting Started](./getting-started.md) + [About this guide](./about-this-guide.md) --- @@ -231,9 +232,13 @@ --- [Appendix A: Background topics](./appendix/background.md) + [Appendix B: Glossary](./appendix/glossary.md) + [Appendix C: Code Index](./appendix/code-index.md) + [Appendix D: Compiler Lecture Series](./appendix/compiler-lecture.md) + [Appendix E: Bibliography](./appendix/bibliography.md) [Appendix Z: HumorRust](./appendix/humorust.md) diff --git a/src/doc/rustc-dev-guide/src/autodiff/installation.md b/src/doc/rustc-dev-guide/src/autodiff/installation.md index dbea9dbe14451..f3c11395523bf 100644 --- a/src/doc/rustc-dev-guide/src/autodiff/installation.md +++ b/src/doc/rustc-dev-guide/src/autodiff/installation.md @@ -25,9 +25,10 @@ rustup toolchain install nightly # enables -Z unstable-options You can then run our test cases: ```bash -./x.py test --stage 1 library tests/ui/autodiff -./x.py test --stage 1 library tests/codegen/autodiff -./x.py test --stage 1 library tests/pretty/autodiff* +./x.py test --stage 1 tests/codegen/autodiff +./x.py test --stage 1 tests/pretty/autodiff +./x.py test --stage 1 tests/ui/autodiff +./x.py test --stage 1 tests/ui/feature-gates/feature-gate-autodiff.rs ``` Autodiff is still experimental, so if you want to use it in your own projects, you will need to add `lto="fat"` to your Cargo.toml @@ -44,7 +45,7 @@ apt install wget vim python3 git curl libssl-dev pkg-config lld ninja-build cmak ``` Then build rustc in a slightly altered way: ```bash -git clone --depth=1 https://github.com/EnzymeAD/rust.git +git clone --depth=1 https://github.com/rust-lang/rust.git cd rust ./configure --enable-llvm-link-shared --enable-llvm-plugins --enable-llvm-enzyme --release-channel=nightly --enable-llvm-assertions --enable-clang --enable-lld --enable-option-checking --enable-ninja --disable-docs ./x dist @@ -54,7 +55,8 @@ We then copy the tarball to our host. The dockerid is the newest entry under `do docker cp :/rust/build/dist/rust-nightly-x86_64-unknown-linux-gnu.tar.gz rust-nightly-x86_64-unknown-linux-gnu.tar.gz ``` Afterwards we can create a new (pre-release) tag on the EnzymeAD/rust repository and make a PR against the EnzymeAD/enzyme-explorer repository to update the tag. -Remember to ping `tgymnich` on the PR to run his update script. +Remember to ping `tgymnich` on the PR to run his update script. Note: We should archive EnzymeAD/rust and update the instructions here. The explorer should soon +be able to get the rustc toolchain from the official rust servers. ## Build instruction for Enzyme itself diff --git a/src/doc/rustc-dev-guide/src/building/bootstrapping/what-bootstrapping-does.md b/src/doc/rustc-dev-guide/src/building/bootstrapping/what-bootstrapping-does.md index 1dd5402f4cde7..ffcfe25962572 100644 --- a/src/doc/rustc-dev-guide/src/building/bootstrapping/what-bootstrapping-does.md +++ b/src/doc/rustc-dev-guide/src/building/bootstrapping/what-bootstrapping-does.md @@ -394,8 +394,8 @@ will be rare to want to use it. Finally, `MAGIC_EXTRA_RUSTFLAGS` bypasses the this is `compiletest`. For unit tests and doc tests this is the `libtest` runner. -Most test runner accept `--help`, which you can use to find out the options -accepted by the runner. +Most test runners accept `--help`, +which you can use to find out the options accepted by the runner. ## Environment Variables diff --git a/src/doc/rustc-dev-guide/src/git.md b/src/doc/rustc-dev-guide/src/git.md index 177495b536212..8118ddff10c92 100644 --- a/src/doc/rustc-dev-guide/src/git.md +++ b/src/doc/rustc-dev-guide/src/git.md @@ -38,13 +38,13 @@ If you've cloned your fork, then you will be able to reference it with `origin` in your local repo. It may be helpful to also set up a remote for the official rust-lang/rust repo via -```sh +```console git remote add upstream https://github.com/rust-lang/rust.git ``` if you're using HTTPS, or -```sh +```console git remote add upstream git@github.com:rust-lang/rust.git ``` @@ -112,7 +112,7 @@ See [Rebasing](#rebasing) for more about rebasing. This is not a problem from git's perspective. If you run `git remote -v`, it will say something like this: -``` +```console $ git remote -v origin git@github.com:jyn514/rust.git (fetch) origin git@github.com:jyn514/rust.git (push) @@ -158,11 +158,11 @@ To fix it, do the following things: ### I see "error: cannot rebase" when I try to rebase These are two common errors to see when rebasing: -``` +```console error: cannot rebase: Your index contains uncommitted changes. error: Please commit or stash them. ``` -``` +```console error: cannot rebase: You have unstaged changes. error: Please commit or stash them. ``` @@ -174,7 +174,7 @@ commit your changes, or make a temporary commit called a "stash" to have them st when you finish rebasing. You may want to configure git to make this "stash" automatically, which will prevent the "cannot rebase" error in nearly all cases: -``` +```console git config --global rebase.autostash true ``` @@ -205,7 +205,7 @@ git reset --hard master `git push` will not work properly and say something like this: -``` +```console ! [rejected] issue-xxxxx -> issue-xxxxx (non-fast-forward) error: failed to push some refs to 'https://github.com/username/rust.git' hint: Updates were rejected because the tip of your current branch is behind @@ -226,7 +226,7 @@ didn't write, it likely means you're trying to rebase over the wrong branch. For have a `rust-lang/rust` remote `upstream`, but ran `git rebase origin/master` instead of `git rebase upstream/master`. The fix is to abort the rebase and use the correct branch instead: -``` +```console git rebase --abort git rebase -i upstream/master ``` @@ -243,7 +243,7 @@ When updating your local repository with `git pull`, you may notice that sometim Git says you have modified some files that you have never edited. For example, running `git status` gives you something like (note the `new commits` mention): -``` +```console On branch master Your branch is up to date with 'origin/master'. @@ -256,9 +256,12 @@ Changes not staged for commit: no changes added to commit (use "git add" and/or "git commit -a") ``` -These changes are not changes to files: they are changes to submodules (more on this -[later](#git-submodules)). To get rid of those, run `./x --help`, which will automatically update -the submodules. +These changes are not changes to files: they are changes to submodules (more on this [later](#git-submodules)). +To get rid of those: + +```console +git submodule update +``` Some submodules are not actually needed; for example, `src/llvm-project` doesn't need to be checked out if you're using `download-ci-llvm`. To avoid having to keep fetching its history, you can use @@ -278,12 +281,12 @@ merged. To do that, you need to rebase your work on top of rust-lang/rust. To rebase your feature branch on top of the newest version of the master branch of rust-lang/rust, checkout your branch, and then run this command: -``` +```console git pull --rebase https://github.com/rust-lang/rust.git master ``` > If you are met with the following error: -> ``` +> ```console > error: cannot pull with rebase: Your index contains uncommitted changes. > error: please commit or stash them. > ``` @@ -300,13 +303,13 @@ reapply the changes fails because your changes conflicted with other changes that have been made. You can tell that this happened because you'll see lines in the output that look like -``` +```console CONFLICT (content): Merge conflict in file.rs ``` When you open these files, you'll see sections of the form -``` +```console <<<<<<< HEAD Original code ======= @@ -346,7 +349,7 @@ will keep it up-to-date. You will also want to rebase your feature branches up-to-date as well. After pulling, you can checkout the feature branches and rebase them: -``` +```console git checkout master git pull upstream master --ff-only # to make certain there are no merge commits git rebase master feature_branch @@ -384,7 +387,7 @@ change the order in which they are applied, or "squash" them into each other. Alternatively, you can sacrifice the commit history like this: -``` +```console # squash all the changes into one commit so you only have to worry about conflicts once git rebase -i --keep-base master # and squash all changes along the way git rebase master @@ -422,7 +425,7 @@ it shows you the differences between your old diff and your new diff. Here's an example of `git range-diff` output (taken from [Git's docs][range-diff-example-docs]): -``` +```console -: ------- > 1: 0ddba11 Prepare for the inevitable! 1: c0debee = 2: cab005e Add a helpful message at the start 2: f00dbal ! 3: decafe1 Describe a bug @@ -499,7 +502,7 @@ Git and Github's default diff view for large moves *within* a file is quite poor line as deleted and each line as added, forcing you to compare each line yourself. Git has an option to show moved lines in a different color: -``` +```console git log -p --color-moved=dimmed-zebra --color-moved-ws=allow-indentation-change ``` @@ -515,7 +518,7 @@ that was force-pushed to make sure there are no unexpected changes. Many large files in the repo are autogenerated. To view a diff that ignores changes to those files, you can use the following syntax (e.g. Cargo.lock): -``` +```console git log -p ':!Cargo.lock' ``` @@ -545,7 +548,7 @@ The contents of submodules are ignored by Git: submodules are in some sense isol from the rest of the repository. However, if you try to `cd src/llvm-project` and then run `git status`: -``` +```console HEAD detached at 9567f08afc943 nothing to commit, working tree clean ``` @@ -576,7 +579,7 @@ that Git can nicely and fairly conveniently handle for us. Sometimes you might run into (when you run `git status`) -``` +```console Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git restore ..." to discard changes in working directory) @@ -586,7 +589,7 @@ Changes not staged for commit: and when you try to run `git submodule update` it breaks horribly with errors like -``` +```console error: RPC failed; curl 92 HTTP/2 stream 7 was not closed cleanly: CANCEL (err 8) error: 2782 bytes of body are still expected fetch-pack: unexpected disconnect while reading sideband packet @@ -597,8 +600,8 @@ fatal: Fetched in submodule path 'src/llvm-project', but it did not contain 5a51 If you see `(new commits, modified content)` you can run -```bash -$ git submodule foreach git reset --hard +```console +git submodule foreach git reset --hard ``` and then try `git submodule update` again. @@ -607,7 +610,7 @@ and then try `git submodule update` again. If that doesn't work, you can try to deinit all git submodules... -``` +```console git submodule deinit -f --all ``` @@ -618,7 +621,7 @@ completely messed up for some reason. Sometimes, for some forsaken reason, you might run into -```text +```console fatal: not a git repository: src/gcc/../../.git/modules/src/gcc ``` diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 6ecb67c776ca9..622a410837b54 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2213,9 +2213,9 @@ pub(crate) fn clean_middle_ty<'tcx>( })) } - ty::Alias(ty::Weak, data) => { + ty::Alias(ty::Free, data) => { if cx.tcx.features().lazy_type_alias() { - // Weak type alias `data` represents the `type X` in `type X = Y`. If we need `Y`, + // Free type alias `data` represents the `type X` in `type X = Y`. If we need `Y`, // we need to use `type_of`. let path = clean_middle_path( cx, diff --git a/src/librustdoc/clean/render_macro_matchers.rs b/src/librustdoc/clean/render_macro_matchers.rs index fc99dd08b78a6..e967fd406093a 100644 --- a/src/librustdoc/clean/render_macro_matchers.rs +++ b/src/librustdoc/clean/render_macro_matchers.rs @@ -34,20 +34,20 @@ pub(super) fn render_macro_matcher(tcx: TyCtxt<'_>, matcher: &TokenTree) -> Stri // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~! // ) => {...}; // } - printer.cbox(8); + let cb = printer.cbox(8); printer.word("("); printer.zerobreak(); - printer.ibox(0); + let ib = printer.ibox(0); match matcher { TokenTree::Delimited(_span, _spacing, _delim, tts) => print_tts(&mut printer, tts), // Matcher which is not a Delimited is unexpected and should've failed // to compile, but we render whatever it is wrapped in parens. TokenTree::Token(..) => print_tt(&mut printer, matcher), } - printer.end(); + printer.end(ib); printer.break_offset_if_not_bol(0, -4); printer.word(")"); - printer.end(); + printer.end(cb); printer.s.eof() } diff --git a/src/librustdoc/doctest/make.rs b/src/librustdoc/doctest/make.rs index d5c965f7053e0..4194abc8d5742 100644 --- a/src/librustdoc/doctest/make.rs +++ b/src/librustdoc/doctest/make.rs @@ -407,17 +407,27 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result { is_extern_crate = check_item(&item, &mut info, crate_name); } - StmtKind::Expr(ref expr) if matches!(expr.kind, ast::ExprKind::Err(_)) => { - reset_error_count(&psess); - return Err(()); + StmtKind::Expr(ref expr) => { + if matches!(expr.kind, ast::ExprKind::Err(_)) { + reset_error_count(&psess); + return Err(()); + } + has_non_items = true; } - StmtKind::MacCall(ref mac_call) if !info.has_main_fn => { + // We assume that the macro calls will expand to item(s) even though they could + // expand to statements and expressions. And the simple fact that we're trying + // to retrieve a `main` function inside it is a terrible idea. + StmtKind::MacCall(ref mac_call) => { + if info.has_main_fn { + continue; + } let mut iter = mac_call.mac.args.tokens.iter(); while let Some(token) = iter.next() { @@ -437,7 +447,9 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result {} + _ => { + has_non_items = true; + } } // Weirdly enough, the `Stmt` span doesn't include its attributes, so we need to @@ -462,6 +474,11 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result { diff --git a/src/tools/clippy/clippy_lints/src/dereference.rs b/src/tools/clippy/clippy_lints/src/dereference.rs index 7da5a530eaa3d..5edb5c235703e 100644 --- a/src/tools/clippy/clippy_lints/src/dereference.rs +++ b/src/tools/clippy/clippy_lints/src/dereference.rs @@ -853,7 +853,7 @@ impl TyCoercionStability { continue; }, ty::Param(_) if for_return => Self::Deref, - ty::Alias(ty::Weak | ty::Inherent, _) => unreachable!("should have been normalized away above"), + ty::Alias(ty::Free | ty::Inherent, _) => unreachable!("should have been normalized away above"), ty::Alias(ty::Projection, _) if !for_return && ty.has_non_region_param() => Self::Reborrow, ty::Infer(_) | ty::Error(_) diff --git a/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs b/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs index 67537a251da7f..1f142bc3ba63c 100644 --- a/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs +++ b/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs @@ -197,7 +197,7 @@ fn fn_inputs_has_impl_trait_ty(cx: &LateContext<'_>, def_id: LocalDefId) -> bool inputs.iter().any(|input| { matches!( input.kind(), - ty::Alias(ty::AliasTyKind::Weak, alias_ty) if cx.tcx.type_of(alias_ty.def_id).skip_binder().is_impl_trait() + ty::Alias(ty::AliasTyKind::Free, alias_ty) if cx.tcx.type_of(alias_ty.def_id).skip_binder().is_impl_trait() ) }) } diff --git a/src/tools/clippy/tests/ui/checked_unwrap/simple_conditionals.stderr b/src/tools/clippy/tests/ui/checked_unwrap/simple_conditionals.stderr index bdac1e42309d8..ad3c420270c14 100644 --- a/src/tools/clippy/tests/ui/checked_unwrap/simple_conditionals.stderr +++ b/src/tools/clippy/tests/ui/checked_unwrap/simple_conditionals.stderr @@ -236,7 +236,7 @@ LL | if result.is_ok() { LL | result.as_mut().unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^ -error: creating a shared reference to mutable static is discouraged +error: creating a shared reference to mutable static --> tests/ui/checked_unwrap/simple_conditionals.rs:183:12 | LL | if X.is_some() { diff --git a/tests/rustdoc-ui/doctest/auxiliary/macro-after-main.rs b/tests/rustdoc-ui/doctest/auxiliary/macro-after-main.rs new file mode 100644 index 0000000000000..ed7584b742533 --- /dev/null +++ b/tests/rustdoc-ui/doctest/auxiliary/macro-after-main.rs @@ -0,0 +1 @@ +use std::string::String; diff --git a/tests/rustdoc-ui/doctest/failed-doctest-extra-semicolon-on-item.rs b/tests/rustdoc-ui/doctest/failed-doctest-extra-semicolon-on-item.rs index 508faadcf6729..ca5dd78746789 100644 --- a/tests/rustdoc-ui/doctest/failed-doctest-extra-semicolon-on-item.rs +++ b/tests/rustdoc-ui/doctest/failed-doctest-extra-semicolon-on-item.rs @@ -4,12 +4,12 @@ //@ compile-flags:--test //@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR" //@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME" -//@ failure-status: 101 +//@ check-pass /// /// /// ```rust -/// struct S {}; // unexpected semicolon after struct def +/// struct S {}; /// /// fn main() { /// assert_eq!(0, 1); diff --git a/tests/rustdoc-ui/doctest/failed-doctest-extra-semicolon-on-item.stdout b/tests/rustdoc-ui/doctest/failed-doctest-extra-semicolon-on-item.stdout index 9eb8b391e7809..1068b98cb0fbb 100644 --- a/tests/rustdoc-ui/doctest/failed-doctest-extra-semicolon-on-item.stdout +++ b/tests/rustdoc-ui/doctest/failed-doctest-extra-semicolon-on-item.stdout @@ -1,29 +1,6 @@ running 1 test -test $DIR/failed-doctest-extra-semicolon-on-item.rs - m (line 11) ... FAILED +test $DIR/failed-doctest-extra-semicolon-on-item.rs - m (line 11) ... ok -failures: - ----- $DIR/failed-doctest-extra-semicolon-on-item.rs - m (line 11) stdout ---- -error: expected item, found `;` - --> $DIR/failed-doctest-extra-semicolon-on-item.rs:12:12 - | -LL | struct S {}; // unexpected semicolon after struct def - | ^ - | - = help: braced struct declarations are not followed by a semicolon -help: remove this semicolon - | -LL - struct S {}; // unexpected semicolon after struct def -LL + struct S {} // unexpected semicolon after struct def - | - -error: aborting due to 1 previous error - -Couldn't compile the test. - -failures: - $DIR/failed-doctest-extra-semicolon-on-item.rs - m (line 11) - -test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME diff --git a/tests/rustdoc-ui/doctest/macro-after-main.rs b/tests/rustdoc-ui/doctest/macro-after-main.rs new file mode 100644 index 0000000000000..0a42343f1c272 --- /dev/null +++ b/tests/rustdoc-ui/doctest/macro-after-main.rs @@ -0,0 +1,16 @@ +// This test checks a corner case where the macro calls used to be skipped, +// making them considered as statement, and therefore some cases where +// `include!` macro was then put into a function body, making the doctest +// compilation fail. + +//@ compile-flags:--test +//@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR" +//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME" +//@ check-pass + +//! ``` +//! include!("./auxiliary/macro-after-main.rs"); +//! +//! fn main() {} +//! eprintln!(); +//! ``` diff --git a/tests/rustdoc-ui/doctest/macro-after-main.stdout b/tests/rustdoc-ui/doctest/macro-after-main.stdout new file mode 100644 index 0000000000000..72ffe2b5a27c5 --- /dev/null +++ b/tests/rustdoc-ui/doctest/macro-after-main.stdout @@ -0,0 +1,6 @@ + +running 1 test +test $DIR/macro-after-main.rs - (line 11) ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME + diff --git a/tests/rustdoc-ui/doctest/test-main-alongside-exprs.rs b/tests/rustdoc-ui/doctest/test-main-alongside-exprs.rs new file mode 100644 index 0000000000000..ee2299c0fd87e --- /dev/null +++ b/tests/rustdoc-ui/doctest/test-main-alongside-exprs.rs @@ -0,0 +1,22 @@ +// This test ensures that if there is an expression alongside a `main` +// function, it will not consider the entire code to be part of the `main` +// function and will generate its own function to wrap everything. +// +// This is a regression test for: +// * +// * +//@ compile-flags:--test +//@ normalize-stdout: "tests/rustdoc-ui/doctest" -> "$$DIR" +//@ normalize-stdout: "finished in \d+\.\d+s" -> "finished in $$TIME" +//@ check-pass + +#![crate_name = "foo"] + +//! ``` +//! # if cfg!(miri) { return; } +//! use std::ops::Deref; +//! +//! fn main() { +//! println!("Hi!"); +//! } +//! ``` diff --git a/tests/rustdoc-ui/doctest/test-main-alongside-exprs.stdout b/tests/rustdoc-ui/doctest/test-main-alongside-exprs.stdout new file mode 100644 index 0000000000000..90d7c3546bf10 --- /dev/null +++ b/tests/rustdoc-ui/doctest/test-main-alongside-exprs.stdout @@ -0,0 +1,6 @@ + +running 1 test +test $DIR/test-main-alongside-exprs.rs - (line 15) ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME + diff --git a/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.rs b/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.rs index b09c96ada8ab6..5106ba6c86cd4 100644 --- a/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.rs +++ b/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.rs @@ -17,7 +17,7 @@ impl Foo { fn main() { unsafe { let sfoo: *mut Foo = &mut SFOO; - //~^ WARN mutable reference to mutable static is discouraged [static_mut_refs] + //~^ WARN mutable reference to mutable static [static_mut_refs] let x = (*sfoo).x(); (*sfoo).x[1] += 1; *x += 1; diff --git a/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.stderr b/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.stderr index a392177ffe2f3..4e19fd81735e1 100644 --- a/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.stderr +++ b/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.stderr @@ -1,4 +1,4 @@ -warning: creating a mutable reference to mutable static is discouraged +warning: creating a mutable reference to mutable static --> $DIR/borrowck-unsafe-static-mutable-borrows.rs:19:30 | LL | let sfoo: *mut Foo = &mut SFOO; diff --git a/tests/ui/consts/const_let_assign2.rs b/tests/ui/consts/const_let_assign2.rs index e8ebba7b20887..5349ae31bb4df 100644 --- a/tests/ui/consts/const_let_assign2.rs +++ b/tests/ui/consts/const_let_assign2.rs @@ -16,7 +16,7 @@ static mut BB: AA = AA::new(); fn main() { let ptr = unsafe { &mut BB }; - //~^ WARN mutable reference to mutable static is discouraged [static_mut_refs] + //~^ WARN mutable reference to mutable static [static_mut_refs] for a in ptr.data.iter() { println!("{}", a); } diff --git a/tests/ui/consts/const_let_assign2.stderr b/tests/ui/consts/const_let_assign2.stderr index 0d76f142d174d..1bb560437b60e 100644 --- a/tests/ui/consts/const_let_assign2.stderr +++ b/tests/ui/consts/const_let_assign2.stderr @@ -1,4 +1,4 @@ -warning: creating a mutable reference to mutable static is discouraged +warning: creating a mutable reference to mutable static --> $DIR/const_let_assign2.rs:18:24 | LL | let ptr = unsafe { &mut BB }; diff --git a/tests/ui/issues/issue-39367.rs b/tests/ui/issues/issue-39367.rs index 937d6c4b9e0b0..68b4d28aae3ba 100644 --- a/tests/ui/issues/issue-39367.rs +++ b/tests/ui/issues/issue-39367.rs @@ -20,7 +20,7 @@ fn arena() -> &'static ArenaSet> { static mut ONCE: Once = Once::new(); ONCE.call_once(|| { - //~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] + //~^ WARN creating a shared reference to mutable static [static_mut_refs] DATA = transmute ::>>, *const ArenaSet>> (Box::new(__static_ref_initialize())); diff --git a/tests/ui/issues/issue-39367.stderr b/tests/ui/issues/issue-39367.stderr index 333c9f9634a31..df21c09983e26 100644 --- a/tests/ui/issues/issue-39367.stderr +++ b/tests/ui/issues/issue-39367.stderr @@ -1,4 +1,4 @@ -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/issue-39367.rs:22:13 | LL | / ONCE.call_once(|| { diff --git a/tests/ui/lazy-type-alias/extern-crate-has-eager-type-aliases.rs b/tests/ui/lazy-type-alias/extern-crate-has-eager-type-aliases.rs index efd7763919840..3384698776056 100644 --- a/tests/ui/lazy-type-alias/extern-crate-has-eager-type-aliases.rs +++ b/tests/ui/lazy-type-alias/extern-crate-has-eager-type-aliases.rs @@ -9,9 +9,9 @@ #![allow(incomplete_features)] // This used to crash when we were computing the variances of `Struct` since we would convert -// `eager::Alias` to a weak projection due to the presence of `#![feature(lazy_type_alias)]` in +// `eager::Alias` to a weak alias due to the presence of `#![feature(lazy_type_alias)]` in // this (!) crate and subsequently attempt to obtain the variances of the type alias associated with -// the weak projection which would panic because we don't compute this information for eager type +// the weak alias which would panic because we don't compute this information for eager type // aliases at all. struct Struct(eager::Alias); diff --git a/tests/ui/lint/static-mut-refs.e2021.stderr b/tests/ui/lint/static-mut-refs.e2021.stderr index 00a2ca99f241c..320e0cee8e8b7 100644 --- a/tests/ui/lint/static-mut-refs.e2021.stderr +++ b/tests/ui/lint/static-mut-refs.e2021.stderr @@ -1,4 +1,4 @@ -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:38:18 | LL | let _y = &X; @@ -12,7 +12,7 @@ help: use `&raw const` instead to create a raw pointer LL | let _y = &raw const X; | +++++++++ -warning: creating a mutable reference to mutable static is discouraged +warning: creating a mutable reference to mutable static --> $DIR/static-mut-refs.rs:42:18 | LL | let _y = &mut X; @@ -25,7 +25,7 @@ help: use `&raw mut` instead to create a raw pointer LL | let _y = &raw mut X; | +++ -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:50:22 | LL | let ref _a = X; @@ -34,7 +34,7 @@ LL | let ref _a = X; = note: for more information, see = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:54:25 | LL | let (_b, _c) = (&X, &Y); @@ -47,7 +47,7 @@ help: use `&raw const` instead to create a raw pointer LL | let (_b, _c) = (&raw const X, &Y); | +++++++++ -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:54:29 | LL | let (_b, _c) = (&X, &Y); @@ -60,7 +60,7 @@ help: use `&raw const` instead to create a raw pointer LL | let (_b, _c) = (&X, &raw const Y); | +++++++++ -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:60:13 | LL | foo(&X); @@ -73,7 +73,7 @@ help: use `&raw const` instead to create a raw pointer LL | foo(&raw const X); | +++++++++ -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:66:17 | LL | let _ = Z.len(); @@ -82,7 +82,7 @@ LL | let _ = Z.len(); = note: for more information, see = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:72:33 | LL | let _ = format!("{:?}", Z); @@ -91,7 +91,7 @@ LL | let _ = format!("{:?}", Z); = note: for more information, see = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:76:18 | LL | let _v = &A.value; @@ -104,7 +104,7 @@ help: use `&raw const` instead to create a raw pointer LL | let _v = &raw const A.value; | +++++++++ -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:80:18 | LL | let _s = &A.s.value; @@ -117,7 +117,7 @@ help: use `&raw const` instead to create a raw pointer LL | let _s = &raw const A.s.value; | +++++++++ -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:84:22 | LL | let ref _v = A.value; @@ -126,7 +126,7 @@ LL | let ref _v = A.value; = note: for more information, see = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives -warning: creating a mutable reference to mutable static is discouraged +warning: creating a mutable reference to mutable static --> $DIR/static-mut-refs.rs:14:14 | LL | &mut ($x.0) diff --git a/tests/ui/lint/static-mut-refs.e2024.stderr b/tests/ui/lint/static-mut-refs.e2024.stderr index ff41f316250d1..bf7ffc62ce17b 100644 --- a/tests/ui/lint/static-mut-refs.e2024.stderr +++ b/tests/ui/lint/static-mut-refs.e2024.stderr @@ -1,4 +1,4 @@ -error: creating a shared reference to mutable static is discouraged +error: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:38:18 | LL | let _y = &X; @@ -12,7 +12,7 @@ help: use `&raw const` instead to create a raw pointer LL | let _y = &raw const X; | +++++++++ -error: creating a mutable reference to mutable static is discouraged +error: creating a mutable reference to mutable static --> $DIR/static-mut-refs.rs:42:18 | LL | let _y = &mut X; @@ -25,7 +25,7 @@ help: use `&raw mut` instead to create a raw pointer LL | let _y = &raw mut X; | +++ -error: creating a shared reference to mutable static is discouraged +error: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:50:22 | LL | let ref _a = X; @@ -34,7 +34,7 @@ LL | let ref _a = X; = note: for more information, see = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives -error: creating a shared reference to mutable static is discouraged +error: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:54:25 | LL | let (_b, _c) = (&X, &Y); @@ -47,7 +47,7 @@ help: use `&raw const` instead to create a raw pointer LL | let (_b, _c) = (&raw const X, &Y); | +++++++++ -error: creating a shared reference to mutable static is discouraged +error: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:54:29 | LL | let (_b, _c) = (&X, &Y); @@ -60,7 +60,7 @@ help: use `&raw const` instead to create a raw pointer LL | let (_b, _c) = (&X, &raw const Y); | +++++++++ -error: creating a shared reference to mutable static is discouraged +error: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:60:13 | LL | foo(&X); @@ -73,7 +73,7 @@ help: use `&raw const` instead to create a raw pointer LL | foo(&raw const X); | +++++++++ -error: creating a shared reference to mutable static is discouraged +error: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:66:17 | LL | let _ = Z.len(); @@ -82,7 +82,7 @@ LL | let _ = Z.len(); = note: for more information, see = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives -error: creating a shared reference to mutable static is discouraged +error: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:72:33 | LL | let _ = format!("{:?}", Z); @@ -91,7 +91,7 @@ LL | let _ = format!("{:?}", Z); = note: for more information, see = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives -error: creating a shared reference to mutable static is discouraged +error: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:76:18 | LL | let _v = &A.value; @@ -104,7 +104,7 @@ help: use `&raw const` instead to create a raw pointer LL | let _v = &raw const A.value; | +++++++++ -error: creating a shared reference to mutable static is discouraged +error: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:80:18 | LL | let _s = &A.s.value; @@ -117,7 +117,7 @@ help: use `&raw const` instead to create a raw pointer LL | let _s = &raw const A.s.value; | +++++++++ -error: creating a shared reference to mutable static is discouraged +error: creating a shared reference to mutable static --> $DIR/static-mut-refs.rs:84:22 | LL | let ref _v = A.value; @@ -126,7 +126,7 @@ LL | let ref _v = A.value; = note: for more information, see = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives -error: creating a mutable reference to mutable static is discouraged +error: creating a mutable reference to mutable static --> $DIR/static-mut-refs.rs:14:14 | LL | &mut ($x.0) diff --git a/tests/ui/lint/static-mut-refs.rs b/tests/ui/lint/static-mut-refs.rs index 1040dfcae7a95..d2511fb5b128a 100644 --- a/tests/ui/lint/static-mut-refs.rs +++ b/tests/ui/lint/static-mut-refs.rs @@ -12,8 +12,8 @@ static mut FOO: (u32, u32) = (1, 2); macro_rules! bar { ($x:expr) => { &mut ($x.0) - //[e2021]~^ WARN creating a mutable reference to mutable static is discouraged [static_mut_refs] - //[e2024]~^^ ERROR creating a mutable reference to mutable static is discouraged [static_mut_refs] + //[e2021]~^ WARN creating a mutable reference to mutable static [static_mut_refs] + //[e2024]~^^ ERROR creating a mutable reference to mutable static [static_mut_refs] }; } @@ -36,54 +36,54 @@ fn main() { unsafe { let _y = &X; - //[e2021]~^ WARN shared reference to mutable static is discouraged [static_mut_refs] - //[e2024]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] + //[e2021]~^ WARN shared reference to mutable static [static_mut_refs] + //[e2024]~^^ ERROR shared reference to mutable static [static_mut_refs] let _y = &mut X; - //[e2021]~^ WARN mutable reference to mutable static is discouraged [static_mut_refs] - //[e2024]~^^ ERROR mutable reference to mutable static is discouraged [static_mut_refs] + //[e2021]~^ WARN mutable reference to mutable static [static_mut_refs] + //[e2024]~^^ ERROR mutable reference to mutable static [static_mut_refs] let _z = &raw mut X; let _p = &raw const X; let ref _a = X; - //[e2021]~^ WARN shared reference to mutable static is discouraged [static_mut_refs] - //[e2024]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] + //[e2021]~^ WARN shared reference to mutable static [static_mut_refs] + //[e2024]~^^ ERROR shared reference to mutable static [static_mut_refs] let (_b, _c) = (&X, &Y); - //[e2021]~^ WARN shared reference to mutable static is discouraged [static_mut_refs] - //[e2024]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] - //[e2021]~^^^ WARN shared reference to mutable static is discouraged [static_mut_refs] - //[e2024]~^^^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] + //[e2021]~^ WARN shared reference to mutable static [static_mut_refs] + //[e2024]~^^ ERROR shared reference to mutable static [static_mut_refs] + //[e2021]~^^^ WARN shared reference to mutable static [static_mut_refs] + //[e2024]~^^^^ ERROR shared reference to mutable static [static_mut_refs] foo(&X); - //[e2021]~^ WARN shared reference to mutable static is discouraged [static_mut_refs] - //[e2024]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] + //[e2021]~^ WARN shared reference to mutable static [static_mut_refs] + //[e2024]~^^ ERROR shared reference to mutable static [static_mut_refs] static mut Z: &[i32; 3] = &[0, 1, 2]; let _ = Z.len(); - //[e2021]~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] - //[e2024]~^^ ERROR creating a shared reference to mutable static is discouraged [static_mut_refs] + //[e2021]~^ WARN creating a shared reference to mutable static [static_mut_refs] + //[e2024]~^^ ERROR creating a shared reference to mutable static [static_mut_refs] let _ = Z[0]; let _ = format!("{:?}", Z); - //[e2021]~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] - //[e2024]~^^ ERROR creating a shared reference to mutable static is discouraged [static_mut_refs] + //[e2021]~^ WARN creating a shared reference to mutable static [static_mut_refs] + //[e2024]~^^ ERROR creating a shared reference to mutable static [static_mut_refs] let _v = &A.value; - //[e2021]~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] - //[e2024]~^^ ERROR creating a shared reference to mutable static is discouraged [static_mut_refs] + //[e2021]~^ WARN creating a shared reference to mutable static [static_mut_refs] + //[e2024]~^^ ERROR creating a shared reference to mutable static [static_mut_refs] let _s = &A.s.value; - //[e2021]~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] - //[e2024]~^^ ERROR creating a shared reference to mutable static is discouraged [static_mut_refs] + //[e2021]~^ WARN creating a shared reference to mutable static [static_mut_refs] + //[e2024]~^^ ERROR creating a shared reference to mutable static [static_mut_refs] let ref _v = A.value; - //[e2021]~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] - //[e2024]~^^ ERROR creating a shared reference to mutable static is discouraged [static_mut_refs] + //[e2021]~^ WARN creating a shared reference to mutable static [static_mut_refs] + //[e2024]~^^ ERROR creating a shared reference to mutable static [static_mut_refs] let _x = bar!(FOO); diff --git a/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.rs b/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.rs index be882085c5c90..73597408d1061 100644 --- a/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.rs +++ b/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.rs @@ -15,7 +15,7 @@ struct S1 { impl S1 { fn new(_x: u64) -> S1 { S1 { a: unsafe { &mut X1 } } - //~^ WARN mutable reference to mutable static is discouraged [static_mut_refs] + //~^ WARN mutable reference to mutable static [static_mut_refs] } } diff --git a/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.stderr b/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.stderr index 9dbd676969364..8268f5df23655 100644 --- a/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.stderr +++ b/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.stderr @@ -1,4 +1,4 @@ -warning: creating a mutable reference to mutable static is discouraged +warning: creating a mutable reference to mutable static --> $DIR/borrowck-thread-local-static-mut-borrow-outlives-fn.rs:17:26 | LL | S1 { a: unsafe { &mut X1 } } diff --git a/tests/ui/statics/issue-15261.rs b/tests/ui/statics/issue-15261.rs index ed79a201488f3..f8c5ebaa54840 100644 --- a/tests/ui/statics/issue-15261.rs +++ b/tests/ui/statics/issue-15261.rs @@ -2,10 +2,9 @@ #![allow(dead_code)] #![allow(non_upper_case_globals)] - static mut n_mut: usize = 0; static n: &'static usize = unsafe { &n_mut }; -//~^ WARN shared reference to mutable static is discouraged [static_mut_refs] +//~^ WARN shared reference to mutable static [static_mut_refs] fn main() {} diff --git a/tests/ui/statics/issue-15261.stderr b/tests/ui/statics/issue-15261.stderr index 7e6aebcbb1f0e..d2dd762aa66eb 100644 --- a/tests/ui/statics/issue-15261.stderr +++ b/tests/ui/statics/issue-15261.stderr @@ -1,5 +1,5 @@ -warning: creating a shared reference to mutable static is discouraged - --> $DIR/issue-15261.rs:8:37 +warning: creating a shared reference to mutable static + --> $DIR/issue-15261.rs:7:37 | LL | static n: &'static usize = unsafe { &n_mut }; | ^^^^^^ shared reference to mutable static diff --git a/tests/ui/statics/static-mut-shared-parens.rs b/tests/ui/statics/static-mut-shared-parens.rs index 8e58152e27acd..7eda7d67d9248 100644 --- a/tests/ui/statics/static-mut-shared-parens.rs +++ b/tests/ui/statics/static-mut-shared-parens.rs @@ -1,13 +1,12 @@ //Missing paren in diagnostic msg: https://github.com/rust-lang/rust/issues/131977 //@check-pass - static mut TEST: usize = 0; fn main() { let _ = unsafe { (&TEST) as *const usize }; - //~^WARN creating a shared reference to mutable static is discouraged + //~^WARN creating a shared reference to mutable static - let _ = unsafe { ((&mut TEST)) as *const usize }; - //~^WARN creating a mutable reference to mutable static is discouraged + let _ = unsafe { (&mut TEST) as *const usize }; + //~^WARN creating a mutable reference to mutable static } diff --git a/tests/ui/statics/static-mut-shared-parens.stderr b/tests/ui/statics/static-mut-shared-parens.stderr index 30a586c286ae7..3825e8efc4277 100644 --- a/tests/ui/statics/static-mut-shared-parens.stderr +++ b/tests/ui/statics/static-mut-shared-parens.stderr @@ -1,5 +1,5 @@ -warning: creating a shared reference to mutable static is discouraged - --> $DIR/static-mut-shared-parens.rs:8:22 +warning: creating a shared reference to mutable static + --> $DIR/static-mut-shared-parens.rs:7:22 | LL | let _ = unsafe { (&TEST) as *const usize }; | ^^^^^^^ shared reference to mutable static @@ -12,18 +12,18 @@ help: use `&raw const` instead to create a raw pointer LL | let _ = unsafe { (&raw const TEST) as *const usize }; | +++++++++ -warning: creating a mutable reference to mutable static is discouraged - --> $DIR/static-mut-shared-parens.rs:11:22 +warning: creating a mutable reference to mutable static + --> $DIR/static-mut-shared-parens.rs:10:22 | -LL | let _ = unsafe { ((&mut TEST)) as *const usize }; - | ^^^^^^^^^^^^^ mutable reference to mutable static +LL | let _ = unsafe { (&mut TEST) as *const usize }; + | ^^^^^^^^^^^ mutable reference to mutable static | = note: for more information, see = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives help: use `&raw mut` instead to create a raw pointer | -LL | let _ = unsafe { ((&raw mut TEST)) as *const usize }; - | +++ +LL | let _ = unsafe { (&raw mut TEST) as *const usize }; + | +++ warning: 2 warnings emitted diff --git a/tests/ui/statics/static-mut-xc.rs b/tests/ui/statics/static-mut-xc.rs index c23cc822ce741..878f5a8e0f70a 100644 --- a/tests/ui/statics/static-mut-xc.rs +++ b/tests/ui/statics/static-mut-xc.rs @@ -17,23 +17,23 @@ fn static_bound_set(a: &'static mut isize) { unsafe fn run() { assert_eq!(static_mut_xc::a, 3); - //~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] + //~^ WARN creating a shared reference to mutable static [static_mut_refs] static_mut_xc::a = 4; assert_eq!(static_mut_xc::a, 4); - //~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] + //~^ WARN creating a shared reference to mutable static [static_mut_refs] static_mut_xc::a += 1; assert_eq!(static_mut_xc::a, 5); - //~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] + //~^ WARN creating a shared reference to mutable static [static_mut_refs] static_mut_xc::a *= 3; assert_eq!(static_mut_xc::a, 15); - //~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] + //~^ WARN creating a shared reference to mutable static [static_mut_refs] static_mut_xc::a = -3; assert_eq!(static_mut_xc::a, -3); - //~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] + //~^ WARN creating a shared reference to mutable static [static_mut_refs] static_bound(&static_mut_xc::a); - //~^ WARN shared reference to mutable static is discouraged [static_mut_refs] + //~^ WARN shared reference to mutable static [static_mut_refs] static_bound_set(&mut static_mut_xc::a); - //~^ WARN mutable reference to mutable static is discouraged [static_mut_refs] + //~^ WARN mutable reference to mutable static [static_mut_refs] } pub fn main() { diff --git a/tests/ui/statics/static-mut-xc.stderr b/tests/ui/statics/static-mut-xc.stderr index 69f334a5636af..2d7a0553e925f 100644 --- a/tests/ui/statics/static-mut-xc.stderr +++ b/tests/ui/statics/static-mut-xc.stderr @@ -1,4 +1,4 @@ -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/static-mut-xc.rs:19:16 | LL | assert_eq!(static_mut_xc::a, 3); @@ -8,7 +8,7 @@ LL | assert_eq!(static_mut_xc::a, 3); = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives = note: `#[warn(static_mut_refs)]` on by default -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/static-mut-xc.rs:22:16 | LL | assert_eq!(static_mut_xc::a, 4); @@ -17,7 +17,7 @@ LL | assert_eq!(static_mut_xc::a, 4); = note: for more information, see = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/static-mut-xc.rs:25:16 | LL | assert_eq!(static_mut_xc::a, 5); @@ -26,7 +26,7 @@ LL | assert_eq!(static_mut_xc::a, 5); = note: for more information, see = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/static-mut-xc.rs:28:16 | LL | assert_eq!(static_mut_xc::a, 15); @@ -35,7 +35,7 @@ LL | assert_eq!(static_mut_xc::a, 15); = note: for more information, see = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/static-mut-xc.rs:31:16 | LL | assert_eq!(static_mut_xc::a, -3); @@ -44,7 +44,7 @@ LL | assert_eq!(static_mut_xc::a, -3); = note: for more information, see = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/static-mut-xc.rs:33:18 | LL | static_bound(&static_mut_xc::a); @@ -57,7 +57,7 @@ help: use `&raw const` instead to create a raw pointer LL | static_bound(&raw const static_mut_xc::a); | +++++++++ -warning: creating a mutable reference to mutable static is discouraged +warning: creating a mutable reference to mutable static --> $DIR/static-mut-xc.rs:35:22 | LL | static_bound_set(&mut static_mut_xc::a); diff --git a/tests/ui/statics/static-recursive.rs b/tests/ui/statics/static-recursive.rs index da23b54d1fca0..dab60dd8641bd 100644 --- a/tests/ui/statics/static-recursive.rs +++ b/tests/ui/statics/static-recursive.rs @@ -1,7 +1,7 @@ //@ run-pass static mut S: *const u8 = unsafe { &S as *const *const u8 as *const u8 }; -//~^ WARN shared reference to mutable static is discouraged [static_mut_refs] +//~^ WARN shared reference to mutable static [static_mut_refs] struct StaticDoubleLinked { prev: &'static StaticDoubleLinked, @@ -17,7 +17,7 @@ static L3: StaticDoubleLinked = StaticDoubleLinked { prev: &L2, next: &L1, data: pub fn main() { unsafe { assert_eq!(S, *(S as *const *const u8)); - //~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] + //~^ WARN creating a shared reference to mutable static [static_mut_refs] } let mut test_vec = Vec::new(); diff --git a/tests/ui/statics/static-recursive.stderr b/tests/ui/statics/static-recursive.stderr index 039934dfc692d..252807e2e5dea 100644 --- a/tests/ui/statics/static-recursive.stderr +++ b/tests/ui/statics/static-recursive.stderr @@ -1,4 +1,4 @@ -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/static-recursive.rs:3:36 | LL | static mut S: *const u8 = unsafe { &S as *const *const u8 as *const u8 }; @@ -12,7 +12,7 @@ help: use `&raw const` instead to create a raw pointer LL | static mut S: *const u8 = unsafe { &raw const S as *const *const u8 as *const u8 }; | +++++++++ -warning: creating a shared reference to mutable static is discouraged +warning: creating a shared reference to mutable static --> $DIR/static-recursive.rs:19:20 | LL | assert_eq!(S, *(S as *const *const u8));