Representing an $L^2$-functional by a non-$L^2$-function on a dense subspace Let $(X, \mu)$ be your favourite measure space (finite or $\sigma$-finite if you like), let $g \in L^2$ (say, the scalar field of $L^2$ is $\mathbb{R}$, though this probably doesn't matter). Let $\tilde g: X \to \mathbb{R}$ be a measurable function and assume that there exists a norm dense vector subspace $D$ of $L^2$ with the following property:
For every $f \in D$ the function $\tilde g f$ is integrable, and we have $\int \tilde g f \, d\mu = \int g f \, d\mu$.
Question. Does it follow that $\tilde g = g$ almost everywhere?
Remarks.

*

*Under the given assumptions, the propertiy $\tilde g = g$ almost everyhwere is equivalent to $\tilde g \in L^2$. For if $\tilde g \in L^2$, then the integral equality in the assumption extends to all $f \in L^2$ by density, and for $f := \tilde g - g$ we thus obtain that $\lVert \tilde g - g\rVert^2 = \int (\tilde g - g) f \, d\mu = 0$.
In other words, the question asks whether it is impossible to represent, on any dense subspace of $L^2$, a continuous linear functional on $L^2$ by a non-$L^2$-function.


*If $D$ is a lattice ideal in $L^2$, meaning that $f_1 \in D$ whenever $\lvert f_1 \rvert \le \lvert f_2 \rvert$ for some $f_2 \in D$, then the answer to the question is yes.
Proof of the claim in the second bullet point.
Assume that $D$ is a lattice ideal, and let $f \in D$. There exists a measurable function $s: X \to \mathbb{R}$ of modulus $\lvert s \rvert = 1$ such that $\tilde g f s \ge 0$. Note that this implies $\lvert \tilde g f \rvert = \tilde g f s$. As $\lvert s f \rvert = \lvert f \rvert$ we have $sf \in D$, so it follows that
$$
  \label{1}\tag{$*$}
  \int \lvert \tilde g f \rvert \, d\mu 
  = 
  \int \tilde g f s \, d\mu 
  = 
  \int g f s \, d\mu 
  \le 
  \lVert f \rVert \lVert g \rVert.
$$
Now, consider a function $0 \le h \in L^2$. By density, there exists a sequence $(f_n)$ in $D$ which converges to $h$. By replacing each $f_n$ with $(h \land f_n) \lor 0$ (where $\land$ denotes the pointwise minimum and $\lor$ denotes the pointwise maximum of functions) we may assume that $0 \le f_n \le h$ for each $n$. Moreover, by then replacing each $f_n$ with the pointwise maximum of the functions $f_1, \dots, f_n$, we may also assume that the sequence $(f_n)$ is increasing. Hence, it follows from the monotone convergence theorem and from \eqref{1} that
$$
  \int \lvert \tilde g h \rvert \, d\mu 
  = 
  \lim_{n \to \infty} \int \lvert \tilde g \rvert f_n \, d \mu
  \le 
  \lim_{n \to \infty} \lVert f_n \rVert \lVert g \rVert
  = 
  \lVert h \rVert \lVert g \rVert.
$$
For a general (i.e., not necessarily positive) function $h \in L^2$ we can apply the estimate that we just proved to $\lvert h \rvert$ and thus obtain the same estimate
$$
  \int \lvert \tilde g h \rvert \, d\mu 
  \le 
  \lVert h \rVert \lVert g \rVert
$$
for even all $h \in L^2$. But this shows that $\tilde g\in L^2$, so $\tilde g = g$ almost everyhwere due to the remark in the first bullet point. $\square$
 A: The answer is no and the following result provides a quite interesting counterexample. This is a known result, but I am not sure where to find it in the literature.

Theorem. If $f\in L^1_{\rm loc}(\mathbb{R}^n)\setminus L^2(\mathbb{R}^n)$, then there is an orthonormal basis
$\{\varphi_k\}_{k=1}^\infty$ of $L^2(\mathbb{R}^n)$ consisting of compactly supported smooth functions, such that
$$
\int_{\mathbb{R}^n}f(x)\varphi_n(x)\, dx=0
\quad
\text{for all $n=1,2,3\ldots$}
\tag1
$$

Indeed, if $D=\operatorname{span}\{\varphi_k\}$, then
$$
\int_{\mathbb{R}^n} f\varphi = \int_{\mathbb{R}^n} 0\cdot\varphi,
\quad
\forall \varphi\in D.
$$
Proof. Let $\mathbb{K}=\mathbb{R}$ or $\mathbb{C}$ be the given field.
In the proof we will need the following result.

Lemma.
Let $L:X\to\mathbb{K}$ be a discontinuous linear functional defined on a normed space $X$. Then $\ker L$ is a dense linear subspace of $X$.

Proof.
Since $L$ is discontinuous, it is unbounded, so there is a sequence $y_n\in X$ such that $|Ly_n|>n\Vert y_n\Vert$. Thus $x_n=y_n/\Vert y_n\Vert$ satisfies
$$
\Vert x_n\Vert =1
\quad
\text{and}
\quad
|Lx_n|>n. \tag2
$$
Let $y\in X$ be arbitrary. Then (2) yields
$$
\ker L\ni y-\Big(\frac{Ly}{Lx_n}\Big)x_n\to y
$$
so $\ker L$ is dense in $X$.
$\Box$
Proof of the theorem.
$X:=C_c^\infty(\mathbb{R}^n)\subset L^2(\mathbb{R}^n)$ is a dense linear subspace. Note that
$$
L:X\to\mathbb{K},
\qquad
L\varphi=\int_{\mathbb{R}^n}f(x)\varphi(x)\, dx
$$
is a linear functional on $X$. We will show that $L$ is unbounded (with respect to the $L^2$ norm in $X$). Suppose to the contrary that $L$ is bounded. Then it  extends uniquely to a bounded linear functional
$\tilde{L}:L^2(\mathbb{R}^n)\to\mathbb{K}$. Therefore, the Riesz Representation yields existence of $g\in L^2(\mathbb{R}^n)$, such that
$$
\tilde{L}\varphi=\int_{\mathbb{R}^n}\varphi(x)\overline{g(x)}\, dx
\quad
\varphi\in L^2(\mathbb{R}^n).
$$
In particular
$$
\int_{\mathbb{R}^n}(f(x)-\overline{g(x)})\varphi(x)\, dx=0
\quad
\text{for all $\varphi\in C_c^{\infty}(\mathbb{R}^n)$.}
$$
Therefore $f-\overline{g}=0$ a.e., $f=\overline{g}\in L^2(\mathbb{R}^n)$ which contradicts the assumption about $f$.
We proved that $L$ is discontinuous, hence by the lemma, $\ker L\subset X=C_c^\infty(\mathbb{R}^n)$ is dense and hence it is dense in $L^2(\mathbb{R}^n)$. Now applying the Gramm-Schmidt orthogonalization to a countable and dense subset of $\ker L$ we obtain an orthonormal basis
$\{\varphi_k\}_{k=1}^\infty\subset\ker L$ of $L^2(\mathbb{R}^n)$ consisting of compactly supported smooth functions. Since $L\varphi_k=0$ for all $k$, (1) follows.
$\Box$
A: I believe the following is a simple counterexample:
Let $(X,\mu)$ be $\mathbb{N}$ with the counting measure (so I will be writing $\ell^2$ for $L^2(X,\mu)$.  Let $g=0$ and $\tilde g(n) = (-1)^n$.  Let $D$ be the set of (finite) linear combinations of sequences of the form $(0,\ldots,0,1,1,0,\ldots)$ (i.e., two consecutive $1$'s).  Clearly, if $f\in D$ then $\sum_n f(n)\,g(n)$ is convergent, in fact, it is even a finite sum, and its value is $0$ because this holds on the elements spanning $D$.
Now $D$ is dense in $\ell^2$ (for the norm topology): to see this, it is enough to show $D^\perp = 0$ (orthogonal in $\ell^2$).  But if $h \in D^\perp$ then we have $h(n) = -h(n+1)$ for every $n$, so $h$ is proportional to $\tilde g$, which is not in $\ell^2$, so $h=0$.
