Skip to content

Commit 9ab78c2

Browse files
author
Carolyn Zech
authored
KaniAttributes Path Resolution Refactor (#4249)
Standardize our resolution handling for contracts and stubs: - Make sure that the function paths for stubs and contract proofs resolve ASAP (during `check_attributes`, pre-codegen), rather than waiting until we're trying to actually do the stubbing or contract instrumentation logic. The current implementation only does this for stubs. Users get errors faster this way. - Use the updated path resolution logic (c.f. #4057) for both contracts and stubs. - Deduplicate a lot of the logic and make error messages/spans more precise This is useful to ensure that our error messages are consistent across stubs and contracts for the same kinds of problems. I opened #4251 with some thoughts about further improvements. Resolves #4057 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
1 parent 39d85fc commit 9ab78c2

File tree

15 files changed

+260
-280
lines changed

15 files changed

+260
-280
lines changed

kani-compiler/src/codegen_aeneas_llbc/compiler_interface.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use rustc_middle::ty::TyCtxt;
3535
use rustc_middle::util::Providers;
3636
use rustc_public::mir::mono::{Instance, MonoItem};
3737
use rustc_public::rustc_internal;
38+
use rustc_public::ty::FnDef;
3839
use rustc_public::{CrateDef, DefId};
3940
use rustc_session::Session;
4041
use rustc_session::config::{CrateType, OutputFilenames, OutputType};
@@ -233,7 +234,8 @@ impl CodegenBackend for LlbcCodegenBackend {
233234
tcx,
234235
&[MonoItem::Fn(*harness)],
235236
model_path,
236-
contract_metadata,
237+
contract_metadata
238+
.map(|def| rustc_internal::internal(tcx, def.def_id())),
237239
transformer,
238240
);
239241
transformer = BodyTransformation::new(&queries, tcx, &unit);
@@ -351,9 +353,9 @@ impl ArchiveBuilderBuilder for ArArchiveBuilderBuilder {
351353
fn contract_metadata_for_harness(
352354
tcx: TyCtxt,
353355
def_id: DefId,
354-
) -> Result<Option<InternalDefId>, ErrorGuaranteed> {
356+
) -> Result<Option<FnDef>, ErrorGuaranteed> {
355357
let attrs = KaniAttributes::for_def_id(tcx, def_id);
356-
Ok(attrs.interpret_for_contract_attribute().map(|(_, id, _)| id))
358+
Ok(attrs.interpret_for_contract_attribute())
357359
}
358360

359361
/// Return a struct that contains information about the codegen results as expected by `rustc`.

kani-compiler/src/codegen_cprover_gotoc/compiler_interface.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use rustc_middle::util::Providers;
3737
use rustc_public::CrateDef;
3838
use rustc_public::mir::mono::{Instance, MonoItem};
3939
use rustc_public::rustc_internal;
40+
use rustc_public::ty::FnDef;
4041
use rustc_session::Session;
4142
use rustc_session::config::{CrateType, OutputFilenames, OutputType};
4243
use rustc_session::output::out_filename;
@@ -215,24 +216,23 @@ impl GotocCodegenBackend {
215216
(gcx, items, contract_info)
216217
}
217218

218-
/// Given a contract harness, get the DefId of its target.
219+
/// Given a harness, return the DefId of its target if it's a contract harness.
219220
/// For manual harnesses, extract it from the #[proof_for_contract] attribute.
220221
/// For automatic harnesses, extract the target from the harness's GenericArgs.
221-
fn target_def_id_for_harness(
222+
fn target_if_contract_harness(
222223
&self,
223224
tcx: TyCtxt,
224225
harness: &Instance,
225226
is_automatic_harness: bool,
226-
) -> Option<InternalDefId> {
227+
) -> Option<FnDef> {
227228
if is_automatic_harness {
228229
let kind = harness.args().0[0].expect_ty().kind();
229230
let (fn_to_verify_def, _) = kind.fn_def().unwrap();
230-
let def_id = fn_to_verify_def.def_id();
231-
let attrs = KaniAttributes::for_def_id(tcx, def_id);
232-
if attrs.has_contract() { Some(rustc_internal::internal(tcx, def_id)) } else { None }
231+
let attrs = KaniAttributes::for_def_id(tcx, fn_to_verify_def.def_id());
232+
if attrs.has_contract() { Some(fn_to_verify_def) } else { None }
233233
} else {
234234
let harness_attrs = KaniAttributes::for_def_id(tcx, harness.def.def_id());
235-
harness_attrs.interpret_for_contract_attribute().map(|(_, id, _)| id)
235+
harness_attrs.interpret_for_contract_attribute()
236236
}
237237
}
238238
}
@@ -343,13 +343,14 @@ impl CodegenBackend for GotocCodegenBackend {
343343
let model_path = units.harness_model_path(*harness).unwrap();
344344
let is_automatic_harness = units.is_automatic_harness(harness);
345345
let contract_metadata =
346-
self.target_def_id_for_harness(tcx, harness, is_automatic_harness);
346+
self.target_if_contract_harness(tcx, harness, is_automatic_harness);
347347
let (gcx, items, contract_info) = self.codegen_items(
348348
tcx,
349349
&[MonoItem::Fn(*harness)],
350350
model_path,
351351
&results.machine_model,
352-
contract_metadata,
352+
contract_metadata
353+
.map(|def| rustc_internal::internal(tcx, def.def_id())),
353354
transformer,
354355
);
355356
if gcx.has_loop_contracts {

0 commit comments

Comments
 (0)