diff options
Diffstat (limited to 'compiler')
30 files changed, 369 insertions, 307 deletions
diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 4ae18b4cf48..31dd358ad51 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -403,9 +403,10 @@ impl Default for Generics { /// A where-clause in a definition. #[derive(Clone, Encodable, Decodable, Debug)] pub struct WhereClause { - /// `true` if we ate a `where` token: this can happen - /// if we parsed no predicates (e.g. `struct Foo where {}`). - /// This allows us to pretty-print accurately. + /// `true` if we ate a `where` token. + /// + /// This can happen if we parsed no predicates, e.g., `struct Foo where {}`. + /// This allows us to pretty-print accurately and provide correct suggestion diagnostics. pub has_where_token: bool, pub predicates: ThinVec<WherePredicate>, pub span: Span, @@ -3007,18 +3008,29 @@ pub struct Trait { /// /// If there is no where clause, then this is `false` with `DUMMY_SP`. #[derive(Copy, Clone, Encodable, Decodable, Debug, Default)] -pub struct TyAliasWhereClause(pub bool, pub Span); +pub struct TyAliasWhereClause { + pub has_where_token: bool, + pub span: Span, +} + +/// The span information for the two where clauses on a `TyAlias`. +#[derive(Copy, Clone, Encodable, Decodable, Debug, Default)] +pub struct TyAliasWhereClauses { + /// Before the equals sign. + pub before: TyAliasWhereClause, + /// After the equals sign. + pub after: TyAliasWhereClause, + /// The index in `TyAlias.generics.where_clause.predicates` that would split + /// into predicates from the where clause before the equals sign and the ones + /// from the where clause after the equals sign. + pub split: usize, +} #[derive(Clone, Encodable, Decodable, Debug)] pub struct TyAlias { pub defaultness: Defaultness, pub generics: Generics, - /// The span information for the two where clauses (before equals, after equals) - pub where_clauses: (TyAliasWhereClause, TyAliasWhereClause), - /// The index in `generics.where_clause.predicates` that would split into - /// predicates from the where clause before the equals and the predicates - /// from the where clause after the equals - pub where_predicates_split: usize, + pub where_clauses: TyAliasWhereClauses, pub bounds: GenericBounds, pub ty: Option<P<Ty>>, } diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index 60bc21c6441..2faf3228d55 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -1079,8 +1079,8 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) { }) => { visit_defaultness(defaultness, vis); vis.visit_generics(generics); - vis.visit_span(&mut where_clauses.0.1); - vis.visit_span(&mut where_clauses.1.1); + vis.visit_span(&mut where_clauses.before.span); + vis.visit_span(&mut where_clauses.after.span); visit_bounds(bounds, vis); visit_opt(ty, |ty| vis.visit_ty(ty)); } @@ -1163,8 +1163,8 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>( }) => { visit_defaultness(defaultness, visitor); visitor.visit_generics(generics); - visitor.visit_span(&mut where_clauses.0.1); - visitor.visit_span(&mut where_clauses.1.1); + visitor.visit_span(&mut where_clauses.before.span); + visitor.visit_span(&mut where_clauses.after.span); visit_bounds(bounds, visitor); visit_opt(ty, |ty| visitor.visit_ty(ty)); } @@ -1257,8 +1257,8 @@ pub fn noop_flat_map_foreign_item<T: MutVisitor>( }) => { visit_defaultness(defaultness, visitor); visitor.visit_generics(generics); - visitor.visit_span(&mut where_clauses.0.1); - visitor.visit_span(&mut where_clauses.1.1); + visitor.visit_span(&mut where_clauses.before.span); + visitor.visit_span(&mut where_clauses.after.span); visit_bounds(bounds, visitor); visit_opt(ty, |ty| visitor.visit_ty(ty)); } diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 87ed47648c8..01b1e6fcaff 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -33,19 +33,20 @@ pub(super) struct ItemLowerer<'a, 'hir> { /// clause if it exists. fn add_ty_alias_where_clause( generics: &mut ast::Generics, - mut where_clauses: (TyAliasWhereClause, TyAliasWhereClause), + mut where_clauses: TyAliasWhereClauses, prefer_first: bool, ) { if !prefer_first { - where_clauses = (where_clauses.1, where_clauses.0); - } - if where_clauses.0.0 || !where_clauses.1.0 { - generics.where_clause.has_where_token = where_clauses.0.0; - generics.where_clause.span = where_clauses.0.1; - } else { - generics.where_clause.has_where_token = where_clauses.1.0; - generics.where_clause.span = where_clauses.1.1; + (where_clauses.before, where_clauses.after) = (where_clauses.after, where_clauses.before); } + let where_clause = + if where_clauses.before.has_where_token || !where_clauses.after.has_where_token { + where_clauses.before + } else { + where_clauses.after + }; + generics.where_clause.has_where_token = where_clause.has_where_token; + generics.where_clause.span = where_clause.span; } impl<'a, 'hir> ItemLowerer<'a, 'hir> { diff --git a/compiler/rustc_ast_passes/messages.ftl b/compiler/rustc_ast_passes/messages.ftl index 6586ca5d36f..28a13d275a5 100644 --- a/compiler/rustc_ast_passes/messages.ftl +++ b/compiler/rustc_ast_passes/messages.ftl @@ -280,4 +280,5 @@ ast_passes_where_clause_after_type_alias = where clauses are not allowed after t ast_passes_where_clause_before_type_alias = where clauses are not allowed before the type for type aliases .note = see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information - .suggestion = move it to the end of the type declaration + .remove_suggestion = remove this `where` + .move_suggestion = move it to the end of the type declaration diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 8c9ad836087..b56d695c671 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -138,38 +138,42 @@ impl<'a> AstValidator<'a> { &mut self, ty_alias: &TyAlias, ) -> Result<(), errors::WhereClauseBeforeTypeAlias> { - let before_predicates = - ty_alias.generics.where_clause.predicates.split_at(ty_alias.where_predicates_split).0; - - if ty_alias.ty.is_none() || before_predicates.is_empty() { + if ty_alias.ty.is_none() || !ty_alias.where_clauses.before.has_where_token { return Ok(()); } - let mut state = State::new(); - if !ty_alias.where_clauses.1.0 { - state.space(); - state.word_space("where"); - } else { - state.word_space(","); - } - let mut first = true; - for p in before_predicates { - if !first { - state.word_space(","); + let (before_predicates, after_predicates) = + ty_alias.generics.where_clause.predicates.split_at(ty_alias.where_clauses.split); + let span = ty_alias.where_clauses.before.span; + + let sugg = if !before_predicates.is_empty() || !ty_alias.where_clauses.after.has_where_token + { + let mut state = State::new(); + + if !ty_alias.where_clauses.after.has_where_token { + state.space(); + state.word_space("where"); } - first = false; - state.print_where_predicate(p); - } - let span = ty_alias.where_clauses.0.1; - Err(errors::WhereClauseBeforeTypeAlias { - span, - sugg: errors::WhereClauseBeforeTypeAliasSugg { + let mut first = after_predicates.is_empty(); + for p in before_predicates { + if !first { + state.word_space(","); + } + first = false; + state.print_where_predicate(p); + } + + errors::WhereClauseBeforeTypeAliasSugg::Move { left: span, snippet: state.s.eof(), - right: ty_alias.where_clauses.1.1.shrink_to_hi(), - }, - }) + right: ty_alias.where_clauses.after.span.shrink_to_hi(), + } + } else { + errors::WhereClauseBeforeTypeAliasSugg::Remove { span } + }; + + Err(errors::WhereClauseBeforeTypeAlias { span, sugg }) } fn with_impl_trait(&mut self, outer: Option<Span>, f: impl FnOnce(&mut Self)) { @@ -457,8 +461,7 @@ impl<'a> AstValidator<'a> { fn check_foreign_ty_genericless( &self, generics: &Generics, - before_where_clause: &TyAliasWhereClause, - after_where_clause: &TyAliasWhereClause, + where_clauses: &TyAliasWhereClauses, ) { let cannot_have = |span, descr, remove_descr| { self.dcx().emit_err(errors::ExternTypesCannotHave { @@ -473,14 +476,14 @@ impl<'a> AstValidator<'a> { cannot_have(generics.span, "generic parameters", "generic parameters"); } - let check_where_clause = |where_clause: &TyAliasWhereClause| { - if let TyAliasWhereClause(true, where_clause_span) = where_clause { - cannot_have(*where_clause_span, "`where` clauses", "`where` clause"); + let check_where_clause = |where_clause: TyAliasWhereClause| { + if where_clause.has_where_token { + cannot_have(where_clause.span, "`where` clauses", "`where` clause"); } }; - check_where_clause(before_where_clause); - check_where_clause(after_where_clause); + check_where_clause(where_clauses.before); + check_where_clause(where_clauses.after); } fn check_foreign_kind_bodyless(&self, ident: Ident, kind: &str, body: Option<Span>) { @@ -1122,9 +1125,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> { if let Err(err) = self.check_type_alias_where_clause_location(ty_alias) { self.dcx().emit_err(err); } - } else if where_clauses.1.0 { + } else if where_clauses.after.has_where_token { self.dcx().emit_err(errors::WhereClauseAfterTypeAlias { - span: where_clauses.1.1, + span: where_clauses.after.span, help: self.session.is_nightly_build().then_some(()), }); } @@ -1154,7 +1157,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.check_defaultness(fi.span, *defaultness); self.check_foreign_kind_bodyless(fi.ident, "type", ty.as_ref().map(|b| b.span)); self.check_type_no_bounds(bounds, "`extern` blocks"); - self.check_foreign_ty_genericless(generics, &where_clauses.0, &where_clauses.1); + self.check_foreign_ty_genericless(generics, where_clauses); self.check_foreign_item_ascii_only(fi.ident); } ForeignItemKind::Static(_, _, body) => { @@ -1477,15 +1480,18 @@ impl<'a> Visitor<'a> for AstValidator<'a> { if let AssocItemKind::Type(ty_alias) = &item.kind && let Err(err) = self.check_type_alias_where_clause_location(ty_alias) { + let sugg = match err.sugg { + errors::WhereClauseBeforeTypeAliasSugg::Remove { .. } => None, + errors::WhereClauseBeforeTypeAliasSugg::Move { snippet, right, .. } => { + Some((right, snippet)) + } + }; self.lint_buffer.buffer_lint_with_diagnostic( DEPRECATED_WHERE_CLAUSE_LOCATION, item.id, err.span, fluent::ast_passes_deprecated_where_clause_location, - BuiltinLintDiagnostics::DeprecatedWhereclauseLocation( - err.sugg.right, - err.sugg.snippet, - ), + BuiltinLintDiagnostics::DeprecatedWhereclauseLocation(sugg), ); } diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index 0f37093f057..19eed070911 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -515,17 +515,25 @@ pub struct WhereClauseBeforeTypeAlias { } #[derive(Subdiagnostic)] -#[multipart_suggestion( - ast_passes_suggestion, - applicability = "machine-applicable", - style = "verbose" -)] -pub struct WhereClauseBeforeTypeAliasSugg { - #[suggestion_part(code = "")] - pub left: Span, - pub snippet: String, - #[suggestion_part(code = "{snippet}")] - pub right: Span, + +pub enum WhereClauseBeforeTypeAliasSugg { + #[suggestion(ast_passes_remove_suggestion, applicability = "machine-applicable", code = "")] + Remove { + #[primary_span] + span: Span, + }, + #[multipart_suggestion( + ast_passes_move_suggestion, + applicability = "machine-applicable", + style = "verbose" + )] + Move { + #[suggestion_part(code = "")] + left: Span, + snippet: String, + #[suggestion_part(code = "{snippet}")] + right: Span, + }, } #[derive(Diagnostic)] diff --git a/compiler/rustc_ast_pretty/src/pprust/state/item.rs b/compiler/rustc_ast_pretty/src/pprust/state/item.rs index 584f01e16c2..13f27c1c95c 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/item.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/item.rs @@ -43,7 +43,6 @@ impl<'a> State<'a> { defaultness, generics, where_clauses, - where_predicates_split, bounds, ty, }) => { @@ -51,7 +50,6 @@ impl<'a> State<'a> { ident, generics, *where_clauses, - *where_predicates_split, bounds, ty.as_deref(), vis, @@ -108,15 +106,14 @@ impl<'a> State<'a> { &mut self, ident: Ident, generics: &ast::Generics, - where_clauses: (ast::TyAliasWhereClause, ast::TyAliasWhereClause), - where_predicates_split: usize, + where_clauses: ast::TyAliasWhereClauses, bounds: &ast::GenericBounds, ty: Option<&ast::Ty>, vis: &ast::Visibility, defaultness: ast::Defaultness, ) { let (before_predicates, after_predicates) = - generics.where_clause.predicates.split_at(where_predicates_split); + generics.where_clause.predicates.split_at(where_clauses.split); self.head(""); self.print_visibility(vis); self.print_defaultness(defaultness); @@ -127,13 +124,13 @@ impl<'a> State<'a> { self.word_nbsp(":"); self.print_type_bounds(bounds); } - self.print_where_clause_parts(where_clauses.0.0, before_predicates); + self.print_where_clause_parts(where_clauses.before.has_where_token, before_predicates); if let Some(ty) = ty { self.space(); self.word_space("="); self.print_type(ty); } - self.print_where_clause_parts(where_clauses.1.0, after_predicates); + 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 @@ -249,7 +246,6 @@ impl<'a> State<'a> { defaultness, generics, where_clauses, - where_predicates_split, bounds, ty, }) => { @@ -257,7 +253,6 @@ impl<'a> State<'a> { item.ident, generics, *where_clauses, - *where_predicates_split, bounds, ty.as_deref(), &item.vis, @@ -536,7 +531,6 @@ impl<'a> State<'a> { defaultness, generics, where_clauses, - where_predicates_split, bounds, ty, }) => { @@ -544,7 +538,6 @@ impl<'a> State<'a> { ident, generics, *where_clauses, - *where_predicates_split, bounds, ty.as_deref(), vis, diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index 3ee4fded749..eb664b571ba 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -601,11 +601,7 @@ impl<'a> TraitDef<'a> { kind: ast::AssocItemKind::Type(Box::new(ast::TyAlias { defaultness: ast::Defaultness::Final, generics: Generics::default(), - where_clauses: ( - ast::TyAliasWhereClause::default(), - ast::TyAliasWhereClause::default(), - ), - where_predicates_split: 0, + where_clauses: ast::TyAliasWhereClauses::default(), bounds: Vec::new(), ty: Some(type_def.to_ty(cx, self.span, type_ident, generics)), })), diff --git a/compiler/rustc_codegen_ssa/Cargo.toml b/compiler/rustc_codegen_ssa/Cargo.toml index 781c54bdef8..9359df5de6a 100644 --- a/compiler/rustc_codegen_ssa/Cargo.toml +++ b/compiler/rustc_codegen_ssa/Cargo.toml @@ -38,6 +38,7 @@ tempfile = "3.2" thin-vec = "0.2.12" thorin-dwp = "0.7" tracing = "0.1" +wasm-encoder = "0.200.0" # tidy-alphabetical-end [target.'cfg(unix)'.dependencies] diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index e4a050dcfc9..0c77f7c51bc 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -315,8 +315,11 @@ fn link_rlib<'a>( let trailing_metadata = match flavor { RlibFlavor::Normal => { - let (metadata, metadata_position) = - create_wrapper_file(sess, b".rmeta".to_vec(), codegen_results.metadata.raw_data()); + let (metadata, metadata_position) = create_wrapper_file( + sess, + ".rmeta".to_string(), + codegen_results.metadata.raw_data(), + ); let metadata = emit_wrapper_file(sess, &metadata, tmpdir, METADATA_FILENAME); match metadata_position { MetadataPosition::First => { @@ -384,7 +387,7 @@ fn link_rlib<'a>( let path = find_native_static_library(filename.as_str(), true, &lib_search_paths, sess); let src = read(path) .map_err(|e| sess.dcx().emit_fatal(errors::ReadFileError { message: e }))?; - let (data, _) = create_wrapper_file(sess, b".bundled_lib".to_vec(), &src); + let (data, _) = create_wrapper_file(sess, ".bundled_lib".to_string(), &src); let wrapper_file = emit_wrapper_file(sess, &data, tmpdir, filename.as_str()); packed_bundled_libs.push(wrapper_file); } else { diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs index c6b04431fab..158b8fb8727 100644 --- a/compiler/rustc_codegen_ssa/src/back/metadata.rs +++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs @@ -1,5 +1,6 @@ //! Reading of the rustc metadata for rlibs and dylibs +use std::borrow::Cow; use std::fs::File; use std::io::Write; use std::path::Path; @@ -15,7 +16,6 @@ use rustc_data_structures::owned_slice::{try_slice_owned, OwnedSlice}; use rustc_metadata::creader::MetadataLoader; use rustc_metadata::fs::METADATA_FILENAME; use rustc_metadata::EncodedMetadata; -use rustc_serialize::leb128; use rustc_session::Session; use rustc_span::sym; use rustc_target::abi::Endian; @@ -434,12 +434,15 @@ pub enum MetadataPosition { /// automatically removed from the final output. pub fn create_wrapper_file( sess: &Session, - section_name: Vec<u8>, + section_name: String, data: &[u8], ) -> (Vec<u8>, MetadataPosition) { let Some(mut file) = create_object_file(sess) else { if sess.target.is_like_wasm { - return (create_metadata_file_for_wasm(data, §ion_name), MetadataPosition::First); + return ( + create_metadata_file_for_wasm(sess, data, §ion_name), + MetadataPosition::First, + ); } // Targets using this branch don't have support implemented here yet or @@ -452,7 +455,7 @@ pub fn create_wrapper_file( } else { file.add_section( file.segment_name(StandardSegment::Debug).to_vec(), - section_name, + section_name.into_bytes(), SectionKind::Debug, ) }; @@ -524,7 +527,7 @@ pub fn create_compressed_metadata_file( let Some(mut file) = create_object_file(sess) else { if sess.target.is_like_wasm { - return create_metadata_file_for_wasm(&packed_metadata, b".rustc"); + return create_metadata_file_for_wasm(sess, &packed_metadata, ".rustc"); } return packed_metadata.to_vec(); }; @@ -624,51 +627,41 @@ pub fn create_compressed_metadata_file_for_xcoff( /// `data`. /// /// NB: the `object` crate does not yet have support for writing the wasm -/// object file format. The format is simple enough that for now an extra crate -/// from crates.io (such as `wasm-encoder`). The file format is: +/// object file format. In lieu of that the `wasm-encoder` crate is used to +/// build a wasm file by hand. /// -/// * 4-byte header "\0asm" -/// * 4-byte version number - 1u32 in little-endian format -/// * concatenated sections, which for this object is always "custom sections" -/// -/// Custom sections are then defined by: -/// * 1-byte section identifier - 0 for a custom section -/// * leb-encoded section length (size of the contents beneath this bullet) -/// * leb-encoded custom section name length -/// * custom section name -/// * section contents -/// -/// One custom section, `linking`, is added here in accordance with +/// The wasm object file format is defined at /// <https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md> -/// which is required to inform LLD that this is an object file but it should -/// otherwise basically ignore it if it otherwise looks at it. The linking -/// section currently is defined by a single version byte (2) and then further -/// sections, but we have no more sections, so it's just the byte "2". +/// and mainly consists of a `linking` custom section. In this case the custom +/// section there is empty except for a version marker indicating what format +/// it's in. /// -/// The next custom section is the one we're interested in. -pub fn create_metadata_file_for_wasm(data: &[u8], section_name: &[u8]) -> Vec<u8> { - let mut bytes = b"\0asm\x01\0\0\0".to_vec(); - - let mut append_custom_section = |section_name: &[u8], data: &[u8]| { - let mut section_name_len = [0; leb128::max_leb128_len::<usize>()]; - let off = leb128::write_usize_leb128(&mut section_name_len, section_name.len()); - let section_name_len = §ion_name_len[..off]; - - let mut section_len = [0; leb128::max_leb128_len::<usize>()]; - let off = leb128::write_usize_leb128( - &mut section_len, - data.len() + section_name_len.len() + section_name.len(), +/// The main purpose of this is to contain a custom section with `section_name`, +/// which is then appended after `linking`. +/// +/// As a further detail the object needs to have a 64-bit memory if `wasm64` is +/// the target or otherwise it's interpreted as a 32-bit object which is +/// incompatible with 64-bit ones. +pub fn create_metadata_file_for_wasm(sess: &Session, data: &[u8], section_name: &str) -> Vec<u8> { + assert!(sess.target.is_like_wasm); + let mut module = wasm_encoder::Module::new(); + let mut imports = wasm_encoder::ImportSection::new(); + + if sess.target.pointer_width == 64 { + imports.import( + "env", + "__linear_memory", + wasm_encoder::MemoryType { minimum: 0, maximum: None, memory64: true, shared: false }, ); - let section_len = §ion_len[..off]; - - bytes.push(0u8); - bytes.extend_from_slice(section_len); - bytes.extend_from_slice(section_name_len); - bytes.extend_from_slice(section_name); - bytes.extend_from_slice(data); - }; + } - append_custom_section(b"linking", &[2]); - append_custom_section(section_name, data); - bytes + if imports.len() > 0 { + module.section(&imports); + } + module.section(&wasm_encoder::CustomSection { + name: "linking".into(), + data: Cow::Borrowed(&[2]), + }); + module.section(&wasm_encoder::CustomSection { name: section_name.into(), data: data.into() }); + module.finish() } diff --git a/compiler/rustc_const_eval/messages.ftl b/compiler/rustc_const_eval/messages.ftl index c456e40d7c1..2805ca360ad 100644 --- a/compiler/rustc_const_eval/messages.ftl +++ b/compiler/rustc_const_eval/messages.ftl @@ -453,7 +453,6 @@ const_eval_validation_invalid_fn_ptr = {$front_matter}: encountered {$value}, bu const_eval_validation_invalid_ref_meta = {$front_matter}: encountered invalid reference metadata: total size is bigger than largest supported object const_eval_validation_invalid_ref_slice_meta = {$front_matter}: encountered invalid reference metadata: slice is bigger than largest supported object const_eval_validation_invalid_vtable_ptr = {$front_matter}: encountered {$value}, but expected a vtable pointer -const_eval_validation_mutable_ref_in_const_or_static = {$front_matter}: encountered mutable reference in a `const` or `static` const_eval_validation_mutable_ref_to_immutable = {$front_matter}: encountered mutable reference or box pointing to read-only memory const_eval_validation_never_val = {$front_matter}: encountered a value of the never type `!` const_eval_validation_null_box = {$front_matter}: encountered a null box diff --git a/compiler/rustc_const_eval/src/errors.rs b/compiler/rustc_const_eval/src/errors.rs index 62aab9d5635..c59e0a0df9f 100644 --- a/compiler/rustc_const_eval/src/errors.rs +++ b/compiler/rustc_const_eval/src/errors.rs @@ -613,7 +613,6 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> { PartialPointer => const_eval_validation_partial_pointer, ConstRefToMutable => const_eval_validation_const_ref_to_mutable, ConstRefToExtern => const_eval_validation_const_ref_to_extern, - MutableRefInConstOrStatic => const_eval_validation_mutable_ref_in_const_or_static, MutableRefToImmutable => const_eval_validation_mutable_ref_to_immutable, NullFnPtr => const_eval_validation_null_fn_ptr, NeverVal => const_eval_validation_never_val, @@ -767,7 +766,6 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> { } NullPtr { .. } | PtrToStatic { .. } - | MutableRefInConstOrStatic | ConstRefToMutable | ConstRefToExtern | MutableRefToImmutable diff --git a/compiler/rustc_const_eval/src/interpret/validity.rs b/compiler/rustc_const_eval/src/interpret/validity.rs index 792e1c9e736..ff1cb43db86 100644 --- a/compiler/rustc_const_eval/src/interpret/validity.rs +++ b/compiler/rustc_const_eval/src/interpret/validity.rs @@ -148,14 +148,6 @@ impl CtfeValidationMode { } } } - - fn may_contain_mutable_ref(self) -> bool { - match self { - CtfeValidationMode::Static { mutbl } => mutbl == Mutability::Mut, - CtfeValidationMode::Promoted { .. } => false, - CtfeValidationMode::Const { .. } => false, - } - } } /// State for tracking recursive validation of references @@ -511,20 +503,19 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' // If this allocation has size zero, there is no actual mutability here. let (size, _align, _alloc_kind) = self.ecx.get_alloc_info(alloc_id); if size != Size::ZERO { + // Mutable pointer to immutable memory is no good. if ptr_expected_mutbl == Mutability::Mut && alloc_actual_mutbl == Mutability::Not { throw_validation_failure!(self.path, MutableRefToImmutable); } - if ptr_expected_mutbl == Mutability::Mut - && self.ctfe_mode.is_some_and(|c| !c.may_contain_mutable_ref()) - { - throw_validation_failure!(self.path, MutableRefInConstOrStatic); - } - if alloc_actual_mutbl == Mutability::Mut - && matches!(self.ctfe_mode, Some(CtfeValidationMode::Const { .. })) - { - throw_validation_failure!(self.path, ConstRefToMutable); + // In a const, everything must be completely immutable. + if matches!(self.ctfe_mode, Some(CtfeValidationMode::Const { .. })) { + if ptr_expected_mutbl == Mutability::Mut + || alloc_actual_mutbl == Mutability::Mut + { + throw_validation_failure!(self.path, ConstRefToMutable); + } } } // Potentially skip recursive check. diff --git a/compiler/rustc_hir/src/lang_items.rs b/compiler/rustc_hir/src/lang_items.rs index 9c9e72d6e65..8a89a3b5faa 100644 --- a/compiler/rustc_hir/src/lang_items.rs +++ b/compiler/rustc_hir/src/lang_items.rs @@ -167,7 +167,7 @@ language_item_table! { // language items relating to transmutability TransmuteOpts, sym::transmute_opts, transmute_opts, Target::Struct, GenericRequirement::Exact(0); - TransmuteTrait, sym::transmute_trait, transmute_trait, Target::Trait, GenericRequirement::Exact(3); + TransmuteTrait, sym::transmute_trait, transmute_trait, Target::Trait, GenericRequirement::Exact(2); Add, sym::add, add_trait, Target::Trait, GenericRequirement::Exact(1); Sub, sym::sub, sub_trait, Target::Trait, GenericRequirement::Exact(1); diff --git a/compiler/rustc_lint/src/context/diagnostics.rs b/compiler/rustc_lint/src/context/diagnostics.rs index 71aef50391d..14e4c79563b 100644 --- a/compiler/rustc_lint/src/context/diagnostics.rs +++ b/compiler/rustc_lint/src/context/diagnostics.rs @@ -428,15 +428,22 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiagnostics, diag: diag.note("see <https://doc.rust-lang.org/nightly/unstable-book/compiler-flags/check-cfg.html> for more information about checking conditional configuration"); } } - BuiltinLintDiagnostics::DeprecatedWhereclauseLocation(new_span, suggestion) => { - diag.multipart_suggestion( - "move it to the end of the type declaration", - vec![(diag.span.primary_span().unwrap(), "".to_string()), (new_span, suggestion)], - Applicability::MachineApplicable, - ); - diag.note( - "see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information", - ); + BuiltinLintDiagnostics::DeprecatedWhereclauseLocation(sugg) => { + let left_sp = diag.span.primary_span().unwrap(); + match sugg { + Some((right_sp, sugg)) => diag.multipart_suggestion( + "move it to the end of the type declaration", + vec![(left_sp, String::new()), (right_sp, sugg)], + Applicability::MachineApplicable, + ), + None => diag.span_suggestion( + left_sp, + "remove this `where`", + "", + Applicability::MachineApplicable, + ), + }; + diag.note("see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information"); } BuiltinLintDiagnostics::SingleUseLifetime { param_span, diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index fc590001c0d..198008d4d0d 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -597,7 +597,7 @@ pub enum BuiltinLintDiagnostics { UnicodeTextFlow(Span, String), UnexpectedCfgName((Symbol, Span), Option<(Symbol, Span)>), UnexpectedCfgValue((Symbol, Span), Option<(Symbol, Span)>), - DeprecatedWhereclauseLocation(Span, String), + DeprecatedWhereclauseLocation(Option<(Span, String)>), SingleUseLifetime { /// Span of the parameter which declares this lifetime. param_span: Span, diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs index f344dd5a8c3..b85354e10f9 100644 --- a/compiler/rustc_middle/src/mir/interpret/error.rs +++ b/compiler/rustc_middle/src/mir/interpret/error.rs @@ -420,7 +420,6 @@ pub enum ValidationErrorKind<'tcx> { PartialPointer, PtrToUninhabited { ptr_kind: PointerKind, ty: Ty<'tcx> }, PtrToStatic { ptr_kind: PointerKind }, - MutableRefInConstOrStatic, ConstRefToMutable, ConstRefToExtern, MutableRefToImmutable, diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index a678194372e..d8587f1340a 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -971,11 +971,17 @@ impl<'a> Parser<'a> { let after_where_clause = self.parse_where_clause()?; - let where_clauses = ( - TyAliasWhereClause(before_where_clause.has_where_token, before_where_clause.span), - TyAliasWhereClause(after_where_clause.has_where_token, after_where_clause.span), - ); - let where_predicates_split = before_where_clause.predicates.len(); + let where_clauses = TyAliasWhereClauses { + before: TyAliasWhereClause { + has_where_token: before_where_clause.has_where_token, + span: before_where_clause.span, + }, + after: TyAliasWhereClause { + has_where_token: after_where_clause.has_where_token, + span: after_where_clause.span, + }, + split: before_where_clause.predicates.len(), + }; let mut predicates = before_where_clause.predicates; predicates.extend(after_where_clause.predicates); let where_clause = WhereClause { @@ -994,7 +1000,6 @@ impl<'a> Parser<'a> { defaultness, generics, where_clauses, - where_predicates_split, bounds, ty, })), diff --git a/compiler/rustc_trait_selection/src/solve/eval_ctxt/mod.rs b/compiler/rustc_trait_selection/src/solve/eval_ctxt/mod.rs index ed428bb8e66..df4dcaff1e7 100644 --- a/compiler/rustc_trait_selection/src/solve/eval_ctxt/mod.rs +++ b/compiler/rustc_trait_selection/src/solve/eval_ctxt/mod.rs @@ -874,7 +874,6 @@ impl<'tcx> EvalCtxt<'_, 'tcx> { pub(super) fn is_transmutable( &self, src_and_dst: rustc_transmute::Types<'tcx>, - scope: Ty<'tcx>, assume: rustc_transmute::Assume, ) -> Result<Certainty, NoSolution> { use rustc_transmute::Answer; @@ -882,7 +881,6 @@ impl<'tcx> EvalCtxt<'_, 'tcx> { match rustc_transmute::TransmuteTypeEnv::new(self.infcx).is_transmutable( ObligationCause::dummy(), src_and_dst, - scope, assume, ) { Answer::Yes => Ok(Certainty::Yes), diff --git a/compiler/rustc_trait_selection/src/solve/trait_goals.rs b/compiler/rustc_trait_selection/src/solve/trait_goals.rs index eba6ba3f7b0..80198ba39f9 100644 --- a/compiler/rustc_trait_selection/src/solve/trait_goals.rs +++ b/compiler/rustc_trait_selection/src/solve/trait_goals.rs @@ -549,14 +549,13 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> { let args = ecx.tcx().erase_regions(goal.predicate.trait_ref.args); let Some(assume) = - rustc_transmute::Assume::from_const(ecx.tcx(), goal.param_env, args.const_at(3)) + rustc_transmute::Assume::from_const(ecx.tcx(), goal.param_env, args.const_at(2)) else { return Err(NoSolution); }; let certainty = ecx.is_transmutable( rustc_transmute::Types { dst: args.type_at(0), src: args.type_at(1) }, - args.type_at(2), assume, )?; ecx.evaluate_added_goals_and_make_canonical_response(certainty) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs index d9e49f1eb0a..3275a4f3527 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs @@ -2960,11 +2960,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { dst: trait_ref.args.type_at(0), src: trait_ref.args.type_at(1), }; - let scope = trait_ref.args.type_at(2); let Some(assume) = rustc_transmute::Assume::from_const( self.infcx.tcx, obligation.param_env, - trait_ref.args.const_at(3), + trait_ref.args.const_at(2), ) else { self.dcx().span_delayed_bug( span, @@ -2976,15 +2975,12 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { match rustc_transmute::TransmuteTypeEnv::new(self.infcx).is_transmutable( obligation.cause, src_and_dst, - scope, assume, ) { Answer::No(reason) => { let dst = trait_ref.args.type_at(0); let src = trait_ref.args.type_at(1); - let err_msg = format!( - "`{src}` cannot be safely transmuted into `{dst}` in the defining scope of `{scope}`" - ); + let err_msg = format!("`{src}` cannot be safely transmuted into `{dst}`"); let safe_transmute_explanation = match reason { rustc_transmute::Reason::SrcIsUnspecified => { format!("`{src}` does not have a well-specified layout") @@ -2998,9 +2994,9 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { format!("At least one value of `{src}` isn't a bit-valid value of `{dst}`") } - rustc_transmute::Reason::DstIsPrivate => format!( - "`{dst}` is or contains a type or field that is not visible in that scope" - ), + rustc_transmute::Reason::DstMayHaveSafetyInvariants => { + format!("`{dst}` may carry safety invariants") + } rustc_transmute::Reason::DstIsTooBig => { format!("The size of `{src}` is smaller than the size of `{dst}`") } diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index b8733bab27b..70f6b240ab7 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -310,8 +310,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { .collect(), Condition::IfTransmutable { src, dst } => { let trait_def_id = obligation.predicate.def_id(); - let scope = predicate.trait_ref.args.type_at(2); - let assume_const = predicate.trait_ref.args.const_at(3); + let assume_const = predicate.trait_ref.args.const_at(2); let make_obl = |from_ty, to_ty| { let trait_ref1 = ty::TraitRef::new( tcx, @@ -319,7 +318,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { [ ty::GenericArg::from(to_ty), ty::GenericArg::from(from_ty), - ty::GenericArg::from(scope), ty::GenericArg::from(assume_const), ], ); @@ -355,7 +353,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let Some(assume) = rustc_transmute::Assume::from_const( self.infcx.tcx, obligation.param_env, - predicate.trait_ref.args.const_at(3), + predicate.trait_ref.args.const_at(2), ) else { return Err(Unimplemented); }; @@ -367,7 +365,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let maybe_transmutable = transmute_env.is_transmutable( obligation.cause.clone(), rustc_transmute::Types { dst, src }, - predicate.trait_ref.args.type_at(2), assume, ); diff --git a/compiler/rustc_transmute/src/layout/mod.rs b/compiler/rustc_transmute/src/layout/mod.rs index 76d97e0e6e7..0441b49cb14 100644 --- a/compiler/rustc_transmute/src/layout/mod.rs +++ b/compiler/rustc_transmute/src/layout/mod.rs @@ -29,14 +29,21 @@ impl fmt::Debug for Byte { } } -pub(crate) trait Def: Debug + Hash + Eq + PartialEq + Copy + Clone {} +pub(crate) trait Def: Debug + Hash + Eq + PartialEq + Copy + Clone { + fn has_safety_invariants(&self) -> bool; +} pub trait Ref: Debug + Hash + Eq + PartialEq + Copy + Clone { fn min_align(&self) -> usize; fn is_mutable(&self) -> bool; } -impl Def for ! {} +impl Def for ! { + fn has_safety_invariants(&self) -> bool { + unreachable!() + } +} + impl Ref for ! { fn min_align(&self) -> usize { unreachable!() @@ -83,5 +90,12 @@ pub mod rustc { Primitive, } - impl<'tcx> super::Def for Def<'tcx> {} + impl<'tcx> super::Def for Def<'tcx> { + fn has_safety_invariants(&self) -> bool { + // Rust presently has no notion of 'unsafe fields', so for now we + // make the conservative assumption that everything besides + // primitive types carry safety invariants. + self != &Self::Primitive + } + } } diff --git a/compiler/rustc_transmute/src/layout/tree.rs b/compiler/rustc_transmute/src/layout/tree.rs index 86a077ee808..71b72828e4c 100644 --- a/compiler/rustc_transmute/src/layout/tree.rs +++ b/compiler/rustc_transmute/src/layout/tree.rs @@ -81,7 +81,8 @@ where Self::Seq(vec![Self::uninit(); width_in_bytes]) } - /// Remove all `Def` nodes, and all branches of the layout for which `f` produces false. + /// Remove all `Def` nodes, and all branches of the layout for which `f` + /// produces `true`. pub(crate) fn prune<F>(self, f: &F) -> Tree<!, R> where F: Fn(D) -> bool, @@ -106,7 +107,7 @@ where Self::Byte(b) => Tree::Byte(b), Self::Ref(r) => Tree::Ref(r), Self::Def(d) => { - if !f(d) { + if f(d) { Tree::uninhabited() } else { Tree::unit() diff --git a/compiler/rustc_transmute/src/layout/tree/tests.rs b/compiler/rustc_transmute/src/layout/tree/tests.rs index 90515e92f7a..3cb47517c21 100644 --- a/compiler/rustc_transmute/src/layout/tree/tests.rs +++ b/compiler/rustc_transmute/src/layout/tree/tests.rs @@ -2,11 +2,15 @@ use super::Tree; #[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)] pub enum Def { - Visible, - Invisible, + NoSafetyInvariants, + HasSafetyInvariants, } -impl super::Def for Def {} +impl super::Def for Def { + fn has_safety_invariants(&self) -> bool { + self == &Self::HasSafetyInvariants + } +} mod prune { use super::*; @@ -16,17 +20,22 @@ mod prune { #[test] fn seq_1() { - let layout: Tree<Def, !> = Tree::def(Def::Visible).then(Tree::from_bits(0x00)); - assert_eq!(layout.prune(&|d| matches!(d, Def::Visible)), Tree::from_bits(0x00)); + let layout: Tree<Def, !> = + Tree::def(Def::NoSafetyInvariants).then(Tree::from_bits(0x00)); + assert_eq!( + layout.prune(&|d| matches!(d, Def::HasSafetyInvariants)), + Tree::from_bits(0x00) + ); } #[test] fn seq_2() { - let layout: Tree<Def, !> = - Tree::from_bits(0x00).then(Tree::def(Def::Visible)).then(Tree::from_bits(0x01)); + let layout: Tree<Def, !> = Tree::from_bits(0x00) + .then(Tree::def(Def::NoSafetyInvariants)) + .then(Tree::from_bits(0x01)); assert_eq!( - layout.prune(&|d| matches!(d, Def::Visible)), + layout.prune(&|d| matches!(d, Def::HasSafetyInvariants)), Tree::from_bits(0x00).then(Tree::from_bits(0x01)) ); } @@ -37,21 +46,32 @@ mod prune { #[test] fn invisible_def() { - let layout: Tree<Def, !> = Tree::def(Def::Invisible); - assert_eq!(layout.prune(&|d| matches!(d, Def::Visible)), Tree::uninhabited()); + let layout: Tree<Def, !> = Tree::def(Def::HasSafetyInvariants); + assert_eq!( + layout.prune(&|d| matches!(d, Def::HasSafetyInvariants)), + Tree::uninhabited() + ); } #[test] fn invisible_def_in_seq_len_2() { - let layout: Tree<Def, !> = Tree::def(Def::Visible).then(Tree::def(Def::Invisible)); - assert_eq!(layout.prune(&|d| matches!(d, Def::Visible)), Tree::uninhabited()); + let layout: Tree<Def, !> = + Tree::def(Def::NoSafetyInvariants).then(Tree::def(Def::HasSafetyInvariants)); + assert_eq!( + layout.prune(&|d| matches!(d, Def::HasSafetyInvariants)), + Tree::uninhabited() + ); } #[test] fn invisible_def_in_seq_len_3() { - let layout: Tree<Def, !> = - Tree::def(Def::Visible).then(Tree::from_bits(0x00)).then(Tree::def(Def::Invisible)); - assert_eq!(layout.prune(&|d| matches!(d, Def::Visible)), Tree::uninhabited()); + let layout: Tree<Def, !> = Tree::def(Def::NoSafetyInvariants) + .then(Tree::from_bits(0x00)) + .then(Tree::def(Def::HasSafetyInvariants)); + assert_eq!( + layout.prune(&|d| matches!(d, Def::HasSafetyInvariants)), + Tree::uninhabited() + ); } } @@ -60,21 +80,26 @@ mod prune { #[test] fn visible_def() { - let layout: Tree<Def, !> = Tree::def(Def::Visible); - assert_eq!(layout.prune(&|d| matches!(d, Def::Visible)), Tree::unit()); + let layout: Tree<Def, !> = Tree::def(Def::NoSafetyInvariants); + assert_eq!(layout.prune(&|d| matches!(d, Def::HasSafetyInvariants)), Tree::unit()); } #[test] fn visible_def_in_seq_len_2() { - let layout: Tree<Def, !> = Tree::def(Def::Visible).then(Tree::def(Def::Visible)); - assert_eq!(layout.prune(&|d| matches!(d, Def::Visible)), Tree::unit()); + let layout: Tree<Def, !> = + Tree::def(Def::NoSafetyInvariants).then(Tree::def(Def::NoSafetyInvariants)); + assert_eq!(layout.prune(&|d| matches!(d, Def::HasSafetyInvariants)), Tree::unit()); } #[test] fn visible_def_in_seq_len_3() { - let layout: Tree<Def, !> = - Tree::def(Def::Visible).then(Tree::from_bits(0x00)).then(Tree::def(Def::Visible)); - assert_eq!(layout.prune(&|d| matches!(d, Def::Visible)), Tree::from_bits(0x00)); + let layout: Tree<Def, !> = Tree::def(Def::NoSafetyInvariants) + .then(Tree::from_bits(0x00)) + .then(Tree::def(Def::NoSafetyInvariants)); + assert_eq!( + layout.prune(&|d| matches!(d, Def::HasSafetyInvariants)), + Tree::from_bits(0x00) + ); } } } diff --git a/compiler/rustc_transmute/src/lib.rs b/compiler/rustc_transmute/src/lib.rs index ac4f67d1b55..fefce2640eb 100644 --- a/compiler/rustc_transmute/src/lib.rs +++ b/compiler/rustc_transmute/src/lib.rs @@ -49,8 +49,8 @@ pub enum Reason { DstIsUnspecified, /// The layout of the destination type is bit-incompatible with the source type. DstIsBitIncompatible, - /// There aren't any public constructors for `Dst`. - DstIsPrivate, + /// The destination type may carry safety invariants. + DstMayHaveSafetyInvariants, /// `Dst` is larger than `Src`, and the excess bytes were not exclusively uninitialized. DstIsTooBig, /// Src should have a stricter alignment than Dst, but it does not. @@ -106,13 +106,11 @@ mod rustc { &mut self, cause: ObligationCause<'tcx>, types: Types<'tcx>, - scope: Ty<'tcx>, assume: crate::Assume, ) -> crate::Answer<crate::layout::rustc::Ref<'tcx>> { crate::maybe_transmutable::MaybeTransmutableQuery::new( types.src, types.dst, - scope, assume, self.infcx.tcx, ) diff --git a/compiler/rustc_transmute/src/maybe_transmutable/mod.rs b/compiler/rustc_transmute/src/maybe_transmutable/mod.rs index bf3c390c800..0e05aa4d3b2 100644 --- a/compiler/rustc_transmute/src/maybe_transmutable/mod.rs +++ b/compiler/rustc_transmute/src/maybe_transmutable/mod.rs @@ -3,7 +3,7 @@ pub(crate) mod query_context; mod tests; use crate::{ - layout::{self, dfa, Byte, Dfa, Nfa, Ref, Tree, Uninhabited}, + layout::{self, dfa, Byte, Def, Dfa, Nfa, Ref, Tree, Uninhabited}, maybe_transmutable::query_context::QueryContext, Answer, Condition, Map, Reason, }; @@ -14,7 +14,6 @@ where { src: L, dst: L, - scope: <C as QueryContext>::Scope, assume: crate::Assume, context: C, } @@ -23,14 +22,8 @@ impl<L, C> MaybeTransmutableQuery<L, C> where C: QueryContext, { - pub(crate) fn new( - src: L, - dst: L, - scope: <C as QueryContext>::Scope, - assume: crate::Assume, - context: C, - ) -> Self { - Self { src, dst, scope, assume, context } + pub(crate) fn new(src: L, dst: L, assume: crate::Assume, context: C) -> Self { + Self { src, dst, assume, context } } } @@ -48,7 +41,7 @@ mod rustc { /// then computes an answer using those trees. #[instrument(level = "debug", skip(self), fields(src = ?self.src, dst = ?self.dst))] pub fn answer(self) -> Answer<<TyCtxt<'tcx> as QueryContext>::Ref> { - let Self { src, dst, scope, assume, context } = self; + let Self { src, dst, assume, context } = self; // Convert `src` and `dst` from their rustc representations, to `Tree`-based // representations. If these conversions fail, conclude that the transmutation is @@ -67,9 +60,7 @@ mod rustc { (_, Err(Err::Unspecified)) => Answer::No(Reason::DstIsUnspecified), (Err(Err::SizeOverflow), _) => Answer::No(Reason::SrcSizeOverflow), (_, Err(Err::SizeOverflow)) => Answer::No(Reason::DstSizeOverflow), - (Ok(src), Ok(dst)) => { - MaybeTransmutableQuery { src, dst, scope, assume, context }.answer() - } + (Ok(src), Ok(dst)) => MaybeTransmutableQuery { src, dst, assume, context }.answer(), } } } @@ -86,43 +77,51 @@ where #[inline(always)] #[instrument(level = "debug", skip(self), fields(src = ?self.src, dst = ?self.dst))] pub(crate) fn answer(self) -> Answer<<C as QueryContext>::Ref> { - let assume_visibility = self.assume.safety; - - let Self { src, dst, scope, assume, context } = self; + let Self { src, dst, assume, context } = self; - // Remove all `Def` nodes from `src`, without checking their visibility. - let src = src.prune(&|def| true); + // Unconditionally all `Def` nodes from `src`, without pruning away the + // branches they appear in. This is valid to do for value-to-value + // transmutations, but not for `&mut T` to `&mut U`; we will need to be + // more sophisticated to handle transmutations between mutable + // references. + let src = src.prune(&|def| false); trace!(?src, "pruned src"); // Remove all `Def` nodes from `dst`, additionally... - let dst = if assume_visibility { - // ...if visibility is assumed, don't check their visibility. - dst.prune(&|def| true) + let dst = if assume.safety { + // ...if safety is assumed, don't check if they carry safety + // invariants; retain all paths. + dst.prune(&|def| false) } else { - // ...otherwise, prune away all unreachable paths through the `Dst` layout. - dst.prune(&|def| context.is_accessible_from(def, scope)) + // ...otherwise, prune away all paths with safety invariants from + // the `Dst` layout. + dst.prune(&|def| def.has_safety_invariants()) }; trace!(?dst, "pruned dst"); - // Convert `src` from a tree-based representation to an NFA-based representation. - // If the conversion fails because `src` is uninhabited, conclude that the transmutation - // is acceptable, because instances of the `src` type do not exist. + // Convert `src` from a tree-based representation to an NFA-based + // representation. If the conversion fails because `src` is uninhabited, + // conclude that the transmutation is acceptable, because instances of + // the `src` type do not exist. let src = match Nfa::from_tree(src) { Ok(src) => src, Err(Uninhabited) => return Answer::Yes, }; - // Convert `dst` from a tree-based representation to an NFA-based representation. - // If the conversion fails because `src` is uninhabited, conclude that the transmutation - // is unacceptable, because instances of the `dst` type do not exist. + // Convert `dst` from a tree-based representation to an NFA-based + // representation. If the conversion fails because `src` is uninhabited, + // conclude that the transmutation is unacceptable. Valid instances of + // the `dst` type do not exist, either because it's genuinely + // uninhabited, or because there are no branches of the tree that are + // free of safety invariants. let dst = match Nfa::from_tree(dst) { Ok(dst) => dst, - Err(Uninhabited) => return Answer::No(Reason::DstIsPrivate), + Err(Uninhabited) => return Answer::No(Reason::DstMayHaveSafetyInvariants), }; - MaybeTransmutableQuery { src, dst, scope, assume, context }.answer() + MaybeTransmutableQuery { src, dst, assume, context }.answer() } } @@ -136,10 +135,10 @@ where #[inline(always)] #[instrument(level = "debug", skip(self), fields(src = ?self.src, dst = ?self.dst))] pub(crate) fn answer(self) -> Answer<<C as QueryContext>::Ref> { - let Self { src, dst, scope, assume, context } = self; + let Self { src, dst, assume, context } = self; let src = Dfa::from_nfa(src); let dst = Dfa::from_nfa(dst); - MaybeTransmutableQuery { src, dst, scope, assume, context }.answer() + MaybeTransmutableQuery { src, dst, assume, context }.answer() } } diff --git a/compiler/rustc_transmute/src/maybe_transmutable/query_context.rs b/compiler/rustc_transmute/src/maybe_transmutable/query_context.rs index 0cae0377ee8..54ed03d44e6 100644 --- a/compiler/rustc_transmute/src/maybe_transmutable/query_context.rs +++ b/compiler/rustc_transmute/src/maybe_transmutable/query_context.rs @@ -6,9 +6,6 @@ pub(crate) trait QueryContext { type Ref: layout::Ref; type Scope: Copy; - /// Is `def` accessible from the defining module of `scope`? - fn is_accessible_from(&self, def: Self::Def, scope: Self::Scope) -> bool; - fn min_align(&self, reference: Self::Ref) -> usize; } @@ -20,21 +17,21 @@ pub(crate) mod test { #[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)] pub(crate) enum Def { - Visible, - Invisible, + HasSafetyInvariants, + NoSafetyInvariants, } - impl crate::layout::Def for Def {} + impl crate::layout::Def for Def { + fn has_safety_invariants(&self) -> bool { + self == &Self::HasSafetyInvariants + } + } impl QueryContext for UltraMinimal { type Def = Def; type Ref = !; type Scope = (); - fn is_accessible_from(&self, def: Def, scope: ()) -> bool { - matches!(Def::Visible, def) - } - fn min_align(&self, reference: !) -> usize { unimplemented!() } @@ -52,34 +49,6 @@ mod rustc { type Scope = Ty<'tcx>; - #[instrument(level = "debug", skip(self))] - fn is_accessible_from(&self, def: Self::Def, scope: Self::Scope) -> bool { - use layout::rustc::Def; - use rustc_middle::ty; - - let parent = if let ty::Adt(adt_def, ..) = scope.kind() { - self.parent(adt_def.did()) - } else { - // Is this always how we want to handle a non-ADT scope? - return false; - }; - - let def_id = match def { - Def::Adt(adt_def) => adt_def.did(), - Def::Variant(variant_def) => variant_def.def_id, - Def::Field(field_def) => field_def.did, - Def::Primitive => { - // primitives do not have a def_id, but they're always accessible - return true; - } - }; - - let ret: bool = self.visibility(def_id).is_accessible_from(parent, *self); - - trace!(?ret, "ret"); - ret - } - fn min_align(&self, reference: Self::Ref) -> usize { unimplemented!() } diff --git a/compiler/rustc_transmute/src/maybe_transmutable/tests.rs b/compiler/rustc_transmute/src/maybe_transmutable/tests.rs index e49bebf571d..9c7abf1cbd6 100644 --- a/compiler/rustc_transmute/src/maybe_transmutable/tests.rs +++ b/compiler/rustc_transmute/src/maybe_transmutable/tests.rs @@ -3,6 +3,65 @@ use crate::maybe_transmutable::MaybeTransmutableQuery; use crate::{layout, Reason}; use itertools::Itertools; +mod safety { + use crate::Answer; + + use super::*; + + type Tree = layout::Tree<Def, !>; + + const DST_HAS_SAFETY_INVARIANTS: Answer<!> = + Answer::No(crate::Reason::DstMayHaveSafetyInvariants); + + fn is_transmutable(src: &Tree, dst: &Tree, assume_safety: bool) -> crate::Answer<!> { + let src = src.clone(); + let dst = dst.clone(); + // The only dimension of the transmutability analysis we want to test + // here is the safety analysis. To ensure this, we disable all other + // toggleable aspects of the transmutability analysis. + let assume = crate::Assume { + alignment: true, + lifetimes: true, + validity: true, + safety: assume_safety, + }; + crate::maybe_transmutable::MaybeTransmutableQuery::new(src, dst, assume, UltraMinimal) + .answer() + } + + #[test] + fn src_safe_dst_safe() { + let src = Tree::Def(Def::NoSafetyInvariants).then(Tree::u8()); + let dst = Tree::Def(Def::NoSafetyInvariants).then(Tree::u8()); + assert_eq!(is_transmutable(&src, &dst, false), Answer::Yes); + assert_eq!(is_transmutable(&src, &dst, true), Answer::Yes); + } + + #[test] + fn src_safe_dst_unsafe() { + let src = Tree::Def(Def::NoSafetyInvariants).then(Tree::u8()); + let dst = Tree::Def(Def::HasSafetyInvariants).then(Tree::u8()); + assert_eq!(is_transmutable(&src, &dst, false), DST_HAS_SAFETY_INVARIANTS); + assert_eq!(is_transmutable(&src, &dst, true), Answer::Yes); + } + + #[test] + fn src_unsafe_dst_safe() { + let src = Tree::Def(Def::HasSafetyInvariants).then(Tree::u8()); + let dst = Tree::Def(Def::NoSafetyInvariants).then(Tree::u8()); + assert_eq!(is_transmutable(&src, &dst, false), Answer::Yes); + assert_eq!(is_transmutable(&src, &dst, true), Answer::Yes); + } + + #[test] + fn src_unsafe_dst_unsafe() { + let src = Tree::Def(Def::HasSafetyInvariants).then(Tree::u8()); + let dst = Tree::Def(Def::HasSafetyInvariants).then(Tree::u8()); + assert_eq!(is_transmutable(&src, &dst, false), DST_HAS_SAFETY_INVARIANTS); + assert_eq!(is_transmutable(&src, &dst, true), Answer::Yes); + } +} + mod bool { use crate::Answer; @@ -10,11 +69,9 @@ mod bool { #[test] fn should_permit_identity_transmutation_tree() { - println!("{:?}", layout::Tree::<!, !>::bool()); let answer = crate::maybe_transmutable::MaybeTransmutableQuery::new( layout::Tree::<Def, !>::bool(), layout::Tree::<Def, !>::bool(), - (), crate::Assume { alignment: false, lifetimes: false, validity: true, safety: false }, UltraMinimal, ) @@ -27,7 +84,6 @@ mod bool { let answer = crate::maybe_transmutable::MaybeTransmutableQuery::new( layout::Dfa::<!>::bool(), layout::Dfa::<!>::bool(), - (), crate::Assume { alignment: false, lifetimes: false, validity: true, safety: false }, UltraMinimal, ) @@ -71,7 +127,6 @@ mod bool { MaybeTransmutableQuery::new( src_layout.clone(), dst_layout.clone(), - (), crate::Assume { validity: false, ..crate::Assume::default() }, UltraMinimal, ) @@ -86,7 +141,6 @@ mod bool { MaybeTransmutableQuery::new( src_layout.clone(), dst_layout.clone(), - (), crate::Assume { validity: true, ..crate::Assume::default() }, UltraMinimal, ) @@ -101,7 +155,6 @@ mod bool { MaybeTransmutableQuery::new( src_layout.clone(), dst_layout.clone(), - (), crate::Assume { validity: false, ..crate::Assume::default() }, UltraMinimal, ) |
